thousight
thousight

Reputation: 1154

Golang Postgres pq failed scanning to *string

I'm trying to scan a postgresql list into an empty slice of string. However, I'm getting the error below: Failed creating education: sql: Scan error on column index 14, name "descriptions": unsupported Scan, storing driver.Value type string into type *[]*string. Looks like I need to customize the scanner somehow, but how do I do that with squirrel? thanks.

Here's how I'm building the query:

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 *").
        Scan(
            &savedEducation.ID,
            &savedEducation.School,
            &savedEducation.City,
            &savedEducation.State,
            &savedEducation.Degree,
            &savedEducation.MonthStart,
            &savedEducation.YearStart,
            &savedEducation.MonthEnd,
            &savedEducation.YearEnd,
            &savedEducation.Order,
            &savedEducation.LogoURL,
            &savedEducation.CreatedAt,
            &savedEducation.UpdatedAt,
            &ignored,
            &savedEducation.Descriptions,
        )

Upvotes: 1

Views: 2196

Answers (1)

karora
karora

Reputation: 1303

You need to use the pq.StringArray type for the field savedEducation.Descriptions. This is a []string with the additional Value() and Scan() methods provided by the pq library.

Upvotes: 0

Related Questions