diff --git a/vlib/orm/orm_test.v b/vlib/orm/orm_test.v index 6aa0268be9..e29d4435e1 100644 --- a/vlib/orm/orm_test.v +++ b/vlib/orm/orm_test.v @@ -29,6 +29,7 @@ fn test_orm_sqlite() { db.exec("insert into User (name, age) values ('Sam', 29)") db.exec("insert into User (name, age) values ('Peter', 31)") db.exec("insert into User (name, age, is_customer) values ('Kate', 30, 1)") + nr_all_users := sql db { select count from User } @@ -213,6 +214,12 @@ fn test_orm_sqlite() { select from User order by age desc limit 1 } assert updated_oldest.age == 31 + + db.exec('insert into User (name, age) values (NULL, 31)') + null_user := sql db { + select from User where id == 5 + } + assert null_user.name == '' } fn test_orm_pg() { diff --git a/vlib/v/gen/sql.v b/vlib/v/gen/sql.v index 3613bdbe8b..5f8ef28bb1 100644 --- a/vlib/v/gen/sql.v +++ b/vlib/v/gen/sql.v @@ -226,7 +226,11 @@ fn (mut g Gen) sql_select_expr(node ast.SqlExpr) { mut func := 'sqlite3_column_int' if field.typ == table.string_type { func = 'sqlite3_column_text' - g.writeln('${tmp}.$field.name = tos_clone(${func}($g.sql_stmt_name, $i));') + string_data := g.new_tmp_var() + g.writeln('byteptr $string_data = ${func}($g.sql_stmt_name, $i);') + g.writeln('if ($string_data != NULL) {') + g.writeln('\t${tmp}.$field.name = tos_clone($string_data);') + g.writeln('}') } else { g.writeln('${tmp}.$field.name = ${func}($g.sql_stmt_name, $i);') }