Skip to content
This repository was archived by the owner on Jul 12, 2023. It is now read-only.

Commit 1b0a580

Browse files
authored
Drop FK constraints if they already exist before creating (#1534)
1 parent f2b0194 commit 1b0a580

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

pkg/database/migrations.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1764,9 +1764,15 @@ func (db *Database) Migrations(ctx context.Context) []*gormigrate.Migration {
17641764

17651765
// Convert realm and user into real foreign keys. If the parent record
17661766
// is deleted, also delete the membership record.
1767+
if err := tx.Exec(`ALTER TABLE user_realms DROP CONSTRAINT IF EXISTS fk_realm`).Error; err != nil {
1768+
return err
1769+
}
17671770
if err := tx.Exec(`ALTER TABLE user_realms ADD CONSTRAINT fk_realm FOREIGN KEY (realm_id) REFERENCES realms(id) ON DELETE CASCADE`).Error; err != nil {
17681771
return err
17691772
}
1773+
if err := tx.Exec(`ALTER TABLE user_realms DROP CONSTRAINT IF EXISTS fk_user`).Error; err != nil {
1774+
return err
1775+
}
17701776
if err := tx.Exec(`ALTER TABLE user_realms ADD CONSTRAINT fk_user FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE`).Error; err != nil {
17711777
return err
17721778
}

0 commit comments

Comments
 (0)