mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
orm: improve the README and the tests. Add an error message for foreign key tags (#15670)
This commit is contained in:
@@ -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 == '' {
|
||||
|
@@ -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'
|
||||
}
|
||||
|
Reference in New Issue
Block a user