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 Migration - OnUpdate / OnDelete CASCADE not being applied to schema #4289
Comments
This issue has been automatically marked as stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 30 days |
Same here but with PostgreSQL. Any ideas? |
I noticed that this won't work:
Notice that I have references both ways. Removing |
Or maybe because Gorm prefers to have the constraint on the parent side of the relationship. So the following code works:
|
@ngocketit you are spot on; it seems Gorm wants the constraint applied NOT at the (in your example) QuestionID member, but at the Question. Good catch! @jinzhu Would it be possible to make this clearer in the documentation? I've looked and whilst it is (now) obvious that the constraint is applied to the Company stuct member (https://gorm.io/docs/belongs_to.html#FOREIGN-KEY-Constraints), it may be better for others to highlight where the constraint should be placed in their model definitions? |
I also encountered this problem. As of now, the document has not been revised, which is very misleading. If document is right, so this is an unimplemented feature. |
Hi! Firstly, thanks for a great library, it is really intuitive and the documentation is excellent.
However, I am having difficulty understanding why a constraint I am adding to my foreign keys is not being applied to the database schemas when migrating. Just to make sure it is not related to altering existing tables, I have dropped them and recreated them using AutoMigrate and still the issue persists. I have to then manually change the schema definitions by hand to cascade deletes and updates.
These are my GORM tags:
gorm:"index; not null; constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"
gorm:"index;index:idx_calling_point_index,unique; index:idx_tiploc,priority:2; not null; constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"
https://gorm.io/docs/associations.html#tags
I'm hoping I've either simply missed something, or that for some reason GORM doesn't respect the "CASCADE" constraint for FKs. If this is the case, some advice on how to emulate the OnDelete CASCADE behavior for three levels of relationships.
The text was updated successfully, but these errors were encountered: