-
Notifications
You must be signed in to change notification settings - Fork 108
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
HasField
for SqlExpr (Maybe (Entity a))
should use Nullable
#407
Comments
Proposed because one of the most common sources of the |
This may not actually be super beginner friendly - the |
Ah, alas, we can't use /home/matt/Projects/esqueleto/src/Database/Esqueleto/Internal/Internal.hs:2483:5: error: [GHC-73138]
• Illegal type synonym family application ‘Nullable typ’ in instance:
HasField
sym
(SqlExpr (Maybe (Entity rec)))
(SqlExpr (Value (Maybe (Nullable typ))))
• In the instance declaration for
‘HasField sym (SqlExpr (Maybe (Entity rec))) (SqlExpr (Value (Maybe (Nullable typ))))’
|
2483 | HasField sym (SqlExpr (Maybe (Entity rec))) (SqlExpr (Value (Maybe (Nullable typ))))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ So we need a class... This works, but it's real ugly: instance
(PersistEntity rec, PersistField typ, PersistField typ', SymbolToField sym rec typ
, NullableFieldProjection typ typ'
, HasField sym (SqlExpr (Maybe (Entity rec))) (SqlExpr (Value (Maybe typ')))
)
=>
HasField sym (SqlExpr (Maybe (Entity rec))) (SqlExpr (Value (Maybe typ')))
where
getField expr = veryUnsafeCoerceSqlExpr (expr ?. symbolToField @sym)
class NullableFieldProjection typ typ'
instance {-# overlapping #-} NullableFieldProjection (Maybe typ) typ
instance {-# overlappable #-} NullableFieldProjection typ typ The type inference is really bad, too. We can't have a functional dependency because there's an overlap in the two instances. However, we do get this working: instance
(PersistEntity rec, PersistField typ, PersistField typ', SymbolToField sym rec typ
, NullableFieldProjection typ typ'
, HasField sym (SqlExpr (Maybe (Entity rec))) (SqlExpr (Value (Maybe typ')))
)
=>
HasField sym (SqlExpr (Maybe (Entity rec))) (SqlExpr (Value (Maybe typ')))
where
getField expr = veryUnsafeCoerceSqlExpr (expr ?. symbolToField @sym)
class NullableFieldProjection typ typ'
instance {-# overlapping #-} (typ ~ typ') => NullableFieldProjection (Maybe typ) typ'
instance {-# overlappable #-} (typ ~ typ') => NullableFieldProjection typ typ' But unfortunately the errors are pretty bad where itDb "joins Maybe together" $ do
void $ select $ do
deed :& lord <-
Experimental.from $
table @Deed
`leftJoin` table @Lord
`Experimental.on` do
\(deed :& lord) ->
lord.id ==. just deed.ownerId
where_ $ joinV lord.dogs >=. just (val 10)
pure lord This fails with:
We can fix this by changing joinV
:: (NullableFieldProjection typ typ')
=> SqlExpr (Value (Maybe typ))
-> SqlExpr (Value (Maybe typ'))
joinV = veryUnsafeCoerceSqlExprValue
class NullableFieldProjection typ typ'
instance {-# incoherent #-} (typ ~ typ') => NullableFieldProjection (Maybe typ) typ'
instance {-# overlappable #-} (typ ~ typ') => NullableFieldProjection typ typ' This feels dicey enough that I want to experiment with this separately on the work project to see how it works out. |
@rampion suggested this in the Mercury slack, and I like the idea.
Quoting:
The text was updated successfully, but these errors were encountered: