Skip to content

Commit

Permalink
fix gorm primary key string types
Browse files Browse the repository at this point in the history
  • Loading branch information
iesreza committed Dec 22, 2024
1 parent 2c13460 commit 3a4f122
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ require (
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/rogpeppe/go-internal v1.10.0 // indirect
github.com/shopspring/decimal v1.4.0 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.1 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,8 @@ github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncj
github.com/scylladb/termtables v0.0.0-20191203121021-c4c0b6d42ff4/go.mod h1:C1a7PQSMz9NShzorzCiG2fk9+xuCgLkPeCvMHYR2OWg=
github.com/segmentio/kafka-go v0.4.39 h1:75smaomhvkYRwtuOwqLsdhgCG30B82NsbdkdDfFbvrw=
github.com/segmentio/kafka-go v0.4.39/go.mod h1:T0MLgygYvmqmBvC+s8aCcbVNfJN4znVne5j0Pzowp/Q=
github.com/shopspring/decimal v1.4.0 h1:bxl37RwXBklmTi0C79JfXCEBD1cqqHt0bbgBAGFp81k=
github.com/shopspring/decimal v1.4.0/go.mod h1:gawqmDU56v4yIKSwfBSFip1HdCCXN8/+DMd9qYNcwME=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
Expand Down
28 changes: 24 additions & 4 deletions lib/db/schema/ddl/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ type Table struct {
}

type Column struct {
Name string
Nullable bool
PrimaryKey bool
//Size int
Name string
Nullable bool
PrimaryKey bool
Size int
Scale int
Precision int
Type string
Expand Down Expand Up @@ -120,6 +120,7 @@ func FromStatement(stmt *gorm.Statement) Table {
}

var datatype = stmt.Dialector.DataTypeOf(field)

if strings.Contains(datatype, "enum") {
datatype = cleanEnum(datatype)
} else {
Expand All @@ -138,6 +139,7 @@ func FromStatement(stmt *gorm.Statement) Table {
var column = Column{
Name: field.DBName,
Type: datatype,
Size: field.Size,
Scale: field.Scale,
Precision: field.Precision,
Default: field.DefaultValue,
Expand All @@ -147,6 +149,24 @@ func FromStatement(stmt *gorm.Statement) Table {
Unique: field.Unique,
}

// fix gorm problem with primaryKey
if (column.Type == "bigint(20)" || column.Type == "bigint" || column.Type == "int") && field.FieldType.Kind() == reflect.String {
column.Type = "varchar"
}
if column.Type == "varchar" {
if column.Size == 0 {
column.Size = 255 // default size for VARCHAR is 255, but GORM requires a size > 0 for string fields.
}
column.Type = "varchar(" + strconv.Itoa(column.Size) + ")"
}

if column.Type == "char" {
if column.Size == 0 {
column.Size = 255 // default size for VARCHAR is 255, but GORM requires a size > 0 for string fields.
}
column.Type = "char(" + strconv.Itoa(column.Size) + ")"
}

if v, ok := field.TagSettings["CHARSET"]; ok {
column.Charset = v
}
Expand Down

0 comments on commit 3a4f122

Please sign in to comment.