-
-
Notifications
You must be signed in to change notification settings - Fork 4k
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
AutoMigrate not updating existing Foreign Key with OnDelete constraint #5559
Comments
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 |
What dose |
@a631807682 - In the test you linked, the table is Dropped and then AutoMigrate() is called:
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. It doesn't seem like AutoMigrate can do this currently without requiring manually dropping the FK first. |
It sounds like |
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 |
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. |
When deleting a DB row that has relations, these are not automatically deleted and the foreign key constraints prevent you from actually deleting the row. We need to cascade the deletes, however gorm does not support patching constraints[1], therefore we have to delete them. There's a one-time SQL script in scripts/ that we can run to clear the constraints, which gorm wil re-add back. I've tested this on a production database restore and it seems to do what I expect. We could write our own migrator, but it looks somewhat challenging, and this is probably a one-ish time thing. [1] go-gorm/gorm#5559 (comment)
When deleting a DB row that has relations, these are not automatically deleted and the foreign key constraints prevent you from actually deleting the row. We need to cascade the deletes, however gorm does not support patching constraints[1], therefore we have to delete them. There's a one-time SQL script in scripts/ that we can run to clear the constraints, which gorm wil re-add back. I've tested this on a production database restore and it seems to do what I expect. We could write our own migrator, but it looks somewhat challenging, and this is probably a one-ish time thing. [1] go-gorm/gorm#5559 (comment)
When deleting a DB row that has relations, these are not automatically deleted and the foreign key constraints prevent you from actually deleting the row. We need to cascade the deletes, however gorm does not support patching constraints[1], therefore we have to delete them. There's a one-time SQL script in scripts/ that we can run to clear the constraints, which gorm wil re-add back. I've tested this on a production database restore and it seems to do what I expect. We could write our own migrator, but it looks somewhat challenging, and this is probably a one-ish time thing. [1] go-gorm/gorm#5559 (comment)
@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: Line 166 in 418ee3f
Probably a method MigrateConstraint is necessary to check the definition and update accordingly.
|
Description
main.go:
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 text was updated successfully, but these errors were encountered: