2

I'm simply trying to insert my object in which one of the fields is a slice. So I built my query with Squirrel like below:

squirrel.StatementBuilder.PlaceholderFormat(squirrel.Dollar).RunWith(db).Insert("educations").
        Columns("id", "school", "city", "state", "degree", "month_start", "year_start", "month_end", "year_end", "order", "logo_url", "created_at", "updated_at", "style", "descriptions").
        Values(
            uuid.Must(uuid.NewV4()).String(),
            education.School,
            education.City,
            education.State,
            education.Degree,
            education.MonthStart,
            education.YearStart,
            education.MonthEnd,
            education.YearEnd,
            education.Order,
            education.LogoURL,
            currentTime,
            currentTime,
            savedStyle.ID,
            pq.Array(education.Descriptions),
        ).
        Suffix("RETURNING *")

The field descriptions is just a slice of strings. However, I'm getting an error message saying ERROR: syntax error at or near "order" (SQLSTATE 42601). Need another pair of eyes to see what goes wrong.

Here's the .ToSql() result:

INSERT INTO educations (id,school,city,state,degree,month_start,year_start,month_end,year_end,order,logo_url,
created_at,updated_at,style,descriptions) VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,
$12,$13,$14,$15) RETURNING * [9d54d890-1d24-47a0-a96d-652e525b6c72 XXX University San Jose, CA Bachelor of Science 7 2014 0xc000410450 0xc000410458 0 https://i.imgur.com/123123.png 2020-05-02 19:50:52.626946 -0700 PDT m=+74.496935801 2020-05-02 19:50:52.626946 -0700 PDT m=+74.496935801 0xc000169510 {[0xc0005b34c0 0xc0005b34d0 0xc0005b34e0]}] <nil>
thousight
  • 1,134
  • 1
  • 14
  • 39
  • 4
    This might be happening because the table has a column named `order`, but `order` is an sql keyword. Rename the column to something else. – Mark May 03 '20 at 03:19
  • 5
    If you can't rename the column or don't want to, find some way to configure Squirrel to double quote the column names, maybe even `Columns(..., "\"order\"", ...)` would be enough. – mu is too short May 03 '20 at 04:20
  • thanks all, I think adding `\"` around order works. Feel free to post it as the answer and I'll get it checked. – thousight May 04 '20 at 23:27

0 Answers0