codegen: handle nullable foreign keys using 'ifnull' and add a test case for them
All checks were successful
CI / build-docker (push) Successful in 18s
CI / build-docker-bootstrap (push) Has been skipped
CI / release-test (push) Successful in 1m2s

This commit is contained in:
wispem-wantex
2026-01-26 16:00:19 -08:00
parent e85a68e69d
commit a0d0461f06
2 changed files with 9 additions and 2 deletions

View File

@@ -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, ", ") + "`"