@@ -143,8 +143,11 @@ func (m Migrator) AutoMigrate(values ...interface{}) error {
143
143
}
144
144
}
145
145
146
- for _ , rel := range stmt .Schema .Relationships .Relations {
147
- if ! m .DB .Config .DisableForeignKeyConstraintWhenMigrating {
146
+ if ! m .DB .DisableForeignKeyConstraintWhenMigrating && ! m .DB .IgnoreRelationshipsWhenMigrating {
147
+ for _ , rel := range stmt .Schema .Relationships .Relations {
148
+ if rel .Field .IgnoreMigration {
149
+ continue
150
+ }
148
151
if constraint := rel .ParseConstraint (); constraint != nil &&
149
152
constraint .Schema == stmt .Schema && ! queryTx .Migrator ().HasConstraint (value , constraint .Name ) {
150
153
if err := execTx .Migrator ().CreateConstraint (value , constraint .Name ); err != nil {
@@ -244,8 +247,11 @@ func (m Migrator) CreateTable(values ...interface{}) error {
244
247
}
245
248
}
246
249
247
- for _ , rel := range stmt .Schema .Relationships .Relations {
248
- if ! m .DB .DisableForeignKeyConstraintWhenMigrating {
250
+ if ! m .DB .DisableForeignKeyConstraintWhenMigrating && ! m .DB .IgnoreRelationshipsWhenMigrating {
251
+ for _ , rel := range stmt .Schema .Relationships .Relations {
252
+ if rel .Field .IgnoreMigration {
253
+ continue
254
+ }
249
255
if constraint := rel .ParseConstraint (); constraint != nil {
250
256
if constraint .Schema == stmt .Schema {
251
257
sql , vars := buildConstraint (constraint )
@@ -818,26 +824,31 @@ func (m Migrator) ReorderModels(values []interface{}, autoAdd bool) (results []i
818
824
}
819
825
parsedSchemas [dep .Statement .Schema ] = true
820
826
821
- for _ , rel := range dep .Schema .Relationships .Relations {
822
- if c := rel .ParseConstraint (); c != nil && c .Schema == dep .Statement .Schema && c .Schema != c .ReferenceSchema {
823
- dep .Depends = append (dep .Depends , c .ReferenceSchema )
824
- }
827
+ if ! m .DB .IgnoreRelationshipsWhenMigrating {
828
+ for _ , rel := range dep .Schema .Relationships .Relations {
829
+ if rel .Field .IgnoreMigration {
830
+ continue
831
+ }
832
+ if c := rel .ParseConstraint (); c != nil && c .Schema == dep .Statement .Schema && c .Schema != c .ReferenceSchema {
833
+ dep .Depends = append (dep .Depends , c .ReferenceSchema )
834
+ }
825
835
826
- if rel .Type == schema .HasOne || rel .Type == schema .HasMany {
827
- beDependedOn [rel .FieldSchema ] = true
828
- }
836
+ if rel .Type == schema .HasOne || rel .Type == schema .HasMany {
837
+ beDependedOn [rel .FieldSchema ] = true
838
+ }
829
839
830
- if rel .JoinTable != nil {
831
- // append join value
832
- defer func (rel * schema.Relationship , joinValue interface {}) {
833
- if ! beDependedOn [rel .FieldSchema ] {
834
- dep .Depends = append (dep .Depends , rel .FieldSchema )
835
- } else {
836
- fieldValue := reflect .New (rel .FieldSchema .ModelType ).Interface ()
837
- parseDependence (fieldValue , autoAdd )
838
- }
839
- parseDependence (joinValue , autoAdd )
840
- }(rel , reflect .New (rel .JoinTable .ModelType ).Interface ())
840
+ if rel .JoinTable != nil {
841
+ // append join value
842
+ defer func (rel * schema.Relationship , joinValue interface {}) {
843
+ if ! beDependedOn [rel .FieldSchema ] {
844
+ dep .Depends = append (dep .Depends , rel .FieldSchema )
845
+ } else {
846
+ fieldValue := reflect .New (rel .FieldSchema .ModelType ).Interface ()
847
+ parseDependence (fieldValue , autoAdd )
848
+ }
849
+ parseDependence (joinValue , autoAdd )
850
+ }(rel , reflect .New (rel .JoinTable .ModelType ).Interface ())
851
+ }
841
852
}
842
853
}
843
854
0 commit comments