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?
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: