修复gorm自动创建无用外键的问题
This commit is contained in:
@@ -261,6 +261,5 @@ func initStorage(cfg config.DatabaseConfig, logger *logs.Logger) (database.Stora
|
|||||||
if err := storage.Migrate(models.GetAllModels()...); err != nil {
|
if err := storage.Migrate(models.GetAllModels()...); err != nil {
|
||||||
return nil, fmt.Errorf("数据库迁移失败: %w", err)
|
return nil, fmt.Errorf("数据库迁移失败: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return storage, nil
|
return storage, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -70,6 +70,9 @@ func (ps *PostgresStorage) Connect() error {
|
|||||||
sqlDB.SetMaxIdleConns(ps.maxIdleConns)
|
sqlDB.SetMaxIdleConns(ps.maxIdleConns)
|
||||||
sqlDB.SetConnMaxLifetime(time.Duration(ps.connMaxLifetime) * time.Second)
|
sqlDB.SetConnMaxLifetime(time.Duration(ps.connMaxLifetime) * time.Second)
|
||||||
|
|
||||||
|
// gorm会根据字段名自动创建外键约束, 但触发器Task的PlanID是不存在的, 所以需要关闭, 这个关闭对
|
||||||
|
ps.db.DisableForeignKeyConstraintWhenMigrating = true
|
||||||
|
|
||||||
ps.logger.Info("PostgreSQL数据库连接成功")
|
ps.logger.Info("PostgreSQL数据库连接成功")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user