gorm
gorm copied to clipboard
AutoMigrate not updating existing Foreign Key with OnDelete constraint
Description
type Parent struct {
ParentID uuid.UUID `gorm:"type:uuid;primary_key;" json:"parentId,omitempty"`
Name string `gorm:"type:varchar(255)" json:"name"`
}
type Child struct {
ChildID uuid.UUID `gorm:"type:uuid;primary_key;" json:"childId"`
Name string `gorm:"type:varchar(255)" json:"name"`
// Foreign Key
Parent Parent `gorm:"foreignkey:ParentID;constraint:OnDelete:CASCADE;"`
}
main.go:
func main() {
. . .
// Auto Migrate
//
db.AutoMigrate(
&models.Parent{},
&models.Child{},
)
The existing model contained the FK relation already defined/applied to the DB. When I added the constraint to cascade DELETE (constraint:OnDelete:CASCADE;) and started up the server, I don't see this as having been applied.
Is this by design, or is this a bug? I found the following issue (#4110) which added support for migrating unique constraints. Perhaps OnDelete/OnUpdate require such a fix as well?
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 30 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.io ✨ Search Before Asking ✨
What dose I don't see this as having been applied
means?
I can not find the difference from the test, can you describe it?
https://github.com/go-gorm/gorm/blob/master/tests/associations_test.go#L128
@a631807682 - In the test you linked, the table is Dropped and then AutoMigrate() is called:
DB.Migrator().DropTable(&Profile{}, &Member{})
if err := DB.AutoMigrate(&Profile{}, &Member{}); err != nil {
t.Fatalf("Failed to migrate, got error: %v", err)
}
The issue I mention relates to an existing table on which a foreign key has already been defined. I am now trying to add on a 'constraint:OnDelete:CASCADE;' annotation to the model, but it is a no-op. The only way I could get the AutoMigration() to modify the FK on the table to cascade the delete was by manually dropping the the FK from the table, and having AutoMigrate() re-create it.
It doesn't seem like AutoMigrate can do this currently without requiring manually dropping the FK first.
It sounds like MigrateColumn
problem, I'll check it later
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 30 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.io ✨ Search Before Asking ✨
Patch Constraint is not currently supported, and Constraint changes do not seem to take effect
https://github.com/go-gorm/gorm/blob/master/migrator/migrator.go#L133
Thanks @a631807682 , that is unfortunate. Hopefully a fix for this can be prioritized soon.
@Avinodh I've just stumbled about the same problem:
type Device struct {
ID int `gorm:"primarykey"`
Class Class
Type string
Details []DeviceDetail `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE"`
}
type DeviceDetail struct {
DeviceID int `gorm:"primarykey"`
Key string `gorm:"primarykey"`
Value string
}
Any fix in sight?
same
This issue happens because the Migrator only checks if the constraint is present by the name, and not by its content: https://github.com/go-gorm/gorm/blob/418ee3fc1939d87a05bbb8ac6d7c7223e2c4571f/migrator/migrator.go#L166
Probably a method MigrateConstraint
is necessary to check the definition and update accordingly.
https://github.com/go-gorm/gorm/issues/6381#issuecomment-1929427042