@@ -152,6 +152,10 @@ func getForeignField(column string, fields []*StructField) *StructField {
152
152
153
153
// GetModelStruct get value's model struct, relationships based on struct and tag definition
154
154
func (scope * Scope ) GetModelStruct () * ModelStruct {
155
+ return scope .getModelStruct (scope , make ([]* StructField , 0 ))
156
+ }
157
+
158
+ func (scope * Scope ) getModelStruct (rootScope * Scope , allFields []* StructField ) * ModelStruct {
155
159
var modelStruct ModelStruct
156
160
// Scope value can't be nil
157
161
if scope .Value == nil {
@@ -237,7 +241,7 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
237
241
field .IsNormal = true
238
242
} else if _ , ok := field .TagSettingsGet ("EMBEDDED" ); ok || fieldStruct .Anonymous {
239
243
// is embedded struct
240
- for _ , subField := range scope .New (fieldValue ).GetModelStruct ( ).StructFields {
244
+ for _ , subField := range scope .New (fieldValue ).getModelStruct ( rootScope , allFields ).StructFields {
241
245
subField = subField .clone ()
242
246
subField .Names = append ([]string {fieldStruct .Name }, subField .Names ... )
243
247
if prefix , ok := field .TagSettingsGet ("EMBEDDED_PREFIX" ); ok {
@@ -261,6 +265,7 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
261
265
}
262
266
263
267
modelStruct .StructFields = append (modelStruct .StructFields , subField )
268
+ allFields = append (allFields , subField )
264
269
}
265
270
continue
266
271
} else {
@@ -394,7 +399,7 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
394
399
} else {
395
400
// generate foreign keys from defined association foreign keys
396
401
for _ , scopeFieldName := range associationForeignKeys {
397
- if foreignField := getForeignField (scopeFieldName , modelStruct . StructFields ); foreignField != nil {
402
+ if foreignField := getForeignField (scopeFieldName , allFields ); foreignField != nil {
398
403
foreignKeys = append (foreignKeys , associationType + foreignField .Name )
399
404
associationForeignKeys = append (associationForeignKeys , foreignField .Name )
400
405
}
@@ -406,13 +411,13 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
406
411
for _ , foreignKey := range foreignKeys {
407
412
if strings .HasPrefix (foreignKey , associationType ) {
408
413
associationForeignKey := strings .TrimPrefix (foreignKey , associationType )
409
- if foreignField := getForeignField (associationForeignKey , modelStruct . StructFields ); foreignField != nil {
414
+ if foreignField := getForeignField (associationForeignKey , allFields ); foreignField != nil {
410
415
associationForeignKeys = append (associationForeignKeys , associationForeignKey )
411
416
}
412
417
}
413
418
}
414
419
if len (associationForeignKeys ) == 0 && len (foreignKeys ) == 1 {
415
- associationForeignKeys = []string {scope .PrimaryKey ()}
420
+ associationForeignKeys = []string {rootScope .PrimaryKey ()}
416
421
}
417
422
} else if len (foreignKeys ) != len (associationForeignKeys ) {
418
423
scope .Err (errors .New ("invalid foreign keys, should have same length" ))
@@ -422,7 +427,7 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
422
427
423
428
for idx , foreignKey := range foreignKeys {
424
429
if foreignField := getForeignField (foreignKey , toFields ); foreignField != nil {
425
- if associationField := getForeignField (associationForeignKeys [idx ], modelStruct . StructFields ); associationField != nil {
430
+ if associationField := getForeignField (associationForeignKeys [idx ], allFields ); associationField != nil {
426
431
// mark field as foreignkey, use global lock to avoid race
427
432
structsLock .Lock ()
428
433
foreignField .IsForeignKey = true
@@ -502,7 +507,7 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
502
507
} else {
503
508
// generate foreign keys form association foreign keys
504
509
for _ , associationForeignKey := range tagAssociationForeignKeys {
505
- if foreignField := getForeignField (associationForeignKey , modelStruct . StructFields ); foreignField != nil {
510
+ if foreignField := getForeignField (associationForeignKey , allFields ); foreignField != nil {
506
511
foreignKeys = append (foreignKeys , associationType + foreignField .Name )
507
512
associationForeignKeys = append (associationForeignKeys , foreignField .Name )
508
513
}
@@ -514,13 +519,13 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
514
519
for _ , foreignKey := range foreignKeys {
515
520
if strings .HasPrefix (foreignKey , associationType ) {
516
521
associationForeignKey := strings .TrimPrefix (foreignKey , associationType )
517
- if foreignField := getForeignField (associationForeignKey , modelStruct . StructFields ); foreignField != nil {
522
+ if foreignField := getForeignField (associationForeignKey , allFields ); foreignField != nil {
518
523
associationForeignKeys = append (associationForeignKeys , associationForeignKey )
519
524
}
520
525
}
521
526
}
522
527
if len (associationForeignKeys ) == 0 && len (foreignKeys ) == 1 {
523
- associationForeignKeys = []string {scope .PrimaryKey ()}
528
+ associationForeignKeys = []string {rootScope .PrimaryKey ()}
524
529
}
525
530
} else if len (foreignKeys ) != len (associationForeignKeys ) {
526
531
scope .Err (errors .New ("invalid foreign keys, should have same length" ))
@@ -530,7 +535,7 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
530
535
531
536
for idx , foreignKey := range foreignKeys {
532
537
if foreignField := getForeignField (foreignKey , toFields ); foreignField != nil {
533
- if scopeField := getForeignField (associationForeignKeys [idx ], modelStruct . StructFields ); scopeField != nil {
538
+ if scopeField := getForeignField (associationForeignKeys [idx ], allFields ); scopeField != nil {
534
539
// mark field as foreignkey, use global lock to avoid race
535
540
structsLock .Lock ()
536
541
foreignField .IsForeignKey = true
@@ -630,6 +635,7 @@ func (scope *Scope) GetModelStruct() *ModelStruct {
630
635
}
631
636
632
637
modelStruct .StructFields = append (modelStruct .StructFields , field )
638
+ allFields = append (allFields , field )
633
639
}
634
640
}
635
641
0 commit comments