go-gorm / gorm

The fantastic ORM library for Golang, aims to be developer friendly
https://gorm.io
MIT License
36.92k stars 3.93k forks source link

Replace associations not working as expected #4010

Closed eloo closed 3 years ago

eloo commented 3 years ago

GORM Playground Link

https://github.com/go-gorm/playground/pull/264

Description

It looks like the association replace function is not working as expected as it was already mentioned 3 month ago into the docs comments (http://disq.us/p/2cz6yps)

So either its a bug or the documentation is not sufficient enough.

Maybe you can provide a proper example of how associations can be really replaced?

Thanks

github-actions[bot] commented 3 years ago

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 2 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.ioSearch Before Asking

jinzhu commented 3 years ago
image

if GORM doesn't works like as the document said, create a bug report.

eloo commented 3 years ago

@jinzhu i guess i have created a bug report here? so why this closed? does seem to be fixed

jinzhu commented 3 years ago

I think this should works like the above document, a bug report have to follow the guide by having a reproducible playground PR. feel free to create a new one.

eloo commented 3 years ago

yep i know.. and also the guideline is that i have 2 days to provide this.. so please don't close my issue before these 2 days

github-actions[bot] commented 3 years ago

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 2 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.ioSearch Before Asking

eloo commented 3 years ago

playground link is now added

jinzhu commented 3 years ago

Hello,

Association mode doesn't expect to update associations, it will use the OnConflict DoNothing mode.

Just added FullSaveAssociations support for Association mode, you could write it like below using the latest master.

err = DB.Model(&user).Session(&gorm.Session{FullSaveAssociations: true}).Association("Pets").Replace(&pet2)

Thank you for your report.

eloo commented 3 years ago

@jinzhu

Hi, okay just tested it and it looks promising 👍 Do you mind to add this as an example to your docs?

Further i've seen that this will leave orphans in the database. Is there any way to get rid of orphans directly?

Sh1d0w commented 3 years ago

@jinzhu I second @eloo question. Why replacing model associations, does update set null on the foreign key of the rows, instead of deleting them? It just don't make sense. Furthermore Replace won't work if you set the models foreign key to not null, as it will fail to update the keys to null. This seems like a bug to me.

PS. Same behaviour have Association.Delete, it again leaves orphans in DB instead of deleting them.

dharkness commented 2 years ago

Adding Session(&gorm.Session{FullSaveAssociations: true}) did not solve the issue for me. The child records' foreign keys are set to null instead of being deleted. Why doesn't it delete the children, and how can I make it do so? Is there a way to tell Gorm that the child rows are owned by the parent?

Edit: Just noticed you said it's on the latest master. Does this change cause it to delete rows instead of orphaning them?

FonzTech commented 2 years ago

PR from @bfabio actually fixes the problem, but for those who can't upgrade GORM without hassle (there could be some minor behaviour changes, which break your application), you have to set the foreign key to be nullable. Then, after you replaced associations, clear all the orphaned rows.