codegen: handle nullable foreign keys using 'ifnull' and add a test case for them
This commit is contained in:
@@ -351,7 +351,11 @@ func GenerateDeleteItemFunc(tbl schema.Table) *ast.FuncDecl {
|
||||
func GenerateSQLFieldsConst(tbl schema.Table) *ast.GenDecl {
|
||||
columns := make([]string, 0, len(tbl.Columns))
|
||||
for _, col := range tbl.Columns {
|
||||
columns = append(columns, col.Name)
|
||||
if col.IsNullableForeignKey() {
|
||||
columns = append(columns, fmt.Sprintf("ifnull(%s, 0) %s", col.Name, col.Name))
|
||||
} else {
|
||||
columns = append(columns, col.Name)
|
||||
}
|
||||
}
|
||||
// Join with comma and space
|
||||
value := "`" + strings.Join(columns, ", ") + "`"
|
||||
|
||||
Reference in New Issue
Block a user