Skip to content

Commit 7078a2e

Browse files
authored
orm: fix codegen for option fk (fix #23383) (#23400)
1 parent 124927b commit 7078a2e

3 files changed

Lines changed: 55 additions & 1 deletion

File tree

‎cmd/tools/vtest-self.v‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ const skip_with_fsanitize_memory = [
167167
'vlib/orm/orm_option_time_test.v',
168168
'vlib/orm/orm_order_by_custom_field_test.v',
169169
'vlib/orm/orm_serial_attribute_test.v',
170+
'vlib/orm/orm_option_subselect_test.v',
170171
'vlib/db/sqlite/sqlite_test.v',
171172
'vlib/db/sqlite/sqlite_orm_test.v',
172173
'vlib/db/sqlite/sqlite_comptime_field_test.v',
@@ -272,6 +273,7 @@ const skip_on_ubuntu_musl = [
272273
'vlib/orm/orm_option_time_test.v',
273274
'vlib/orm/orm_order_by_custom_field_test.v',
274275
'vlib/orm/orm_serial_attribute_test.v',
276+
'vlib/orm/orm_option_subselect_test.v',
275277
'vlib/v/tests/orm_enum_test.v',
276278
'vlib/v/tests/orm_sub_struct_test.v',
277279
'vlib/v/tests/orm_sub_array_struct_test.v',
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import db.sqlite
2+
3+
fn test_main() {
4+
db := sqlite.connect(':memory:')!
5+
6+
sql db {
7+
create table Commit
8+
create table Measurement
9+
}!
10+
11+
c := Commit{
12+
commit_hash: 'hash'
13+
}
14+
sql db {
15+
insert c into Commit
16+
}!
17+
18+
c2 := sql db {
19+
select from Commit
20+
}!
21+
assert c2[0].v_self_default == none
22+
23+
c3 := Commit{
24+
commit_hash: 'hash1'
25+
v_self_default: Measurement{
26+
id: 123
27+
}
28+
}
29+
sql db {
30+
insert c3 into Commit
31+
}!
32+
33+
c4 := sql db {
34+
select from Commit
35+
}!
36+
assert c4[0].v_self_default == none
37+
assert c4[1].v_self_default != none
38+
}
39+
40+
@[table: 'commits']
41+
struct Commit {
42+
commit_hash string @[primary]
43+
v_self_default ?Measurement
44+
}
45+
46+
@[table: 'measurements']
47+
struct Measurement {
48+
id int @[primary; serial]
49+
}

‎vlib/v/gen/c/orm.v‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1071,7 +1071,6 @@ fn (mut g Gen) write_orm_select(node ast.SqlExpr, connection_var_name string, re
10711071
sub_result_c_typ := g.styp(sub.typ)
10721072
g.writeln('${sub_result_c_typ} ${sub_result_var};')
10731073
g.write_orm_select(sub, connection_var_name, sub_result_var)
1074-
10751074
if field.typ.has_flag(.option) {
10761075
unwrapped_field_c_typ := g.styp(field.typ.clear_flag(.option))
10771076
g.writeln('if (!${sub_result_var}.is_error)')
@@ -1172,6 +1171,10 @@ fn (mut g Gen) write_orm_select(node ast.SqlExpr, connection_var_name string, re
11721171
}
11731172

11741173
g.indent--
1174+
if !node.is_array {
1175+
g.writeln('} else {')
1176+
g.writeln('\t${result_var}.is_error = true;')
1177+
}
11751178
g.writeln('}')
11761179

11771180
if node.is_array {

0 commit comments

Comments
 (0)