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() should always migrate checks, even there is no relationship constraints. #5644

Merged
merged 2 commits into from Sep 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 5 additions & 5 deletions migrator/migrator.go
Expand Up @@ -135,12 +135,12 @@ func (m Migrator) AutoMigrate(values ...interface{}) error {
}
}
}
}

for _, chk := range stmt.Schema.ParseCheckConstraints() {
if !tx.Migrator().HasConstraint(value, chk.Name) {
if err := tx.Migrator().CreateConstraint(value, chk.Name); err != nil {
return err
}
for _, chk := range stmt.Schema.ParseCheckConstraints() {
if !tx.Migrator().HasConstraint(value, chk.Name) {
if err := tx.Migrator().CreateConstraint(value, chk.Name); err != nil {
return err
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions tests/postgres_test.go
Expand Up @@ -63,13 +63,13 @@ func TestPostgres(t *testing.T) {
}

type Post struct {
ID uuid.UUID `gorm:"primary_key;type:uuid;default:uuid_generate_v4();autoincrement"`
ID uuid.UUID `gorm:"primary_key;type:uuid;default:uuid_generate_v4();"`
Title string
Categories []*Category `gorm:"Many2Many:post_categories"`
}

type Category struct {
ID uuid.UUID `gorm:"primary_key;type:uuid;default:uuid_generate_v4();autoincrement"`
ID uuid.UUID `gorm:"primary_key;type:uuid;default:uuid_generate_v4();"`
Title string
Posts []*Post `gorm:"Many2Many:post_categories"`
}
Expand Down