When using the following code, I don't want to use the default database key. Intead, I plan to generate a hashcode from a piece of information and use it as a key. How should I use such a key?
share [mkPersist sqlSettings, mkMigrate "migrateAll"] [persistLowerCase|
Person
name String
age Int Maybe
deriving Show
BlogPost
title String
authorId PersonId
deriving Show
|]
A code demonstrating such an idea:
#!/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
Note that above code will produce this output:
CREATE TABLE "blogpost"("topic" VARCHAR NOT NULL,"hash" VARCHAR NOT NULL, PRIMARY KEY ("hash"))
You can see how the primary key of the blogpost
table is hash
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With