From 95a328be98fba2ff0101aa4bda9e9200e7e847d8 Mon Sep 17 00:00:00 2001 From: Hitalo Souza <63821277+enghitalo@users.noreply.github.com> Date: Tue, 6 Sep 2022 07:12:37 -0300 Subject: [PATCH] orm: improve the README and the tests. Add an error message for foreign key tags (#15670) --- vlib/orm/README.md | 21 +++++++++++++++++++-- vlib/orm/orm.v | 9 --------- vlib/v/gen/c/sql.v | 16 ++++++++++++---- vlib/v/tests/orm_sub_array_struct_test.v | 20 ++++++++++---------- 4 files changed, 41 insertions(+), 25 deletions(-) diff --git a/vlib/orm/README.md b/vlib/orm/README.md index 29d86043bc..7ad3fc53cb 100644 --- a/vlib/orm/README.md +++ b/vlib/orm/README.md @@ -17,6 +17,7 @@ - `[sql: 'name']` sets a custom column name for the field - `[sql_type: 'SQL TYPE']` sets the sql type which is used in sql - `[default: 'sql defaults']` sets the default value or function when create a new table +- `[fkey: 'parent_id']` sets foreign key for an field which holds an array ## Usage @@ -27,7 +28,15 @@ struct Foo { created_at time.Time [sql_type: 'DATETIME'] updated_at string [sql_type: 'DATETIME'] deleted_at time.Time + children []Child [fkey: 'parent_id'] } + +struct Child { + id int [primary; sql: serial] + parent_id int + name string +} + ``` ### Create @@ -54,6 +63,14 @@ var := Foo{ created_at: time.now() updated_at: time.now().str() deleted_at: time.now() + children: [ + Child{ + name: 'abc' + }, + Child{ + name: 'def' + }, + ] } sql db { @@ -65,7 +82,7 @@ sql db { ```v ignore sql db { - update Foo set name = 'cde' where name == 'abc' + update Foo set name = 'cde', updated_at = time.now() where name == 'abc' } ``` @@ -84,7 +101,7 @@ result := sql db { ``` ```v ignore result := sql db { - select from Foo where id > 1 limit 5 + select from Foo where id > 1 && name != 'lasanha' limit 5 } ``` ```v ignore diff --git a/vlib/orm/orm.v b/vlib/orm/orm.v index 206397bac3..097606c85b 100644 --- a/vlib/orm/orm.v +++ b/vlib/orm/orm.v @@ -383,7 +383,6 @@ pub fn orm_table_gen(table string, q string, defaults bool, def_unique_len int, mut is_unique := false mut is_skip := false mut unique_len := 0 - // mut fkey := '' mut field_name := sql_field_name(field) mut ctyp := sql_from_v(sql_field_type(field)) or { field_name = '${field_name}_id' @@ -427,14 +426,6 @@ pub fn orm_table_gen(table string, q string, defaults bool, def_unique_len int, default_val = attr.arg } } - /*'fkey' { - if attr.arg != '' { - if attr.kind == .string { - fkey = attr.arg - continue - } - } - }*/ else {} } } diff --git a/vlib/v/gen/c/sql.v b/vlib/v/gen/c/sql.v index 4861578a6e..440af1ae7f 100644 --- a/vlib/v/gen/c/sql.v +++ b/vlib/v/gen/c/sql.v @@ -157,8 +157,12 @@ fn (mut g Gen) sql_insert(node ast.SqlStmtLine, expr string, table_name string, } else if sym.kind == .array { mut f_key := '' for attr in f.attrs { - if attr.name == 'fkey' && attr.has_arg && attr.kind == .string { - f_key = attr.arg + if attr.name == 'fkey' && attr.has_arg { + if attr.kind == .string { + f_key = attr.arg + } else { + verror("fkey attribute need be string. Try [fkey: '$attr.arg'] instead of [fkey: $attr.arg]") + } } } if f_key == '' { @@ -714,8 +718,12 @@ fn (mut g Gen) sql_select(node ast.SqlExpr, expr string, left string, or_expr as } else if sym.kind == .array { mut fkey := '' for attr in field.attrs { - if attr.name == 'fkey' && attr.has_arg && attr.kind == .string { - fkey = attr.arg + if attr.name == 'fkey' && attr.has_arg { + if attr.kind == .string { + fkey = attr.arg + } else { + verror("fkey attribute need be string. Try [fkey: '$attr.arg'] instead of [fkey: $attr.arg]") + } } } if fkey == '' { diff --git a/vlib/v/tests/orm_sub_array_struct_test.v b/vlib/v/tests/orm_sub_array_struct_test.v index f5322be290..cc79e755ca 100644 --- a/vlib/v/tests/orm_sub_array_struct_test.v +++ b/vlib/v/tests/orm_sub_array_struct_test.v @@ -1,12 +1,12 @@ import sqlite struct Parent { - id int [primary; sql: serial] - name string - chields []Chield [fkey: 'parent_id'] + id int [primary; sql: serial] + name string + children []Child [fkey: 'parent_id'] } -struct Chield { +struct Child { id int [primary; sql: serial] parent_id int name string @@ -20,11 +20,11 @@ fn test_orm_array() { par := Parent{ name: 'test' - chields: [ - Chield{ + children: [ + Child{ name: 'abc' }, - Chield{ + Child{ name: 'def' }, ] @@ -43,7 +43,7 @@ fn test_orm_array() { } assert parent.name == par.name - assert parent.chields.len == par.chields.len - assert parent.chields[0].name == 'abc' - assert parent.chields[1].name == 'def' + assert parent.children.len == par.children.len + assert parent.children[0].name == 'abc' + assert parent.children[1].name == 'def' }