一個代碼演示這樣的想法:
#!/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
表的主鍵是如何hash
你在使用什麼庫。 – SwiftsNamesake
Persistent.sqlite – WellTyped
數據庫密鑰是什麼意思?作爲主鍵的列? –