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.

Comment From: github-actions[bot]

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

Comment From: ngocketit

Same here but with PostgreSQL. Any ideas?

Comment From: ngocketit

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?

Comment From: ngocketit

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;"`
}

Comment From: kaigoh

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

Comment From: dablelv

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