Skip to content

Instantly share code, notes, and snippets.

@nepeckman
Created September 8, 2020 21:35
Show Gist options
  • Save nepeckman/5152f1eb93f8213782f880d666e066cb to your computer and use it in GitHub Desktop.
Save nepeckman/5152f1eb93f8213782f880d666e066cb to your computer and use it in GitHub Desktop.
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