From d33f7d12f73d9f7cfb27c28939a2dd4305d4ffc5 Mon Sep 17 00:00:00 2001 From: Louis Schmieder Date: Thu, 28 Oct 2021 21:31:41 +0200 Subject: [PATCH] orm: fix time struct in sql stmt (#12298) --- vlib/orm/orm_test.v | 15 ++++++++++++++- vlib/v/checker/checker.v | 2 +- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/vlib/orm/orm_test.v b/vlib/orm/orm_test.v index 9c85a54d5e..e862a478bd 100644 --- a/vlib/orm/orm_test.v +++ b/vlib/orm/orm_test.v @@ -5,11 +5,12 @@ import time import sqlite struct Module { - id int [primary; sql: serial] + id int [primary; sql: serial] name string nr_downloads int test_id u64 user User + created time.Time } [table: 'userlist'] @@ -330,4 +331,16 @@ fn test_orm_sqlite() { } assert test_id_mod.test_id == 11 + + t := time.now() + sql db { + update Module set created = t where id == 1 + } + updated_time_mod := sql db { + select from Module where id == 1 + } + // NB: usually updated_time_mod.created != t, because t has + // its microseconds set, while the value retrieved from the DB + // has them zeroed, because the db field resolution is seconds. + assert updated_time_mod.created.format_ss() == t.format_ss() } diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 2d6de47363..12ebdb888a 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -7893,7 +7893,7 @@ fn (mut c Checker) fetch_field_name(field ast.StructField) string { } } sym := c.table.get_type_symbol(field.typ) - if sym.kind == .struct_ { + if sym.kind == .struct_ && sym.name != 'time.Time' { name = '${name}_id' } return name