Gorm: Insert data into database using interface{}

Created on 23 Nov 2019  路  4Comments  路  Source: go-gorm/gorm

In my use case, I have to insert data into a table. I am unaware of the schema of the table. I have the data in json format. Is there any way, I can insert the data in the table with out defining any structs because the data and tables will change very frequently.

I have tried this, but it did not work.

data := {"uid":"1234", "email_id":"[email protected]"}

var insertData interface{}
_ = json.Unmarshal([]byte(data), &insertData )
db.Table("USR_DATA").Create(&insert)

gorm_v1

All 4 comments

That's not possible with Gorm, you have to use a struct to create new record. Otherwise, try to build the raw SQL sentence yourself.

By the way, it sounds weird to me that the table schema would be changed "very frequently" when you're using Gorm, which is an ORM lib for the relational database (SQL)... You might want to use the non-relational database (NoSQL) instead.

The schema would change "very frequently" for the application. I meant I have to store the data in different database tables and I have no idea about the schema of the tables.

try to build the raw SQL sentence yourself.

Agree.

I think Gorm (and any other ORM libraries) is not suitable for such use case.

This issue will be automatically closed because it is marked as GORM V1 issue, we have released the public testing GORM V2 release and its documents https://v2.gorm.io/docs/ already, the testing release has been used in some production services for a while, and going to release the final version in following weeks, we are still actively collecting feedback before it, please open a new issue for any suggestion or problem, thank you

Also check out https://github.com/go-gorm/gorm/wiki/GORM-V2-Release-Note-Draft for how to use the public testing version and its changelog

Was this page helpful?
0 / 5 - 0 ratings

Related issues

satb picture satb  路  3Comments

kumarsiva07 picture kumarsiva07  路  3Comments

hypertornado picture hypertornado  路  3Comments

pjebs picture pjebs  路  3Comments

Quentin-M picture Quentin-M  路  3Comments