Skip to content

Commit 3a4f122

Browse files
committed
fix gorm primary key string types
1 parent 2c13460 commit 3a4f122

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ require (
7676
github.com/pmezard/go-difflib v1.0.0 // indirect
7777
github.com/rivo/uniseg v0.4.7 // indirect
7878
github.com/rogpeppe/go-internal v1.10.0 // indirect
79+
github.com/shopspring/decimal v1.4.0 // indirect
7980
github.com/tidwall/match v1.1.1 // indirect
8081
github.com/tidwall/pretty v1.2.1 // indirect
8182
github.com/valyala/bytebufferpool v1.0.0 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,8 @@ github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncj
198198
github.com/scylladb/termtables v0.0.0-20191203121021-c4c0b6d42ff4/go.mod h1:C1a7PQSMz9NShzorzCiG2fk9+xuCgLkPeCvMHYR2OWg=
199199
github.com/segmentio/kafka-go v0.4.39 h1:75smaomhvkYRwtuOwqLsdhgCG30B82NsbdkdDfFbvrw=
200200
github.com/segmentio/kafka-go v0.4.39/go.mod h1:T0MLgygYvmqmBvC+s8aCcbVNfJN4znVne5j0Pzowp/Q=
201+
github.com/shopspring/decimal v1.4.0 h1:bxl37RwXBklmTi0C79JfXCEBD1cqqHt0bbgBAGFp81k=
202+
github.com/shopspring/decimal v1.4.0/go.mod h1:gawqmDU56v4yIKSwfBSFip1HdCCXN8/+DMd9qYNcwME=
201203
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
202204
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
203205
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=

lib/db/schema/ddl/ddl.go

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ type Table struct {
5050
}
5151

5252
type 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

Comments
 (0)