Skip to content

Commit ebeef84

Browse files
authored
orm: fix update stmt with enum value (fix #23031) (#23037)
1 parent 31ce668 commit ebeef84

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

cmd/tools/vtest-self.v

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ const skip_with_fsanitize_memory = [
181181
'vlib/v/tests/orm_array_field_test.v',
182182
'vlib/v/tests/orm_handle_error_for_select_from_not_created_table_test.v',
183183
'vlib/v/tests/orm_create_several_tables_test.v',
184+
'vlib/v/tests/orm_update_test.v',
184185
'vlib/vweb/tests/vweb_test.v',
185186
'vlib/vweb/csrf/csrf_test.v',
186187
'vlib/net/http/request_test.v',
@@ -204,6 +205,7 @@ const skip_with_fsanitize_address = [
204205
'vlib/v/tests/orm_sub_array_struct_test.v',
205206
'vlib/v/tests/orm_handle_error_for_select_from_not_created_table_test.v',
206207
'vlib/v/tests/orm_create_several_tables_test.v',
208+
'vlib/v/tests/orm_update_test.v',
207209
]
208210
const skip_with_fsanitize_undefined = [
209211
'do_not_remove',
@@ -215,6 +217,7 @@ const skip_with_fsanitize_undefined = [
215217
'vlib/v/tests/orm_sub_array_struct_test.v',
216218
'vlib/v/tests/orm_handle_error_for_select_from_not_created_table_test.v',
217219
'vlib/v/tests/orm_create_several_tables_test.v',
220+
'vlib/v/tests/orm_update_test.v',
218221
'vlib/v/tests/project_with_cpp_code/compiling_cpp_files_with_a_cplusplus_compiler_test.c.v', // fails compilation with: undefined reference to vtable for __cxxabiv1::__function_type_info'
219222
]
220223
const skip_with_werror = [
@@ -274,6 +277,7 @@ const skip_on_ubuntu_musl = [
274277
'vlib/v/tests/orm_array_field_test.v',
275278
'vlib/v/tests/orm_handle_error_for_select_from_not_created_table_test.v',
276279
'vlib/v/tests/orm_create_several_tables_test.v',
280+
'vlib/v/tests/orm_update_test.v',
277281
'vlib/v/tests/sql_statement_inside_fn_call_test.v',
278282
'vlib/clipboard/clipboard_test.v',
279283
'vlib/vweb/tests/vweb_test.v',

vlib/v/gen/c/orm.v

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ fn (mut g Gen) write_orm_insert_with_last_ids(node ast.SqlStmtLine, connection_v
413413
ctyp = 'time__Time'
414414
typ = 'time'
415415
} else if sym.kind == .enum {
416-
typ = 'i64'
416+
typ = g.table.sym(g.table.final_type(field.typ)).cname
417417
}
418418
var := '${node.object_var}${member_access_type}${c_name(field.name)}'
419419
if field.typ.has_flag(.option) {
@@ -604,6 +604,8 @@ fn (mut g Gen) write_orm_primitive(t ast.Type, expr ast.Expr) {
604604

605605
if t.has_flag(.option) {
606606
typ = 'option_${typ}'
607+
} else if g.table.final_sym(t).kind == .enum {
608+
typ = g.table.sym(g.table.final_type(t)).cname
607609
}
608610
g.write('orm__${typ}_to_primitive(')
609611
if expr is ast.CallExpr {

vlib/v/tests/orm_update_test.v

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import db.sqlite
2+
3+
struct Person {
4+
name string
5+
height Height
6+
}
7+
8+
enum Height as u8 {
9+
tall
10+
small
11+
}
12+
13+
fn test_main() {
14+
db := sqlite.connect(':memory:')!
15+
16+
sql db {
17+
create table Person
18+
}!
19+
20+
a := Person{'A', Height.small}
21+
b := Person{'A', Height.tall}
22+
23+
sql db {
24+
insert a into Person
25+
}!
26+
27+
sql db {
28+
insert b into Person
29+
}!
30+
31+
new_height := Height.small
32+
sql db {
33+
update Person set height = new_height where height == Height.tall
34+
}!
35+
36+
rows := sql db {
37+
select from Person where height == Height.small
38+
}!
39+
40+
assert rows.len == 2
41+
}

0 commit comments

Comments
 (0)