@@ -50,10 +50,10 @@ type Table struct {
5050}
5151
5252type Column struct {
53- Name string
54- Nullable bool
55- PrimaryKey bool
56- // Size int
53+ Name string
54+ Nullable bool
55+ PrimaryKey bool
56+ Size int
5757 Scale int
5858 Precision int
5959 Type string
@@ -120,6 +120,7 @@ func FromStatement(stmt *gorm.Statement) Table {
120120 }
121121
122122 var datatype = stmt .Dialector .DataTypeOf (field )
123+
123124 if strings .Contains (datatype , "enum" ) {
124125 datatype = cleanEnum (datatype )
125126 } else {
@@ -138,6 +139,7 @@ func FromStatement(stmt *gorm.Statement) Table {
138139 var column = Column {
139140 Name : field .DBName ,
140141 Type : datatype ,
142+ Size : field .Size ,
141143 Scale : field .Scale ,
142144 Precision : field .Precision ,
143145 Default : field .DefaultValue ,
@@ -147,6 +149,24 @@ func FromStatement(stmt *gorm.Statement) Table {
147149 Unique : field .Unique ,
148150 }
149151
152+ // fix gorm problem with primaryKey
153+ if (column .Type == "bigint(20)" || column .Type == "bigint" || column .Type == "int" ) && field .FieldType .Kind () == reflect .String {
154+ column .Type = "varchar"
155+ }
156+ if column .Type == "varchar" {
157+ if column .Size == 0 {
158+ column .Size = 255 // default size for VARCHAR is 255, but GORM requires a size > 0 for string fields.
159+ }
160+ column .Type = "varchar(" + strconv .Itoa (column .Size ) + ")"
161+ }
162+
163+ if column .Type == "char" {
164+ if column .Size == 0 {
165+ column .Size = 255 // default size for VARCHAR is 255, but GORM requires a size > 0 for string fields.
166+ }
167+ column .Type = "char(" + strconv .Itoa (column .Size ) + ")"
168+ }
169+
150170 if v , ok := field .TagSettings ["CHARSET" ]; ok {
151171 column .Charset = v
152172 }
0 commit comments