-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
(MySql)OnConflict for hasOne/hasMany assotiations - specify all (non keys) fields updates for all tables #3506
Comments
anyway, the default behavior or some option should do let me know if the patch from my side is needed. thanks, Dima |
Yes, it would be helpful, do you have any idea for how to change it? |
From the theory point - actually it is a just a bug. Waiting for your reply with your understanding and vision and then - lets go to the source code of associations.go lines 160-175 - why we just specify foreign keys for update in associations (the rest of the code may be affected is just the way to make things more obious). But first we need to understand what we would break and how is possible that nobody has ccomplained that assotiations in gorm2 with mysql where never updated (only object replacement is possible) |
If your associations's data is not complete, which is common case when you are getting your data from API call, your association's data will be corrupted when saving your data, GORM's default behavior should never do that to users. |
ok but how we've lost this normal feature by migrating from GORM1 to 2 ( due to the lack of joins)? Basically we had a working source code in production. Just to update an entity with associations updated |
btw workaround is to delete all foreign keys and IDs but we have our own delete orphans implementation (also miissed in GORM) and gorm is recreates new records and we delete the old ones. It is totally wrong - but we can't wait anymore. will try to return to you with a patch soon in order just to add all fields by some flag |
anyway this workaround is ugly and it reduces performance - an actual fix required |
Following code will save full associations DB.Session(&gorm.Session{FullSaveAssociations: true}).Save(&user) |
thanks |
Your Question
let's suppose the user table has addresses discounts, whatever - any kinds of hasOne and hasMany relationships for a MySQL
so how to update all fields from the forementioned associations
Upsert / On Conflict how to update ALL columns including all associations (hasOne etc) on conflict?
Db.Clauses(clause.OnConflict
{
Tables:[]clause.Table{"Addresses", "Discounts"}
Columns: []clause.Column{{Name: "some_foreign key_to_root_id"}},
DoUpdates: clause.AssignmentColumns([]string{ALL}),
}).Create(&users)
currently, NOTHING is updated at all
Actually it is required to be in some general Config and not in each Create/Upsert specified
The text was updated successfully, but these errors were encountered: