Skip to content

Commit

Permalink
fix: generated codes, default value shouldn't apply for primary key o…
Browse files Browse the repository at this point in the history
…r foreign key constraint
  • Loading branch information
si3nloong committed Sep 22, 2024
1 parent 1317f6a commit 2b655a5
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 5 deletions.
2 changes: 1 addition & 1 deletion codegen/code_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ func (g *Generator) genModels(pkg *packages.Package, dstDir string, typeInferred
g.WriteString(fmt.Sprintf(`%q+ strconv.Itoa((row * noOfColumn) + %d)`, string(g.dialect.VarRune()), i+1))
}
}
g.WriteString(`+")"` + "\n")
g.WriteString(`+")"`)
g.L("}")
}
}
Expand Down
2 changes: 2 additions & 0 deletions codegen/dialect/dialect.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ type GoColumn interface {
GoPath() string
GoType() string
AutoIncr() bool
// Key is to identify whether column is primary or foreign key
Key() bool
// Type() types.Type
Nullable() bool

Expand Down
11 changes: 7 additions & 4 deletions codegen/dialect/postgres/column_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func (s *postgresDriver) ColumnDataTypes() map[string]*dialect.ColumnType {
Scanner: "{{addrOfGoPath}}",
},
"string": {
DataType: s.columnDataType("varchar(255)"),
DataType: s.columnDataType("varchar(255)", ""),
Valuer: "(string)({{goPath}})",
Scanner: "github.com/si3nloong/sqlgen/sequel/types.String({{addrOfGoPath}})",
},
Expand Down Expand Up @@ -90,7 +90,7 @@ func (s *postgresDriver) ColumnDataTypes() map[string]*dialect.ColumnType {
"time.Time": {
DataType: s.columnDataType("timestamp(6) with time zone", sql.RawBytes(`NOW()`)),
Valuer: "(time.Time)({{goPath}})",
Scanner: "github.com/si3nloong/sqlgen/sequel/types.Time({{addrOfGoPath}})",
Scanner: "(*time.Time)({{addrOfGoPath}})",
},
"*time.Time": {
DataType: s.columnDataType("timestamp(6) with time zone"),
Expand Down Expand Up @@ -222,7 +222,9 @@ func (s *postgresDriver) intDataType(dataType string, defaultValue ...any) func(
}
// Auto increment cannot has default value
if !column.AutoIncr() && len(defaultValue) > 0 {
str += " DEFAULT " + format(defaultValue[0])
if !column.Key() {
str += " DEFAULT " + format(defaultValue[0])
}
switch any(defaultValue[0]).(type) {
case uint64:
str += " CHECK (" + s.QuoteIdentifier(column.Name()) + " >= 0)"
Expand All @@ -242,7 +244,8 @@ func (*postgresDriver) columnDataType(dataType string, defaultValue ...any) func
if !column.Nullable() {
str += " NOT NULL"
}
if len(defaultValue) > 0 {
// If it's not primary key or foreign key
if !column.Key() && len(defaultValue) > 0 {
// PRIMARY KEY cannot have default value
str += " DEFAULT " + format(defaultValue[0])
}
Expand Down
8 changes: 8 additions & 0 deletions codegen/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,14 @@ func (c *columnInfo) DataType() string {
return c.mapper.DataType(c)
}

func (c *columnInfo) Key() bool {
_, ok1 := c.getOption(TagOptionPKAlias)
_, ok2 := c.getOption(TagOptionFK)
_, ok3 := c.getOption(TagOptionPK)
_, ok4 := c.getOption(TagOptionAutoIncrement)
return ok1 || ok2 || ok3 || ok4
}

func (c columnInfo) Name() string {
return c.columnName
}
Expand Down

0 comments on commit 2b655a5

Please sign in to comment.