How to select from table ordering by primary key

243 Views Asked by At

I have this SQLite persitent schema:

share [mkPersist sqlSettings, mkMigrate "migrateAll"] [persistLowerCase|
Entry
  class EntryClass
|]

This maps to a following CREATE TABLE statement:

CREATE TABLE "entry"
  ( "id" INTEGER PRIMARY KEY AUTOINCREMENT
  , "class" INTEGER NOT NULL);"

I've manually added the AUTOINCREMENT keyword to be sure that identifiers are always monotonically increasing. Now I want to pick the oldest entry from the database - that's the one with lowest identifier.

If I would write SQL I would say something like this:

SELECT id, class FROM entry ORDER BY id ASC LIMIT 1

But I have problems translating this query to persistent. It should look something like

nextEntry <- selectFirst [] [Asc EntryId, LimitTo 1]

but this gives me a syntax error complaining about the fact that there is no data constructor EntryId. Indeed EntryId is a type synonym for Database.Persist.Class.PersistEntity.Key Entry, so it cannot be use in the Asc data constructor, which has a type forall typ . Asc (EntityField record typ).

So the question is how could I make persistent order my query results by table's primary key?

1

There are 1 best solutions below

1
On BEST ANSWER

That should actually work; EntryId is both a type and data constructor. For example, the following code (just tweaked from the synopsis of the persistent chapter of the Yesod book) compiles just fine:

{-# LANGUAGE EmptyDataDecls             #-}
{-# LANGUAGE FlexibleContexts           #-}
{-# LANGUAGE GADTs                      #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses      #-}
{-# LANGUAGE OverloadedStrings          #-}
{-# LANGUAGE QuasiQuotes                #-}
{-# LANGUAGE TemplateHaskell            #-}
{-# LANGUAGE TypeFamilies               #-}
import Control.Monad.IO.Class  (liftIO)
import Database.Persist
import Database.Persist.Sqlite
import Database.Persist.TH

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

main :: IO ()
main = runSqlite ":memory:" $ do
    runMigration migrateAll

    johnId <- insert $ Person "John Doe" $ Just 35
    janeId <- insert $ Person "Jane Doe" Nothing

    selectList [] [Asc PersonId] >>= liftIO . mapM_ print