加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

postgresql – 如何根据Haskell Groundhog的数据库类型指定不同

发布时间:2020-12-13 15:52:47 所属栏目:百科 来源:网络整理
导读:使用 Haskell中的Groundhog库,我希望实现一个在后端是Postgresql时使用“uuid”的列类型,否则只需使用“varchar”用于任何其他后端.虽然根据Groundhog.Core中的注释,这应该是可能的,我不完全确定如何从代理db中解包dbType,并且在groundhog-examples中没有这
使用 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 }

解决方法

您可以使用backendName在运行时检查数据库.最好将DbString用于默认情况,以便groundhog可以选择更合适的类型.没有最大长度声明的Varchar在MySQL中无效.

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读