1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

orm: fix inserting sequential values (id=0), in tables with an i64 primary field (#18791)

This commit is contained in:
Delyan Angelov 2023-07-05 23:25:22 +03:00 committed by GitHub
parent 7f8749afdd
commit aa61fcb3dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 3 deletions

View File

@ -209,9 +209,16 @@ pub fn orm_stmt_gen(sql_dialect SQLDialect, table string, q string, kind StmtKin
if data.data.len > 0 {
// Allow the database to insert an automatically generated primary key
// under the hood if it is not passed by the user.
if is_primary_column && data.data[i].type_idx() in orm.nums {
if (data.data[i] as int) == 0 {
continue
tidx := data.data[i].type_idx()
if is_primary_column && (tidx in orm.nums || tidx in orm.num64) {
x := data.data[i]
match x {
i8, i16, int, i64, u8, u16, u32, u64 {
if i64(x) == 0 {
continue
}
}
else {}
}
}

View File

@ -287,3 +287,29 @@ fn test_orm_insert_with_multiple_child_elements() {
assert parent.notes[1].text == 'Second note'
assert parent.notes[2].text == 'Third note'
}
[table: 'customers']
struct Customer {
id i64 [primary; sql: serial]
name string
}
fn test_i64_primary_field_works_with_insertions_of_id_0() {
db := sqlite.connect(':memory:')!
sql db {
create table Customer
}!
for i in ['Bob', 'Charlie'] {
new_customer := Customer{
name: i
}
sql db {
insert new_customer into Customer
}!
}
users := sql db {
select from Customer
}!
assert users.len == 2
// println("${users}")
}