Skip to content

Commit

Permalink
orm: fix orm insert issue if table missing [Issue : #20017] (#20580)
Browse files Browse the repository at this point in the history
  • Loading branch information
GGRei committed Feb 10, 2024
1 parent 16574df commit 2d0ed2c
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
48 changes: 48 additions & 0 deletions vlib/orm/orm_insert_test.v
Expand Up @@ -348,6 +348,54 @@ fn test_orm_insert_with_multiple_child_elements() {
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']
struct Customer {
id i64 @[primary; sql: serial]
Expand Down
4 changes: 4 additions & 0 deletions vlib/v/gen/c/orm.v
Expand Up @@ -420,6 +420,8 @@ fn (mut g Gen) write_orm_insert_with_last_ids(node ast.SqlStmtLine, connection_v
g.writeln('}')
g.indent--
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 {
mut id_name := g.new_tmp_var()
Expand Down Expand Up @@ -473,6 +475,8 @@ fn (mut g Gen) write_orm_insert_with_last_ids(node ast.SqlStmtLine, connection_v
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),
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.writeln('}')
}
Expand Down

0 comments on commit 2d0ed2c

Please sign in to comment.