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,6 +97,7 @@ func (m Migrator) AutoMigrate(values ...interface{}) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, rel := range stmt.Schema.Relationships.Relations {
|
for _, rel := range stmt.Schema.Relationships.Relations {
|
||||||
|
if !m.DB.Config.DisableForeignKeyConstraintWhenMigrating {
|
||||||
if constraint := rel.ParseConstraint(); constraint != nil {
|
if constraint := rel.ParseConstraint(); constraint != nil {
|
||||||
if constraint.Schema == stmt.Schema {
|
if constraint.Schema == stmt.Schema {
|
||||||
if !tx.Migrator().HasConstraint(value, constraint.Name) {
|
if !tx.Migrator().HasConstraint(value, constraint.Name) {
|
||||||
|
@ -106,6 +107,7 @@ func (m Migrator) AutoMigrate(values ...interface{}) error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for _, chk := range stmt.Schema.ParseCheckConstraints() {
|
for _, chk := range stmt.Schema.ParseCheckConstraints() {
|
||||||
if !tx.Migrator().HasConstraint(value, chk.Name) {
|
if !tx.Migrator().HasConstraint(value, chk.Name) {
|
||||||
|
@ -179,6 +181,7 @@ func (m Migrator) CreateTable(values ...interface{}) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, rel := range stmt.Schema.Relationships.Relations {
|
for _, rel := range stmt.Schema.Relationships.Relations {
|
||||||
|
if !m.DB.DisableForeignKeyConstraintWhenMigrating {
|
||||||
if constraint := rel.ParseConstraint(); constraint != nil {
|
if constraint := rel.ParseConstraint(); constraint != nil {
|
||||||
if constraint.Schema == stmt.Schema {
|
if constraint.Schema == stmt.Schema {
|
||||||
sql, vars := buildConstraint(constraint)
|
sql, vars := buildConstraint(constraint)
|
||||||
|
@ -186,6 +189,7 @@ func (m Migrator) CreateTable(values ...interface{}) error {
|
||||||
values = append(values, vars...)
|
values = append(values, vars...)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// create join table
|
// create join table
|
||||||
if rel.JoinTable != nil {
|
if rel.JoinTable != nil {
|
||||||
|
|
Loading…
Reference in New Issue