diff --git a/internal/model/base.go b/internal/model/base.go index 6b4062a9..a9ca6a79 100644 --- a/internal/model/base.go +++ b/internal/model/base.go @@ -123,7 +123,7 @@ type dataTypeMapping func(detailType string) (finalType string) type dataTypeMap map[string]dataTypeMapping func (m dataTypeMap) Get(dataType, detailType string) string { - if convert, ok := m[dataType]; ok { + if convert, ok := m[strings.ToLower(dataType)]; ok { return convert(detailType) } return defaultDataType diff --git a/internal/model/conf.go b/internal/model/conf.go index c3c1e3a7..d0f7b653 100644 --- a/internal/model/conf.go +++ b/internal/model/conf.go @@ -12,7 +12,7 @@ type FieldConf struct { FieldCoverable bool // generate pointer when field has default value FieldSignable bool // detect integer field's unsigned type, adjust generated data type FieldWithIndexTag bool // generate with gorm index tag - FieldWithTypeTag bool // generate with gorm column type tagl + FieldWithTypeTag bool // generate with gorm column type tag FieldJSONTagNS func(columnName string) string FieldNewTagNS func(columnName string) string diff --git a/internal/model/tbl_column.go b/internal/model/tbl_column.go index 7a25a557..d358779a 100644 --- a/internal/model/tbl_column.go +++ b/internal/model/tbl_column.go @@ -55,7 +55,7 @@ func (c *Column) ToField(nullable, coverable, signable bool) *Field { if n, ok := c.Nullable(); ok && n { fieldType = "*" + fieldType } - case coverable && c.withDefaultValue(): + case coverable && c.needDefaultTag(c.defaultTagValue()): fieldType = "*" + fieldType } @@ -84,7 +84,10 @@ func (c *Column) multilineComment() bool { func (c *Column) buildGormTag() string { var buf bytes.Buffer buf.WriteString(fmt.Sprintf("column:%s;type:%s", c.Name(), c.columnType())) - if p, ok := c.PrimaryKey(); ok && p { + + isPriKey, ok := c.PrimaryKey() + isValidPriKey := ok && isPriKey + if isValidPriKey { buf.WriteString(";primaryKey") if at, ok := c.AutoIncrement(); ok { buf.WriteString(fmt.Sprintf(";autoIncrement:%t", at)) @@ -103,27 +106,29 @@ func (c *Column) buildGormTag() string { buf.WriteString(fmt.Sprintf(";index:%s,priority:%d", idx.IndexName, idx.SeqInIndex)) } } - if c.withDefaultValue() { - buf.WriteString(fmt.Sprintf(";default:%s", c.defaultValue())) + + if dtValue := c.defaultTagValue(); !isValidPriKey && c.needDefaultTag(dtValue) { // cannot set default tag for primary key + buf.WriteString(fmt.Sprintf(";default:%s", dtValue)) } return buf.String() } -// withDefaultValue check if col has default value and not created_at or updated_at -func (c *Column) withDefaultValue() (normal bool) { - return c.defaultValue() != "" && c.defaultValue() != "0" && +// needDefaultTag check if default tag needed +func (c *Column) needDefaultTag(defaultTagValue string) bool { + return defaultTagValue != "" && defaultTagValue != "0" && c.Name() != "created_at" && c.Name() != "updated_at" } -func (c *Column) defaultValue() string { - df, ok := c.DefaultValue() +// defaultTagValue return gorm default tag's value +func (c *Column) defaultTagValue() string { + value, ok := c.DefaultValue() if !ok { return "" } - if typ := c.DatabaseTypeName(); strings.Contains(typ, "int") || typ == "numeric" || strings.Contains(typ, "float") { - return df + if strings.Contains(value, " ") { + return "'" + value + "'" } - return "'" + df + "'" + return value } func (c *Column) columnType() (v string) {