diff --git a/schema/field.go b/schema/field.go index 9d3b3033..88a0d3fb 100644 --- a/schema/field.go +++ b/schema/field.go @@ -177,7 +177,7 @@ func (schema *Schema) ParseField(fieldStruct reflect.StructField) *Field { } if _, ok := field.TagSettings["EMBEDDED"]; ok || fieldStruct.Anonymous { - field.EmbeddedbSchema = Parse(fieldValue, sync.Map{}) + field.EmbeddedbSchema = Parse(fieldValue, sync.Map{}, schema.namer) for _, ef := range field.EmbeddedbSchema.Fields { ef.BindNames = append([]string{fieldStruct.Name}, ef.BindNames...) diff --git a/schema/schema.go b/schema/schema.go index 6d85af8c..5069bb44 100644 --- a/schema/schema.go +++ b/schema/schema.go @@ -16,10 +16,11 @@ type Schema struct { FieldsByName map[string]*Field FieldsByDBName map[string]*Field Relationships Relationships + namer Namer } // get data type from dialector -func Parse(dest interface{}, cacheStore sync.Map) *Schema { +func Parse(dest interface{}, cacheStore sync.Map, namer Namer) *Schema { modelType := reflect.ValueOf(dest).Type() for modelType.Kind() == reflect.Slice || modelType.Kind() == reflect.Ptr { modelType = modelType.Elem() @@ -35,6 +36,7 @@ func Parse(dest interface{}, cacheStore sync.Map) *Schema { schema := &Schema{ ModelType: modelType, + Table: namer.TableName(modelType.Name()), FieldsByName: map[string]*Field{}, FieldsByDBName: map[string]*Field{}, } @@ -45,14 +47,23 @@ func Parse(dest interface{}, cacheStore sync.Map) *Schema { continue } - schema.Fields = append(schema.Fields, schema.ParseField(fieldStruct)) - // db namer + field := schema.ParseField(fieldStruct) + schema.Fields = append(schema.Fields, field) + if field.EmbeddedbSchema != nil { + for _, f := range field.EmbeddedbSchema.Fields { + schema.Fields = append(schema.Fields, f) + } + } } for _, field := range schema.Fields { + if field.DBName == "" { + field.DBName = namer.ColumnName(field.Name) + } + if field.DBName != "" { - // nonexistence or shortest path or first appear prioritized - if v, ok := schema.FieldsByDBName[field.DBName]; !ok || len(field.BindNames) < len(v.BindNames) { + // nonexistence or shortest path or first appear prioritized if has permission + if v, ok := schema.FieldsByDBName[field.DBName]; !ok || (field.Creatable && len(field.BindNames) < len(v.BindNames)) { schema.FieldsByDBName[field.DBName] = field schema.FieldsByName[field.Name] = field }