Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

orm,cgen: fix @[sql: serial] and @[serial] are not the same #23678

Merged
merged 1 commit into from
Feb 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions vlib/orm/orm_serial_attribute_test.v
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import db.sqlite

struct Child {
id int @[primary; serial]
parent_id int
}

struct PlainNoArg {
id int @[primary; serial; sql: 'custom_id']
name string
id int @[primary; serial; sql: 'custom_id']
name string
children []Child @[fkey: 'parent_id']
}

fn test_plain_no_arg() {
Expand Down Expand Up @@ -33,6 +39,26 @@ fn test_plain_no_arg() {
assert rows[0].name == 'second'
}

fn test_plain_no_arg_with_children() {
mut db := sqlite.connect(':memory:')!

parent := PlainNoArg{
children: [Child{}, Child{}]
}

sql db {
create table Child
create table PlainNoArg
insert parent into PlainNoArg
}!

parents := sql db {
select from PlainNoArg limit 1
}!

assert parent.children.len == parents[0].children.len
}

struct SqlSerial {
id int @[primary; sql: serial]
name string
Expand Down
4 changes: 2 additions & 2 deletions vlib/v/gen/c/orm.v
Original file line number Diff line number Diff line change
Expand Up @@ -331,8 +331,8 @@ fn (mut g Gen) write_orm_insert_with_last_ids(node ast.SqlStmtLine, connection_v

primary_field := g.get_orm_struct_primary_field(fields) or { ast.StructField{} }

is_serial := primary_field.attrs.contains_arg('sql', 'serial')
&& primary_field.typ == ast.int_type
is_serial := (primary_field.attrs.contains_arg('sql', 'serial')
|| primary_field.attrs.contains('serial')) && primary_field.typ == ast.int_type

mut inserting_object_type := ast.void_type
mut member_access_type := '.'
Expand Down
Loading