Search code examples
yesodpersistent

Persistent `toSqlKey` for custom primary key


I am defining the following models, where Category has a non-integer primary key code of type Text.

Category
    code Text
    Primary code
    deriving Show Typeable

CategoryTreeNode
    name Text
    code CategoryId Maybe
    lft Int MigrationOnly default=0
    rgt Int MigrationOnly default=0
    deriving Show Typeable

For models defined without Primary, I could use toSqlKey to convert arbitrary Int64s into Key Category. Is there an equivalent function for models with custom primary keys?


Solution

  • I found the answer by looking through persistent's TH module:

    https://github.com/yesodweb/persistent/blob/9396c278fc181fdac4a97c53637700417f41a478/persistent-template/Database/Persist/TH.hs#L857-L858

    This line suggest that a CategoryKey is generated via TH and that is indeed the case:

    let x :: Key Category
        x = CategoryKey "foobar"