My initial objective was to convert the result of an optional subquery into a Rel8able record, but I think the issue title describes the root issue.
I am building a query that checks multiple sources for the same data, and I'm trying to return a record-of-Maybes so it's clear which source had the data I'm looking for. My first instinct was to make a fake "table" record:
data LookupResult f = LookupResult
{ fromSource1 :: Column f (Maybe Int)
, fromSource2 :: Column f (Maybe Int)
} deriving stock (Generic)
deriving anyclass (Rel8able)
Both tables I'm querying have the same primary key, but the tables are slightly different: source1 is a nullable column in a normal database table, declared as a Column f (Maybe Int). source2 is a non-nullable column in a different table, declared as Column f Int. We expect source1 to always exist, so it is fetched using each and filter in the standard rel8 idiom.
source2 is expected to only sometimes contain the row matching our primary key, but its column is NOT NULL, so I am currently fetching it by applying optional to the result of an each/filter query.
This query against source2 gives me a MaybeTable Expr (Expr Int), and I'm not sure how to get an Expr (Maybe Int) that I can store into my record. maybeTable brings me no joy: maybeTable Nothing Just returns Maybe (Expr Int).
I can work around this by assembling the record outside of the run1 call, using fmap. But if I want to do more inside a query, I think it would be neat to be able to derive an Expr (Maybe a) from the corresponding MaybeTable. But is it actually possible to do so?
My initial objective was to convert the result of an
optionalsubquery into aRel8ablerecord, but I think the issue title describes the root issue.I am building a query that checks multiple sources for the same data, and I'm trying to return a record-of-
Maybes so it's clear which source had the data I'm looking for. My first instinct was to make a fake "table" record:Both tables I'm querying have the same primary key, but the tables are slightly different:
source1is a nullable column in a normal database table, declared as aColumn f (Maybe Int).source2is a non-nullable column in a different table, declared asColumn f Int. We expectsource1to always exist, so it is fetched usingeachandfilterin the standardrel8idiom.source2is expected to only sometimes contain the row matching our primary key, but its column isNOT NULL, so I am currently fetching it by applyingoptionalto the result of aneach/filterquery.This query against
source2gives me aMaybeTable Expr (Expr Int), and I'm not sure how to get anExpr (Maybe Int)that I can store into my record.maybeTablebrings me no joy:maybeTable Nothing JustreturnsMaybe (Expr Int).I can work around this by assembling the record outside of the
run1call, usingfmap. But if I want to do more inside a query, I think it would be neat to be able to derive anExpr (Maybe a)from the correspondingMaybeTable. But is it actually possible to do so?