vxx2 / vlib / v / checker / tests / orm_fkey_update.vv
58 lines · 48 sloc · 770 bytes · 8198e9ecda84101484435df6f612e3609e9aec37
Raw
1import db.sqlite
2
3struct Foo {
4 id int @[primary; sql: serial]
5 name string
6 children []Child @[fkey: 'parent_id']
7}
8
9struct Child {
10 id int @[primary; sql: serial]
11 parent_id int
12 name string
13 bar ?Bar @[fkey: 'child_id']
14}
15
16struct Bar {
17 id int @[primary; sql: serial]
18 child_id int
19 name string
20}
21
22fn main() {
23 db := sqlite.connect(':memory:')!
24
25 sql db {
26 create table Foo
27 create table Child
28 create table Bar
29 }!
30
31 child_one := Child{
32 name: 'abc'
33 }
34
35 child_two := Child{
36 name: 'def'
37 }
38
39 bar_one := Bar{
40 id: 0
41 name: 'name'
42 }
43
44 foo := Foo{
45 name: 'abc'
46 children: [
47 child_one,
48 child_two,
49 ]
50 }
51 _ := sql db {
52 insert foo into Foo
53 }!
54
55 sql db {
56 update Child set bar = bar_one where id == 0
57 }!
58}
59