Skip to content

Commit

Permalink
orm: fix crash when working with array field (fix #22822) (#22824)
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp authored Nov 13, 2024
1 parent 1a9dab2 commit ebb3a8e
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
2 changes: 2 additions & 0 deletions cmd/tools/vtest-self.v
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand Down
4 changes: 3 additions & 1 deletion vlib/v/gen/c/orm.v
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
35 changes: 35 additions & 0 deletions vlib/v/tests/orm_array_field_test.v
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit ebb3a8e

Please sign in to comment.