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_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
|
||||
|
||||
@@ -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 {}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user