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

@ -31,9 +31,12 @@ cd $test_project
# Create a new table in the schema # Create a new table in the schema
cat >> pkg/db/schema.sql <<EOF cat >> pkg/db/schema.sql <<EOF
create table item_flavor (rowid integer primary key, name text not null) strict;
create table items ( create table items (
rowid integer primary key, rowid integer primary key,
description text not null default '' description text not null default '',
flavor integer references item_flavor(rowid)
) strict; ) strict;
EOF EOF

View File

@ -351,8 +351,12 @@ func GenerateDeleteItemFunc(tbl schema.Table) *ast.FuncDecl {
func GenerateSQLFieldsConst(tbl schema.Table) *ast.GenDecl { func GenerateSQLFieldsConst(tbl schema.Table) *ast.GenDecl {
columns := make([]string, 0, len(tbl.Columns)) columns := make([]string, 0, len(tbl.Columns))
for _, col := range tbl.Columns { for _, col := range tbl.Columns {
if col.IsNullableForeignKey() {
columns = append(columns, fmt.Sprintf("ifnull(%s, 0) %s", col.Name, col.Name))
} else {
columns = append(columns, col.Name) columns = append(columns, col.Name)
} }
}
// Join with comma and space // Join with comma and space
value := "`" + strings.Join(columns, ", ") + "`" value := "`" + strings.Join(columns, ", ") + "`"