mirror of
https://github.com/vlang/v.git
synced 2025-09-13 14:32:26 +03:00
orm: fix subquery without where expr (#21598)
This commit is contained in:
parent
a2ce55d922
commit
1e86e06eb6
3 changed files with 133 additions and 45 deletions
83
vlib/db/sqlite/parent_child_test.v
Normal file
83
vlib/db/sqlite/parent_child_test.v
Normal file
|
@ -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
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue