Created
September 8, 2020 21:35
-
-
Save nepeckman/5152f1eb93f8213782f880d666e066cb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import norm/sqlite, norm/model | |
type Foo* = ref object of Model | |
str*: string | |
val*: int | |
type Bar* = ref object of Model | |
foo1*: Foo | |
foo2*: Foo | |
func newFoo*(str: string, val: int): Foo = | |
Foo(str: str, val: val) | |
func newFoo*(): Foo = newFoo("", 0) | |
func newBar*(foo1, foo2: Foo): Bar = | |
Bar(foo1: foo1, foo2: foo2) | |
func newBar*(): Bar = newBar(newFoo(), newFoo()) | |
withDb: | |
db.createTables(newBar()) | |
var bar = newBar(newFoo("a", 1), newFoo("b", 2)) | |
db.insert(bar) | |
echo bar.foo1[] # (str: "a", val: 1, id: 1) | |
echo bar.foo2[] # (str: "b", val: 2, id: 2) | |
let id = bar.id | |
bar = newBar() | |
db.select(bar, "Bar.id = ?", id) | |
echo bar.foo1[] # (str: "a", val: 1, id: 1) | |
echo bar.foo2[] # (str: "a", val: 1, id: 1) | |
# Desired result: (str: "b", val: 2, id: 2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment