Test same field name from embedded field, close

This commit is contained in:
Jinzhu 2020-08-20 10:58:35 +08:00
parent 0c9870d1ae
commit 06de6e8834
2 changed files with 14 additions and 5 deletions

View File

@ -57,7 +57,7 @@ func checkSchemaField(t *testing.T, s *schema.Schema, f *schema.Field, fc func(*
if !ok { if !ok {
t.Errorf("schema %v failed to look up field with name %v", s, f.Name) t.Errorf("schema %v failed to look up field with name %v", s, f.Name)
} else { } else {
tests.AssertObjEqual(t, parsedField, f, "Name", "DBName", "BindNames", "DataType", "PrimaryKey", "AutoIncrement", "Creatable", "Updatable", "Readable", "HasDefaultValue", "DefaultValue", "NotNull", "Unique", "Comment", "Size", "Precision", "Tag", "TagSettings") tests.AssertObjEqual(t, parsedField, f, "Name", "DBName", "BindNames", "DataType", "PrimaryKey", "AutoIncrement", "Creatable", "Updatable", "Readable", "HasDefaultValue", "DefaultValue", "NotNull", "Unique", "Comment", "Size", "Precision", "TagSettings")
if f.DBName != "" { if f.DBName != "" {
if field, ok := s.FieldsByDBName[f.DBName]; !ok || parsedField != field { if field, ok := s.FieldsByDBName[f.DBName]; !ok || parsedField != field {

View File

@ -4,6 +4,7 @@ import (
"sync" "sync"
"testing" "testing"
"gorm.io/gorm"
"gorm.io/gorm/schema" "gorm.io/gorm/schema"
"gorm.io/gorm/utils/tests" "gorm.io/gorm/utils/tests"
) )
@ -184,13 +185,19 @@ func TestNestedModel(t *testing.T) {
} }
func TestEmbeddedStruct(t *testing.T) { func TestEmbeddedStruct(t *testing.T) {
type CorpBase struct {
gorm.Model
OwnerID string
}
type Company struct { type Company struct {
ID int ID int
Name string OwnerID int
Name string
} }
type Corp struct { type Corp struct {
ID uint CorpBase
Base Company `gorm:"embedded;embeddedPrefix:company_"` Base Company `gorm:"embedded;embeddedPrefix:company_"`
} }
@ -201,9 +208,11 @@ func TestEmbeddedStruct(t *testing.T) {
} }
fields := []schema.Field{ fields := []schema.Field{
{Name: "ID", DBName: "id", BindNames: []string{"ID"}, DataType: schema.Uint, PrimaryKey: true, Size: 64, HasDefaultValue: true, AutoIncrement: true}, {Name: "ID", DBName: "id", BindNames: []string{"CorpBase", "Model", "ID"}, DataType: schema.Uint, PrimaryKey: true, Size: 64, HasDefaultValue: true, AutoIncrement: true, TagSettings: map[string]string{"PRIMARYKEY": "PRIMARYKEY"}},
{Name: "ID", DBName: "company_id", BindNames: []string{"Base", "ID"}, DataType: schema.Int, Size: 64, TagSettings: map[string]string{"EMBEDDED": "EMBEDDED", "EMBEDDEDPREFIX": "company_"}}, {Name: "ID", DBName: "company_id", BindNames: []string{"Base", "ID"}, DataType: schema.Int, Size: 64, TagSettings: map[string]string{"EMBEDDED": "EMBEDDED", "EMBEDDEDPREFIX": "company_"}},
{Name: "Name", DBName: "company_name", BindNames: []string{"Base", "Name"}, DataType: schema.String, TagSettings: map[string]string{"EMBEDDED": "EMBEDDED", "EMBEDDEDPREFIX": "company_"}}, {Name: "Name", DBName: "company_name", BindNames: []string{"Base", "Name"}, DataType: schema.String, TagSettings: map[string]string{"EMBEDDED": "EMBEDDED", "EMBEDDEDPREFIX": "company_"}},
{Name: "OwnerID", DBName: "company_owner_id", BindNames: []string{"Base", "OwnerID"}, DataType: schema.Int, Size: 64, TagSettings: map[string]string{"EMBEDDED": "EMBEDDED", "EMBEDDEDPREFIX": "company_"}},
{Name: "OwnerID", DBName: "owner_id", BindNames: []string{"CorpBase", "OwnerID"}, DataType: schema.String},
} }
for _, f := range fields { for _, f := range fields {