From ebb3a8eb253921e51610adf3028f83f5eddd934d Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Wed, 13 Nov 2024 04:59:04 -0300 Subject: [PATCH] orm: fix crash when working with array field (fix #22822) (#22824) --- cmd/tools/vtest-self.v | 2 ++ vlib/v/gen/c/orm.v | 4 +++- vlib/v/tests/orm_array_field_test.v | 35 +++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 vlib/v/tests/orm_array_field_test.v diff --git a/cmd/tools/vtest-self.v b/cmd/tools/vtest-self.v index fa0207eeb5..418635a44c 100644 --- a/cmd/tools/vtest-self.v +++ b/cmd/tools/vtest-self.v @@ -179,6 +179,7 @@ const skip_with_fsanitize_memory = [ 'vlib/v/tests/sql_statement_inside_fn_call_test.v', 'vlib/v/tests/orm_stmt_wrong_return_checking_test.v', 'vlib/v/tests/orm_table_name_test.v', + 'vlib/v/tests/orm_array_field_test.v', 'vlib/v/tests/orm_handle_error_for_select_from_not_created_table_test.v', 'vlib/v/tests/orm_create_several_tables_test.v', 'vlib/vweb/tests/vweb_test.v', @@ -271,6 +272,7 @@ const skip_on_ubuntu_musl = [ 'vlib/v/tests/orm_joined_tables_select_test.v', 'vlib/v/tests/orm_stmt_wrong_return_checking_test.v', 'vlib/v/tests/orm_table_name_test.v', + 'vlib/v/tests/orm_array_field_test.v', 'vlib/v/tests/orm_handle_error_for_select_from_not_created_table_test.v', 'vlib/v/tests/orm_create_several_tables_test.v', 'vlib/v/tests/sql_statement_inside_fn_call_test.v', diff --git a/vlib/v/gen/c/orm.v b/vlib/v/gen/c/orm.v index b64eb9403a..a7f5dccc52 100644 --- a/vlib/v/gen/c/orm.v +++ b/vlib/v/gen/c/orm.v @@ -319,7 +319,9 @@ fn (mut g Gen) write_orm_insert_with_last_ids(node ast.SqlStmtLine, connection_v if field.typ.has_flag(.option) { opt_fields << arrs.len } - arrs << unsafe { node.sub_structs[int(field.typ)] } + if node.sub_structs.len > 0 { + arrs << unsafe { node.sub_structs[int(field.typ)] } + } field_names << field.name } } diff --git a/vlib/v/tests/orm_array_field_test.v b/vlib/v/tests/orm_array_field_test.v new file mode 100644 index 0000000000..01c299aafb --- /dev/null +++ b/vlib/v/tests/orm_array_field_test.v @@ -0,0 +1,35 @@ +import time +import db.sqlite + +@[table: 'task_metadata'] +struct TaskMetadata { + id string @[primary] + task_id string + key string + value string + created_at time.Time @[default: 'CURRENT_TIME'] + updated_at time.Time @[default: 'CURRENT_TIME'] +} + +@[table: 'tasks'] +struct Task { + id string @[primary] + name string + metadata []TaskMetadata @[fkey: 'task_id'] +} + +struct MyService { +mut: + db sqlite.DB +} + +pub fn (s MyService) create(record Task) int { + result := sql s.db { + insert record into Task + } or { return -1 } + return result +} + +fn test_main() { + assert true +}