Skip to content

Commit aa61fcb

Browse files
authored
orm: fix inserting sequential values (id=0), in tables with an i64 primary field (#18791)
1 parent 7f8749a commit aa61fcb

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

vlib/orm/orm.v

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,16 @@ pub fn orm_stmt_gen(sql_dialect SQLDialect, table string, q string, kind StmtKin
209209
if data.data.len > 0 {
210210
// Allow the database to insert an automatically generated primary key
211211
// under the hood if it is not passed by the user.
212-
if is_primary_column && data.data[i].type_idx() in orm.nums {
213-
if (data.data[i] as int) == 0 {
214-
continue
212+
tidx := data.data[i].type_idx()
213+
if is_primary_column && (tidx in orm.nums || tidx in orm.num64) {
214+
x := data.data[i]
215+
match x {
216+
i8, i16, int, i64, u8, u16, u32, u64 {
217+
if i64(x) == 0 {
218+
continue
219+
}
220+
}
221+
else {}
215222
}
216223
}
217224

vlib/orm/orm_insert_test.v

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,3 +287,29 @@ fn test_orm_insert_with_multiple_child_elements() {
287287
assert parent.notes[1].text == 'Second note'
288288
assert parent.notes[2].text == 'Third note'
289289
}
290+
291+
[table: 'customers']
292+
struct Customer {
293+
id i64 [primary; sql: serial]
294+
name string
295+
}
296+
297+
fn test_i64_primary_field_works_with_insertions_of_id_0() {
298+
db := sqlite.connect(':memory:')!
299+
sql db {
300+
create table Customer
301+
}!
302+
for i in ['Bob', 'Charlie'] {
303+
new_customer := Customer{
304+
name: i
305+
}
306+
sql db {
307+
insert new_customer into Customer
308+
}!
309+
}
310+
users := sql db {
311+
select from Customer
312+
}!
313+
assert users.len == 2
314+
// println("${users}")
315+
}

0 commit comments

Comments
 (0)