我一直在嘗試在項目中使用Opaleye運行左連接,但我無法編譯代碼。我開始與兩個 「模型」,它代表了關聯的表:左加入Opaleye
第一:
data ModelA' a b = Model { primA :: a, foreignA :: b }
type ModelA = ModelA' UUID UUID
type ModelAColumn = ModelA' (Column PGUuid) (Column (Nullable PGUuid))
$(makeAdaptorAndInstance "pModelA" ''ModelA')
table :: Table ModelAColumn ModelAColumn
table = Opaleye.table "model_a" $ pModelA (ModelA (tableColumn "uuid") (tableColumn "foreign"))
而且也:
data ModelB' a b = Model { primB :: a, valB :: b }
type ModelB = ModelB' UUID String
type ModelBColumn = ModelB' (Column PGUuid) (Column PGText)
$(makeAdaptorAndInstance "pModelB" ''ModelB')
table :: Table ModelBColumn ModelBColumn
table = Opaleye.table "model_b" $ pModelB (ModelB (tableColumn "uuid") (tableColumn "val"))
的各類反映,MODELA可以沒有ModelB與相關。
我需要一個查詢來獲得由外部A == primB上的表之間的左連接給出的(ModelA,Maybe ModelB)對。我期待它看起來像:
doJoin :: Connection -> IO [(ModelA, Maybe ModelB)]
doJoin conn = runQuery conn query
where
query :: Query (ModelAColumn, Maybe ModelBColumn)
query = leftJoin (queryTable ModelA.table) (queryTable ModelB.table) (\(ma, mb) -> foreignA ma .== primB mb)
但這不起作用。我也試了多個變種,尤其是我在替換查詢類型簽名的權利,明確規定列的空性:
query :: Query (ModelAColumn, (Column (Nullable PGUuid), Column (Nullable PGText))
但這種失敗:
沒有實例Data.Profunctor.Product.Default.Class.Default Opaleye.Internal.Join.NullMaker ModelBColumn(欄(可空 PGUuid),柱(可空PGText)。
我怎樣才能讓這個查詢在Opaleye?
你需要使用'通過'FunctionalJoin'連接''模塊? –