Skip to content
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

Closed
kaigoh opened this issue Apr 16, 2021 · 6 comments
Closed
Assignees
Labels

Comments

@kaigoh
Copy link

kaigoh commented Apr 16, 2021

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.

@kaigoh kaigoh added the type:question general questions label Apr 16, 2021
@github-actions
Copy link

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

@ngocketit
Copy link

Same here but with PostgreSQL. Any ideas?

@ngocketit
Copy link

I noticed that this won't work:

type Option struct {
	Base
	Content    string    `gorm:"not null"`
	QuestionID string    `gorm:"not null"`
	Question   *Question `gorm:"constraint:OnDelete:CASCADE;"`
}

type Question struct {
	Base
	Content    string                  `gorm:"not null"`
	Options    []*Option
}

Notice that I have references both ways. Removing Options []*Option from Question then it works. However, this is not good since you normally want to have references on both sides of the relationship. Many other ORM frameworks support that. Any ideas?

@ngocketit
Copy link

Or maybe because Gorm prefers to have the constraint on the parent side of the relationship. So the following code works:

type Option struct {
	Base
	Content    string    `gorm:"not null"`
	QuestionID string    `gorm:"not null"`
	Question   *Question
}

type Question struct {
	Base
	Content    string                  `gorm:"not null"`
	Options    []*Option            `gorm:"constraint:OnDelete:CASCADE;"`
}

@kaigoh
Copy link
Author

kaigoh commented Dec 17, 2021

@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?

@dablelv
Copy link

dablelv commented Jan 27, 2024

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants