PostgreSQL – How to specify different column types according to the database type of Haskell GROUNDHOG?

Using the Groundhog library in Haskell, I want to implement a column type that uses “uuid” when the backend is Postgresql, otherwise just use “varchar” for any other backends. Although this should be possible according to the comments in Groundhog.Core, I am not entirely sure how to unpack the dbType from the proxy db, and there is no such example in groundhog-examples because the column types have hard-coded examples in those. p>

I want to get some help in Postgresql case matching, I will solve the remaining problems after sorting. This is where I am:

instance PrimitivePersistField UUID where
toPrimitivePersistValue _ uuid = PersistString $show uuid
fromPrimitivePersistValue _ (PersistString a) = fromJust $UUIDmethods.fromString a

instance PersistField UUID where
persistName _ = "UUID"
toPersistValues ​​= primToPersistValue
fromPersistValues ​​= primFromPersistValue
dbType db _ = case db of
Postgresql _ -> DbTypePrimitive (DbOther$OtherTypeDef [Left "uuid "]) False Nothing Nothing
_ -> DbTypePrimitive (DbOther $OtherTypeDef [Left "varchar"]) False Nothing Nothing

This problem occurred during compilation:

Couldn't match expected type'proxy db'
with actual typ e'Postgresql'
Relevant bindings include
db :: proxy db (bound at basicGroundhog.hs:34:10)
dbType :: proxy db -> UUID -> DbType
(bound at basicGroundhag.hs:34:3)
In the pattern: Postgresql _
In a case alternative:
Postgresql _
-> DbTypePrimitive
(DbOther $ OtherTypeDef [Left "uuid"]) False Nothing Nothing
In the expression:
case db of {
Postgresql _
-> DbTypePrimitive
(DbOther $OtherTypeDef [Left "uuid"]) False Nothing Nothing
_ -> DbTypePrimitive
(DbOther $OtherTypeDef [Left "varchar"]) False Nothing Nothing }

You can use backendName to check the database at runtime. It is best to use DbString for the default case so that groundhog can choose a more suitable type. Varchar without a maximum length declaration Not valid in MySQL.

dbType db _ = case backendName db of
"postgresql" -> DbTypePrimitive (DbOther $ OtherTypeDef [Left "uuid"]) False Nothing Nothing
_ -> DbTypePrimitive DbString False Nothing Nothing

Using the Groundhog library in Haskell, I hope to implement a Use the column type of “uuid” when the backend is Postgresql, otherwise just use “varchar” for any other backend. Although this should be possible according to the comments in Groundhog.Core, I’m not entirely sure how to get from the proxy db Unpack the dbType in the dbType, and there is no such example in groundhog-examples, because the column types have hard-coded examples in those.

I want to get some help in case matching in Postgresql, I will Solve the remaining problems after sorting. This is where I am:

instance PrimitivePersistField UUID where
toPrimitivePersistValue _ uuid = PersistString $show uuid
fromPrimitivePersistValue _ (PersistString a) = fromJust $UUIDmethods.fromString a

instance PersistField UUID where
persistName _ = "UUID"
toPersistValues ​​= primToPersistValue
fromPersistValues ​​= prim br /> dbType db _ = case db of
Postgresql _ -> DbTypePrimitive (DbOther $OtherTypeDef [Left "uuid"]) False Nothing Nothing
_ -> DbTypePrimitive (DbOther $OtherTypeDef [Left "varchar" ]) False Nothing Nothing

This problem occurred at compile time :

Couldn't match expected type'proxy db'
with actual type'Postgresql'
Relevant bindings include
db :: proxy db (bound at basicGroundhog.hs:34:10)
dbType :: proxy db -> UUID -> DbType
(bound at basicGroundhag.hs:34:3)
In the pattern: Postgresql _
In a case alternative:
Postgresql _
-> DbTypePrimitive
(DbOther $OtherTypeDef [Left "uuid"]) False Nothing Nothing
In the expression:
case db of {
Postgresql _
-> DbTypePrimitive
(DbOther $OtherTypeDef [Left "uuid"]) False Nothing Nothing
_ -> DbTypePrimitive
(DbOther $OtherTypeDef [Left "varchar"]) False Nothing Nothing }

You can use backendName to check the database at runtime. It is best to use DbString for The default is so that groundhog can choose a more suitable type. Varchar without a maximum length declaration is invalid in MySQL.

dbType db _ = case backendName db of
"postgresql" -> DbTypePrimitive (DbOther $OtherTypeDef [Left "uuid"]) False Nothing Nothing
_ -> DbTypePrimitive DbString False Nothing Nothing

Leave a Comment

Your email address will not be published.