haskell - Representing sum types in sql with Persistent, and Esqueleto joins -
i have been trying figure out sensible way represent haskell sum types in sql backend using persistent.
my target haskell data type along lines of
data widget = foowidget int | barwidget t.text data helement = helement { name :: t.text , widget :: widget }
i modeling these using following persistent data types:
element name t.text foo elementid elementid size int bar elementid elementid colour t.text
there ever either foo or bar each element, never both.
i want use left outer join
select elements , corresponding foo or bar. esqueleto expression is:
select $ $ \(elem `leftouterjoin` foo `leftouterjoin` bar) -> on (just (elem ^. elementid) ==. foo ?. fooelementid) on (just (elem ^. elementid) ==. bar ?. barelementid) return (elem, foo, bar)
however, when execute code, error:
user error (postgresql.withstmt': bad result status fatalerror (("pgres_fatal_error","error: missing from-clause entry table
if remove second join, giving:
select $ $ \(elem `leftouterjoin` foo) -> on (just (elem ^. elementid) ==. foo ?. fooelementid) return (elem, foo)
the code runs without error. i'm sure it's obvious can't see doing wrong.
edit: found problem was; docs:
note order of on clauses reversed! you're required write ons in reverse order because helps composability (see documentation of on more details).
the following code works (the order of on
expressions reversed):
select $ $ \(elem `leftouterjoin` foo `leftouterjoin` bar) -> on (just (elem ^. elementid) ==. bar ?. barelementid) on (just (elem ^. elementid) ==. foo ?. fooelementid) return (elem,foo,bar)
thanks,
michael
adding answer suggested waldheinz
i found problem was; docs:
note order of on clauses reversed! you're required write ons in reverse order because helps composability (see documentation of on more details).
the following code works (the order of on
expressions reversed):
select $ $ \(elem `leftouterjoin` foo `leftouterjoin` bar) -> on (just (elem ^. elementid) ==. bar ?. barelementid) on (just (elem ^. elementid) ==. foo ?. fooelementid) return (elem,foo,bar)
regards,
michael
Comments
Post a Comment