Skip to content

AutoMigrate not updating existing Foreign Key with OnDelete constraint #5559

@Avinodh

Description

@Avinodh

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?

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions