Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp committed May 31, 2024
1 parent 3e6a9e0 commit 2b8dd1a
Showing 1 changed file with 83 additions and 0 deletions.
83 changes: 83 additions & 0 deletions vlib/db/sqlite/parent_child_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import db.sqlite

struct Parent {
id int @[primary; sql: serial]
name string
children []Child @[fkey: 'parent_id']
}

struct Child {
id int @[primary; sql: serial]
name string
parent_id int
babies []Baby @[fkey: 'child_id']
}

struct Baby {
id int @[primary; sql: serial]
name string
child_id int
}

fn test_main() {
db := sqlite.connect(':memory:')!

sql db {
create table Parent
create table Child
create table Baby
} or { panic(err) }

new_parent := Parent{
name: 'first parent'
children: [
Child{
name: 'first child'
},
Child{
name: 'second_child'
},
]
}

sql db {
insert new_parent into Parent
} or { panic(err) }

babies := [
Baby{
name: 'first baby'
child_id: 1
},
Baby{
name: 'second baby'
child_id: 1
},
Baby{
name: 'third baby'
child_id: 2
},
Baby{
name: 'fourth baby'
child_id: 2
},
Baby{
name: 'fifth baby'
child_id: 2
},
]

for v in babies {
sql db {
insert v into Baby
} or { panic(err) }
}

parent := sql db {
select from Parent
} or { panic(err) }

assert parent[0].children[0].id == 1
assert parent[0].children[1].id == 2
assert parent[0].children.len == 2
}

0 comments on commit 2b8dd1a

Please sign in to comment.