codegen: handle nullable foreign keys using 'ifnull' and add a test case for them
This commit is contained in:
parent
e85a68e69d
commit
a0d0461f06
@ -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
|
||||||
|
|
||||||
|
|||||||
@ -351,7 +351,11 @@ 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 {
|
||||||
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
|
// Join with comma and space
|
||||||
value := "`" + strings.Join(columns, ", ") + "`"
|
value := "`" + strings.Join(columns, ", ") + "`"
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user