1
0
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:
Hitalo Souza 2022-09-06 07:12:37 -03:00 committed by GitHub
parent e3d3863fbe
commit 95a328be98
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 41 additions and 25 deletions

View File

@ -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

View File

@ -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 {}
}
}

View File

@ -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 == '' {

View File

@ -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'
}