orm: fix crash when working with array field (fix #22822) (#22824)

This commit is contained in:
Felipe Pena 2024-11-13 04:59:04 -03:00 committed by GitHub
parent 1a9dab29c6
commit ebb3a8eb25
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 40 additions and 1 deletions

View file

@ -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',

View file

@ -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
}
if node.sub_structs.len > 0 {
arrs << unsafe { node.sub_structs[int(field.typ)] }
}
field_names << field.name
}
}

View file

@ -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
}