-
Notifications
You must be signed in to change notification settings - Fork 63
New issue
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
pq: invalid input syntax for type json
When setup default value tag for json.RawMessage
#40
Comments
pq: invalid input syntax for type json
When setup default tags for json.RawMessage
pq: invalid input syntax for type json
When setup default value tag for json.RawMessage
Sorry, I should probably test this before answering, but what happens if you remove your quotes from the tag? It seems like your result gets double quoted. Data json.RawMessage `gorm:"not null;default:{}"` Also, it seems like gormbulk.BulkInsert(db, []interface{}{&ExampleModel{}}, 1000, "data") I'll look into this and test more throughly when I get some spare time! |
You will get type ExampleModel struct {
gorm.Model
Data json.RawMessage `gorm:"not null;default:{}"`
} |
Interesting, I found another issue if I changed the insert value as: testData := &ExampleModel{Data: ([]byte)(`{"key":"value"}`)} It works when calling INSERT INTO "example_models" ("created_at","updated_at","deleted_at","data")
VALUES ('2020-05-15 16:46:57','2020-05-15 16:46:57',NULL,'[123 34 107 101 121 34 58 34 118 97 108 117 101 34 125]')
RETURNING "example_models"."id" But failed when calling INSERT INTO "example_models" ("created_at", "data", "deleted_at", "updated_at")
VALUES ('2020-05-15 16:46:58', 123,34,107,101,121,34,58,34,118,97,108,117,101,34,125, NULL, '2020-05-15 16:46:58') The error: pq: INSERT has more expressions than target columns |
Thanks for the test! This does not really surprise me since |
any workaround to work with? |
Thanks for reporting @twsiyuan. I am investigating this issue. Actually, I don't fully understand the internal behaviors of GORM, and I'm trying to figure out a good way to fix this. I've found that there are two problems around this.
Altough it's just a workaround, to avoid first problem, you can pass For the second, however, can only be avoided by modifying the internals of this library. In any case, using |
This is the my testing example which set up the database using dockertest:
How to test
main.go
go mod init
under the same folder to setup the package dependency tooldockerRDS
to provide a*gorm.DB
go run main.go
Issue
I saw the SQL when calling
db.Save(...)
is correct:But found invalid SQL when calling
gormbulk.BulkInsert(db, ....)
:The error:
The text was updated successfully, but these errors were encountered: