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:
parent
7f8749afdd
commit
aa61fcb3dc
@ -209,11 +209,18 @@ pub fn orm_stmt_gen(sql_dialect SQLDialect, table string, q string, kind StmtKin
|
|||||||
if data.data.len > 0 {
|
if data.data.len > 0 {
|
||||||
// Allow the database to insert an automatically generated primary key
|
// Allow the database to insert an automatically generated primary key
|
||||||
// under the hood if it is not passed by the user.
|
// under the hood if it is not passed by the user.
|
||||||
if is_primary_column && data.data[i].type_idx() in orm.nums {
|
tidx := data.data[i].type_idx()
|
||||||
if (data.data[i] as int) == 0 {
|
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
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
match data.data[i].type_name() {
|
match data.data[i].type_name() {
|
||||||
'string' {
|
'string' {
|
||||||
|
@ -287,3 +287,29 @@ fn test_orm_insert_with_multiple_child_elements() {
|
|||||||
assert parent.notes[1].text == 'Second note'
|
assert parent.notes[1].text == 'Second note'
|
||||||
assert parent.notes[2].text == 'Third 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}")
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user