We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
go-gorm/playground#668
When inserting go arrays or slices in a GORM model, the generated SQL uses them as records, so the insert fails.
For example:
type Test struct { Data []float64 `gorm:"type:float8[]"` } test := Test{Data: []float64{8, 4, 2, 1, 0.5}} DB.Create(&test)
gives the SQL:
INSERT INTO "tests" ("data") VALUES ((8,4,2,1,0.5))
And I get the error ERROR: column "data" is of type double precision[] but expression is of type record (SQLSTATE 42804)
ERROR: column "data" is of type double precision[] but expression is of type record (SQLSTATE 42804)
Instead, the SQL should be:
INSERT INTO "tests" ("data") VALUES ('{8,4,2,1,0.5}')
or
INSERT INTO "tests" ("data") VALUES (ARRAY [8,4,2,1,0.5])
If I use pgx directly, and do:
conn, err := pgx.Connect(...) arr := []float64{8, 4, 2, 1, 0.5} _, err = conn.Exec(context.Background(), "INSERT INTO tests (data) VALUES($1)", arr)
It works as expected, generating SQL with the '{...}' syntax
'{...}'
The text was updated successfully, but these errors were encountered:
i encountered the same issue, @sleddev did you manage to sort it out?
Sorry, something went wrong.
@carlcris I ended up making type aliases, and implementing my own Scan and Value functions on those types.
Scan
Value
Here's my current implementation: https://gist.github.com/sleddev/17ddd0e5f2a037ecf7bb6527367cb916
There may be edge cases I didn't account for, but from my testing and usage works nicely.
To use it, just swap []float64 to pg.Float8Array in your model
[]float64
pg.Float8Array
Is this issue resolved? I'm still facing the same issue in 1.5.9
jinzhu
No branches or pull requests
GORM Playground Link
go-gorm/playground#668
Description
Explain your user case and expected results
When inserting go arrays or slices in a GORM model, the generated SQL uses them as records, so the insert fails.
For example:
gives the SQL:
And I get the error
ERROR: column "data" is of type double precision[] but expression is of type record (SQLSTATE 42804)
Instead, the SQL should be:
or
If I use pgx directly, and do:
It works as expected, generating SQL with the
'{...}'
syntaxThe text was updated successfully, but these errors were encountered: