orm: fix orm insert issue if table missing [Issue : #20017] (#20580)

This commit is contained in:
GGRei 2024-02-10 03:52:09 +01:00 committed by GitHub
parent 16574df99b
commit 2d0ed2c1d6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 52 additions and 0 deletions

View File

@ -348,6 +348,54 @@ fn test_orm_insert_with_multiple_child_elements() {
assert parent.notes[2].text == 'Third note' assert parent.notes[2].text == 'Third note'
} }
fn test_orm_insert_with_child_element_and_no_table() {
mut db := sqlite.connect(':memory:')!
sql db {
create table Parent
}!
new_parent := Parent{
name: 'test'
children: [
Child{
name: 'Lisa'
},
]
}
sql db {
insert new_parent into Parent
} or { assert true }
sql db {
create table Child
}!
sql db {
insert new_parent into Parent
} or { assert false }
new_parent_two := Parent{
name: 'retest'
children: [
Child{
name: 'Sophia'
},
]
}
sql db {
insert new_parent_two into Parent
} or { assert false }
p_table := sql db {
select from Parent
}!
assert p_table[2].children[0].name == 'Sophia'
}
@[table: 'customers'] @[table: 'customers']
struct Customer { struct Customer {
id i64 @[primary; sql: serial] id i64 @[primary; sql: serial]

View File

@ -420,6 +420,8 @@ fn (mut g Gen) write_orm_insert_with_last_ids(node ast.SqlStmtLine, connection_v
g.writeln('}') g.writeln('}')
g.indent-- g.indent--
g.writeln(');') g.writeln(');')
// Validate main insertion success otherwise, handled and propagated error.
g.or_block(res, or_expr, ast.int_type.set_flag(.result))
if arrs.len > 0 { if arrs.len > 0 {
mut id_name := g.new_tmp_var() mut id_name := g.new_tmp_var()
@ -473,6 +475,8 @@ fn (mut g Gen) write_orm_insert_with_last_ids(node ast.SqlStmtLine, connection_v
unsafe { fff.free() } unsafe { fff.free() }
g.write_orm_insert_with_last_ids(arr, connection_var_name, g.get_table_name_by_struct_type(arr.table_expr.typ), g.write_orm_insert_with_last_ids(arr, connection_var_name, g.get_table_name_by_struct_type(arr.table_expr.typ),
last_ids, res_, id_name, fkeys[i], or_expr) last_ids, res_, id_name, fkeys[i], or_expr)
// Validates sub insertion success otherwise, handled and propagated error.
g.or_block(res_, or_expr, ast.int_type.set_flag(.result))
g.indent-- g.indent--
g.writeln('}') g.writeln('}')
} }