forked from mirror/gorm
Allow DisableForeignKeyConstraintWhenMigrating
This commit is contained in:
parent
7851faa094
commit
5d044642d1
2
gorm.go
2
gorm.go
|
@ -30,6 +30,8 @@ type Config struct {
|
||||||
PrepareStmt bool
|
PrepareStmt bool
|
||||||
// DisableAutomaticPing
|
// DisableAutomaticPing
|
||||||
DisableAutomaticPing bool
|
DisableAutomaticPing bool
|
||||||
|
// DisableForeignKeyConstraintWhenMigrating
|
||||||
|
DisableForeignKeyConstraintWhenMigrating bool
|
||||||
|
|
||||||
// ClauseBuilders clause builder
|
// ClauseBuilders clause builder
|
||||||
ClauseBuilders map[string]clause.ClauseBuilder
|
ClauseBuilders map[string]clause.ClauseBuilder
|
||||||
|
|
|
@ -97,11 +97,13 @@ func (m Migrator) AutoMigrate(values ...interface{}) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, rel := range stmt.Schema.Relationships.Relations {
|
for _, rel := range stmt.Schema.Relationships.Relations {
|
||||||
if constraint := rel.ParseConstraint(); constraint != nil {
|
if !m.DB.Config.DisableForeignKeyConstraintWhenMigrating {
|
||||||
if constraint.Schema == stmt.Schema {
|
if constraint := rel.ParseConstraint(); constraint != nil {
|
||||||
if !tx.Migrator().HasConstraint(value, constraint.Name) {
|
if constraint.Schema == stmt.Schema {
|
||||||
if err := tx.Migrator().CreateConstraint(value, constraint.Name); err != nil {
|
if !tx.Migrator().HasConstraint(value, constraint.Name) {
|
||||||
return err
|
if err := tx.Migrator().CreateConstraint(value, constraint.Name); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -179,11 +181,13 @@ func (m Migrator) CreateTable(values ...interface{}) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, rel := range stmt.Schema.Relationships.Relations {
|
for _, rel := range stmt.Schema.Relationships.Relations {
|
||||||
if constraint := rel.ParseConstraint(); constraint != nil {
|
if !m.DB.DisableForeignKeyConstraintWhenMigrating {
|
||||||
if constraint.Schema == stmt.Schema {
|
if constraint := rel.ParseConstraint(); constraint != nil {
|
||||||
sql, vars := buildConstraint(constraint)
|
if constraint.Schema == stmt.Schema {
|
||||||
createTableSQL += sql + ","
|
sql, vars := buildConstraint(constraint)
|
||||||
values = append(values, vars...)
|
createTableSQL += sql + ","
|
||||||
|
values = append(values, vars...)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue