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

AutoMigrate not updating existing Foreign Key with OnDelete constraint #5559

Closed
Avinodh opened this issue Jul 31, 2022 · 11 comments
Closed

AutoMigrate not updating existing Foreign Key with OnDelete constraint #5559

Avinodh opened this issue Jul 31, 2022 · 11 comments
Assignees
Labels
type:feature_request feature request

Comments

@Avinodh
Copy link

Avinodh commented Jul 31, 2022

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?

@github-actions github-actions bot added the type:missing reproduction steps missing reproduction steps label Jul 31, 2022
@github-actions
Copy link

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.ioSearch Before Asking

@a631807682
Copy link
Member

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

@Avinodh
Copy link
Author

Avinodh commented Aug 1, 2022

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

@a631807682
Copy link
Member

a631807682 commented Aug 1, 2022

It sounds like MigrateColumn problem, I'll check it later

@github-actions
Copy link

github-actions bot commented Aug 1, 2022

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.ioSearch Before Asking

@a631807682
Copy link
Member

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

@a631807682 a631807682 added type:feature_request feature request and removed type:missing reproduction steps missing reproduction steps status:stale labels Aug 1, 2022
@Avinodh
Copy link
Author

Avinodh commented Aug 5, 2022

Thanks @a631807682 , that is unfortunate. Hopefully a fix for this can be prioritized soon.

stbenjam added a commit to stbenjam/sippy that referenced this issue Oct 14, 2022
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)
stbenjam added a commit to stbenjam/sippy that referenced this issue Oct 14, 2022
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)
stbenjam added a commit to stbenjam/sippy that referenced this issue Oct 17, 2022
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)
@andig
Copy link

andig commented Mar 9, 2023

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

@rootgrandfather
Copy link

same

@bkmeneguello
Copy link

This issue happens because the Migrator only checks if the constraint is present by the name, and not by its content:

constraint.Schema == stmt.Schema && !queryTx.Migrator().HasConstraint(value, constraint.Name) {

Probably a method MigrateConstraint is necessary to check the definition and update accordingly.

@a631807682
Copy link
Member

#6381 (comment)

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

No branches or pull requests

6 participants