Skip to content

Commit 711470d

Browse files
authored
orm: fix option field with default null value (fix #24222) (#24228)
1 parent b76997a commit 711470d

2 files changed

Lines changed: 66 additions & 61 deletions

File tree

‎vlib/orm/orm_func.v‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -432,9 +432,12 @@ fn (qb &QueryBuilder[T]) map_row(row []Primitive) !T {
432432
if index >= 0 {
433433
value := row[index]
434434

435-
if value == Primitive(Null{}) {
436-
// set to none by default
437-
} else {
435+
$if field.typ is $option {
436+
if value == Primitive(Null{}) {
437+
instance.$(field.name) = none
438+
}
439+
}
440+
if value != Primitive(Null{}) {
438441
$if field.typ is i8 || field.typ is ?i8 {
439442
instance.$(field.name) = match value {
440443
i8 { i8(value) }

‎vlib/orm/orm_func_test.v‎

Lines changed: 60 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,12 @@ struct User {
4545
// a `null` value in database, will map to default value of the require field in struct
4646
@[table: 'sys_users']
4747
struct UserPart {
48-
id int @[primary; serial]
49-
name string
50-
created_at time.Time @[sql_type: 'TIMESTAMP']
51-
updated_at time.Time @[sql_type: 'TIMESTAMP']
48+
id int @[primary; serial]
49+
name string
50+
created_at time.Time @[sql_type: 'TIMESTAMP']
51+
updated_at time.Time @[sql_type: 'TIMESTAMP']
52+
option_i8 ?i8 = 13 // option with default test
53+
option_string ?string = 'this is not none'
5254
}
5355

5456
fn test_orm_func_where() {
@@ -232,38 +234,38 @@ fn test_orm_func_stmts() {
232234
option_string: 'hello'
233235
},
234236
User{
235-
name: 'Silly'
236-
age: 27
237-
role: 'employer'
238-
status: 5
239-
salary: 2500
240-
title: 'doctor'
241-
score: 81
242-
updated_at: time.now()
243-
type_i8: 1
244-
type_i16: 2
245-
type_int: 3
246-
type_i64: 4
247-
type_u8: 5
248-
type_u16: 6
249-
type_u32: 7
250-
type_u64: 8
251-
type_f32: 1.1
252-
type_f64: 2.2
253-
type_bool: true
254-
type_string: 'hello'
255-
option_i8: 1
256-
option_i16: 2
257-
option_int: 3
258-
option_i64: 4
259-
option_u8: 5
260-
option_u16: 6
261-
option_u32: 7
262-
option_u64: 8
263-
option_f32: 1.1
264-
option_f64: 2.2
265-
option_bool: true
266-
option_string: 'hello'
237+
name: 'Silly'
238+
age: 27
239+
role: 'employer'
240+
status: 5
241+
salary: 2500
242+
title: 'doctor'
243+
score: 81
244+
updated_at: time.now()
245+
type_i8: 1
246+
type_i16: 2
247+
type_int: 3
248+
type_i64: 4
249+
type_u8: 5
250+
type_u16: 6
251+
type_u32: 7
252+
type_u64: 8
253+
type_f32: 1.1
254+
type_f64: 2.2
255+
type_bool: true
256+
type_string: 'hello'
257+
option_i8: 1
258+
option_i16: 2
259+
option_int: 3
260+
option_i64: 4
261+
option_u8: 5
262+
option_u16: 6
263+
option_u32: 7
264+
option_u64: 8
265+
option_f32: 1.1
266+
option_f64: 2.2
267+
option_bool: true
268+
// option_string: 'hello' // option with default test
267269
},
268270
User{
269271
name: 'Smith'
@@ -336,28 +338,28 @@ fn test_orm_func_stmts() {
336338
option_string: 'hello'
337339
},
338340
User{
339-
name: 'Peter'
340-
age: 29
341-
role: 'employer'
342-
status: 1
343-
salary: 3500
344-
title: 'doctor'
345-
score: 80
346-
created_at: time.now()
347-
updated_at: time.now()
348-
type_i8: 1
349-
type_i16: 2
350-
type_int: 3
351-
type_i64: 4
352-
type_u8: 5
353-
type_u16: 6
354-
type_u32: 7
355-
type_u64: 8
356-
type_f32: 1.1
357-
type_f64: 2.2
358-
type_bool: true
359-
type_string: 'hello'
360-
option_i8: 1
341+
name: 'Peter'
342+
age: 29
343+
role: 'employer'
344+
status: 1
345+
salary: 3500
346+
title: 'doctor'
347+
score: 80
348+
created_at: time.now()
349+
updated_at: time.now()
350+
type_i8: 1
351+
type_i16: 2
352+
type_int: 3
353+
type_i64: 4
354+
type_u8: 5
355+
type_u16: 6
356+
type_u32: 7
357+
type_u64: 8
358+
type_f32: 1.1
359+
type_f64: 2.2
360+
type_bool: true
361+
type_string: 'hello'
362+
// option_i8: 1 // option with default test
361363
option_i16: 2
362364
option_int: 3
363365
option_i64: 4

0 commit comments

Comments
 (0)