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:
@ -17,6 +17,7 @@
|
|||||||
- `[sql: 'name']` sets a custom column name for the field
|
- `[sql: 'name']` sets a custom column name for the field
|
||||||
- `[sql_type: 'SQL TYPE']` sets the sql type which is used in sql
|
- `[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
|
- `[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
|
## Usage
|
||||||
|
|
||||||
@ -27,7 +28,15 @@ struct Foo {
|
|||||||
created_at time.Time [sql_type: 'DATETIME']
|
created_at time.Time [sql_type: 'DATETIME']
|
||||||
updated_at string [sql_type: 'DATETIME']
|
updated_at string [sql_type: 'DATETIME']
|
||||||
deleted_at time.Time
|
deleted_at time.Time
|
||||||
|
children []Child [fkey: 'parent_id']
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct Child {
|
||||||
|
id int [primary; sql: serial]
|
||||||
|
parent_id int
|
||||||
|
name string
|
||||||
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
### Create
|
### Create
|
||||||
@ -54,6 +63,14 @@ var := Foo{
|
|||||||
created_at: time.now()
|
created_at: time.now()
|
||||||
updated_at: time.now().str()
|
updated_at: time.now().str()
|
||||||
deleted_at: time.now()
|
deleted_at: time.now()
|
||||||
|
children: [
|
||||||
|
Child{
|
||||||
|
name: 'abc'
|
||||||
|
},
|
||||||
|
Child{
|
||||||
|
name: 'def'
|
||||||
|
},
|
||||||
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
sql db {
|
sql db {
|
||||||
@ -65,7 +82,7 @@ sql db {
|
|||||||
|
|
||||||
```v ignore
|
```v ignore
|
||||||
sql db {
|
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
|
```v ignore
|
||||||
result := sql db {
|
result := sql db {
|
||||||
select from Foo where id > 1 limit 5
|
select from Foo where id > 1 && name != 'lasanha' limit 5
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
```v ignore
|
```v ignore
|
||||||
|
@ -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_unique := false
|
||||||
mut is_skip := false
|
mut is_skip := false
|
||||||
mut unique_len := 0
|
mut unique_len := 0
|
||||||
// mut fkey := ''
|
|
||||||
mut field_name := sql_field_name(field)
|
mut field_name := sql_field_name(field)
|
||||||
mut ctyp := sql_from_v(sql_field_type(field)) or {
|
mut ctyp := sql_from_v(sql_field_type(field)) or {
|
||||||
field_name = '${field_name}_id'
|
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
|
default_val = attr.arg
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/*'fkey' {
|
|
||||||
if attr.arg != '' {
|
|
||||||
if attr.kind == .string {
|
|
||||||
fkey = attr.arg
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}*/
|
|
||||||
else {}
|
else {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -157,8 +157,12 @@ fn (mut g Gen) sql_insert(node ast.SqlStmtLine, expr string, table_name string,
|
|||||||
} else if sym.kind == .array {
|
} else if sym.kind == .array {
|
||||||
mut f_key := ''
|
mut f_key := ''
|
||||||
for attr in f.attrs {
|
for attr in f.attrs {
|
||||||
if attr.name == 'fkey' && attr.has_arg && attr.kind == .string {
|
if attr.name == 'fkey' && attr.has_arg {
|
||||||
f_key = attr.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 == '' {
|
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 {
|
} else if sym.kind == .array {
|
||||||
mut fkey := ''
|
mut fkey := ''
|
||||||
for attr in field.attrs {
|
for attr in field.attrs {
|
||||||
if attr.name == 'fkey' && attr.has_arg && attr.kind == .string {
|
if attr.name == 'fkey' && attr.has_arg {
|
||||||
fkey = attr.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 == '' {
|
if fkey == '' {
|
||||||
|
@ -1,12 +1,12 @@
|
|||||||
import sqlite
|
import sqlite
|
||||||
|
|
||||||
struct Parent {
|
struct Parent {
|
||||||
id int [primary; sql: serial]
|
id int [primary; sql: serial]
|
||||||
name string
|
name string
|
||||||
chields []Chield [fkey: 'parent_id']
|
children []Child [fkey: 'parent_id']
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Chield {
|
struct Child {
|
||||||
id int [primary; sql: serial]
|
id int [primary; sql: serial]
|
||||||
parent_id int
|
parent_id int
|
||||||
name string
|
name string
|
||||||
@ -20,11 +20,11 @@ fn test_orm_array() {
|
|||||||
|
|
||||||
par := Parent{
|
par := Parent{
|
||||||
name: 'test'
|
name: 'test'
|
||||||
chields: [
|
children: [
|
||||||
Chield{
|
Child{
|
||||||
name: 'abc'
|
name: 'abc'
|
||||||
},
|
},
|
||||||
Chield{
|
Child{
|
||||||
name: 'def'
|
name: 'def'
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
@ -43,7 +43,7 @@ fn test_orm_array() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
assert parent.name == par.name
|
assert parent.name == par.name
|
||||||
assert parent.chields.len == par.chields.len
|
assert parent.children.len == par.children.len
|
||||||
assert parent.chields[0].name == 'abc'
|
assert parent.children[0].name == 'abc'
|
||||||
assert parent.chields[1].name == 'def'
|
assert parent.children[1].name == 'def'
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user