Skip to content

Commit a3bbbfc

Browse files
committed
Fix automigrate bug
1 parent d223e0d commit a3bbbfc

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

api/pkg/di/container.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,12 @@ func (container *Container) DB() (db *gorm.DB) {
321321

322322
container.logger.Debug(fmt.Sprintf("Running migrations for %T", db))
323323

324+
// This prevents a bug in the Gorm AutoMigrate where it tries to delete this no existent constraints
325+
db.Exec(`
326+
ALTER TABLE users ADD CONSTRAINT IF NOT EXISTS uni_users_api_key CHECK (api_key IS NOT NULL);
327+
ALTER TABLE phone_api_keys ADD CONSTRAINT IF NOT EXISTS uni_phone_api_keys_api_key CHECK (api_key IS NOT NULL);
328+
ALTER TABLE discords ADD CONSTRAINT IF NOT EXISTS uni_discords_server_id CHECK (server_id IS NOT NULL);`)
329+
324330
if err = db.AutoMigrate(&entities.Message{}); err != nil {
325331
container.logger.Fatal(stacktrace.Propagate(err, fmt.Sprintf("cannot migrate %T", &entities.Message{})))
326332
}

0 commit comments

Comments
 (0)