From 6391f3d2dac87afd8457aa7bda1a657b6c56a13c Mon Sep 17 00:00:00 2001 From: Louis Schmieder Date: Sun, 26 Sep 2021 10:17:56 +0200 Subject: [PATCH] orm: fix other int types (#11981) --- examples/database/orm.v | 2 +- vlib/orm/orm_test.v | 19 ++++++++++++++++++- vlib/v/checker/checker.v | 3 ++- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/examples/database/orm.v b/examples/database/orm.v index b308e914d9..5081df6df2 100644 --- a/examples/database/orm.v +++ b/examples/database/orm.v @@ -12,7 +12,7 @@ struct Module { struct User { id int [primary; sql: serial] - age int [unique: 'user'] + age u32 [unique: 'user'] name string [sql: 'username'; unique] is_customer bool [sql: 'abc'; unique: 'user'] skipped_string string [skip] diff --git a/vlib/orm/orm_test.v b/vlib/orm/orm_test.v index c5e4fe1a74..9c85a54d5e 100644 --- a/vlib/orm/orm_test.v +++ b/vlib/orm/orm_test.v @@ -8,6 +8,7 @@ struct Module { id int [primary; sql: serial] name string nr_downloads int + test_id u64 user User } @@ -33,7 +34,7 @@ fn test_orm_sqlite() { db := sqlite.connect(':memory:') or { panic(err) } db.exec('drop table if exists User') sql db { - create table User + create table Module } name := 'Peter' @@ -313,4 +314,20 @@ fn test_orm_sqlite() { } assert data.len == 1 + + mod := Module{} + + sql db { + insert mod into Module + } + + sql db { + update Module set test_id = 11 where id == 1 + } + + test_id_mod := sql db { + select from Module where id == 1 + } + + assert test_id_mod.test_id == 11 } diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index e622dbe168..2dd35317fa 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -8088,7 +8088,8 @@ fn (mut c Checker) sql_stmt_line(mut node ast.SqlStmtLine) ast.Type { } fn (mut c Checker) fetch_and_verify_orm_fields(info ast.Struct, pos token.Position, table_name string) []ast.StructField { - fields := info.fields.filter((it.typ in [ast.string_type, ast.int_type, ast.bool_type] + fields := info.fields.filter( + (it.typ in [ast.string_type, ast.bool_type] || int(it.typ) in ast.number_type_idxs || c.table.type_symbols[int(it.typ)].kind == .struct_ || (c.table.get_type_symbol(it.typ).kind == .array && c.table.get_type_symbol(c.table.get_type_symbol(it.typ).array_info().elem_type).kind == .struct_))