Haskell持久性数据库主键

时间:2017-07-17 09:14:03

标签: haskell

使用以下代码时,我不想使用默认数据库密钥。 Intead,我计划从一条信息中生成一个哈希码并将其用作密钥。我该如何使用这样的钥匙?

share [mkPersist sqlSettings, mkMigrate "migrateAll"] [persistLowerCase|
Person
    name String
    age Int Maybe
    deriving Show
BlogPost
    title String
    authorId PersonId
    deriving Show
|]

1 个答案:

答案 0 :(得分:3)

展示这样一个想法的代码:

[[7,3,5], [24,16,30], [56,85,20]]

请注意,上面的代码将生成此输出:

#!/usr/bin/env stack
{- stack
     --resolver lts-7.14
     --install-ghc
     runghc
     --package persistent
     --package persistent-template
     --package persistent-sqlite
-}

{-# LANGUAGE EmptyDataDecls #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeFamilies #-}

import Database.Persist
import Database.Persist.Sqlite
import Database.Persist.TH

share
  [mkPersist sqlSettings, mkMigrate "migrateAll"]
  [persistLowerCase|
Blogpost
    topic String
    hash String
    Primary hash
    deriving Show 
|]

main :: IO ()
main = mockMigration migrateAll

您可以看到CREATE TABLE "blogpost"("topic" VARCHAR NOT NULL,"hash" VARCHAR NOT NULL, PRIMARY KEY ("hash")) 表的主键是blogpost