使用
Haskell中的Groundhog库,我希望实现一个在后端是Postgresql时使用“uuid”的列类型,否则只需使用“varchar”用于任何其他后端.虽然根据Groundhog.Core中的注释,这应该是可能的,我不完全确定如何从代理db中解包dbType,并且在groundhog-examples中没有这个例子,因为列类型已在那些中硬编码例子.
我想在Postgresql的案例匹配方面得到一些帮助,我会在排序之后解决剩下的问题.这是我在的地方:
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
在编译时出现了这个问题:
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 }