Skip to content

Commit ebb3a8e

Browse files
authored
orm: fix crash when working with array field (fix #22822) (#22824)
1 parent 1a9dab2 commit ebb3a8e

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

cmd/tools/vtest-self.v

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ const skip_with_fsanitize_memory = [
179179
'vlib/v/tests/sql_statement_inside_fn_call_test.v',
180180
'vlib/v/tests/orm_stmt_wrong_return_checking_test.v',
181181
'vlib/v/tests/orm_table_name_test.v',
182+
'vlib/v/tests/orm_array_field_test.v',
182183
'vlib/v/tests/orm_handle_error_for_select_from_not_created_table_test.v',
183184
'vlib/v/tests/orm_create_several_tables_test.v',
184185
'vlib/vweb/tests/vweb_test.v',
@@ -271,6 +272,7 @@ const skip_on_ubuntu_musl = [
271272
'vlib/v/tests/orm_joined_tables_select_test.v',
272273
'vlib/v/tests/orm_stmt_wrong_return_checking_test.v',
273274
'vlib/v/tests/orm_table_name_test.v',
275+
'vlib/v/tests/orm_array_field_test.v',
274276
'vlib/v/tests/orm_handle_error_for_select_from_not_created_table_test.v',
275277
'vlib/v/tests/orm_create_several_tables_test.v',
276278
'vlib/v/tests/sql_statement_inside_fn_call_test.v',

vlib/v/gen/c/orm.v

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,9 @@ fn (mut g Gen) write_orm_insert_with_last_ids(node ast.SqlStmtLine, connection_v
319319
if field.typ.has_flag(.option) {
320320
opt_fields << arrs.len
321321
}
322-
arrs << unsafe { node.sub_structs[int(field.typ)] }
322+
if node.sub_structs.len > 0 {
323+
arrs << unsafe { node.sub_structs[int(field.typ)] }
324+
}
323325
field_names << field.name
324326
}
325327
}

vlib/v/tests/orm_array_field_test.v

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import time
2+
import db.sqlite
3+
4+
@[table: 'task_metadata']
5+
struct TaskMetadata {
6+
id string @[primary]
7+
task_id string
8+
key string
9+
value string
10+
created_at time.Time @[default: 'CURRENT_TIME']
11+
updated_at time.Time @[default: 'CURRENT_TIME']
12+
}
13+
14+
@[table: 'tasks']
15+
struct Task {
16+
id string @[primary]
17+
name string
18+
metadata []TaskMetadata @[fkey: 'task_id']
19+
}
20+
21+
struct MyService {
22+
mut:
23+
db sqlite.DB
24+
}
25+
26+
pub fn (s MyService) create(record Task) int {
27+
result := sql s.db {
28+
insert record into Task
29+
} or { return -1 }
30+
return result
31+
}
32+
33+
fn test_main() {
34+
assert true
35+
}

0 commit comments

Comments
 (0)