Persistent is a type-safe, universal data store interface for Haskell with support for various backends, including PostgreSQL, SQLite, MySQL and MongoDB.
Questions tagged [haskell-persistent]
57 questions
1
vote
1 answer
Haskell "persistent" model: How to correctly define cross-reference?
Imagine you have two models (Foo and Bar) and they are both have references to each other (Foo have barRef with type BarId and Bar have fooRef with type FooId). Everything goes okay:
#!/usr/bin/env stack
{- stack script --resolver=lts-9.21…

unclechu
- 821
- 10
- 14
1
vote
0 answers
Reduce class constraints in haskell and inspecting persistent tables
I'm trying to setup a basic endpoint which takes an id and goes through a join table to return all joined records using persistent and spock, a working implementation looks like this.
get ("songs" /> var) $ \id -> do
song <- getRecord404 $…

SJH
- 43
- 1
- 3
1
vote
1 answer
How do I store Either (Key a) (Key b)?
I have the following model:
User
...
Group
...
Sharing
objectId (Either UserId GroupId)
In Sharing entity I want to store either UserId or GroupId and differentiate between them. Simply using Either doesn't work:
Not in scope: type…

arrowd
- 33,231
- 8
- 79
- 110
1
vote
1 answer
Get id from Entity record
I have an Entity record, specifically Entity User and I need to extract the Id which the User has in the database as an Int.
From reading the docs it seems entityKey would be useful here but I'm not quite sure how I would go about getting an Int…

Qwertie
- 5,784
- 12
- 45
- 89
1
vote
1 answer
Couldn't match type ‘PersistEntityBackend (Entity a)’ with ‘SqlBackend’
I've got the following:
asSqlBackendReader :: ReaderT SqlBackend m a -> ReaderT SqlBackend m a
asSqlBackendReader = id
insertEnt :: (Entity a) -> IO (Key (Entity a))
insertEnt x = runWithDb $ do
insert $ x
where runWithDb = runSqlite "test.db"…

Chris Stryczynski
- 30,145
- 48
- 175
- 286
1
vote
1 answer
Remove underscore from fields with generated lenses in Persistent
Let's suppose that I have a persistent type and want to project some value from this type:
share [mkPersist sqlSettings, mkMigrate "migrateAll"] [persistLowerCase|
User
name Text
email Text
|]
...
getName :: Entity User ->…

ptkato
- 668
- 1
- 7
- 14
1
vote
0 answers
Persistent selectList type mismatch ‘Database.Persist.Sql.Types.Internal.SqlBackend’
I am working on a Servant 0.7.1 application and trying to use Persistent-2.5 to query a Postgresql database, but I am getting mismatched types with a Persistent query.
This application was previously working with Servant 0.4 and Persistent 2.2, but…

erewok
- 7,555
- 3
- 33
- 45
1
vote
1 answer
PersistValues for MongoDB Keys in Yesod/Persistent 2
I have some code that was doing the following in persistent < 2:
(either (\_ -> 0) id).fromPersistValue.unKey.entityKey $ myEntity
How can I achieve the same functionality using persistent > 2 as keys are now defined by individual Backends and as…

Eric
- 663
- 4
- 14
1
vote
1 answer
How do I respond with the value of a Key in Yesod?
I'm writing a REST API for a resource in Yesod. I have a POST method that should try to create the given resource and if successful return 201 and the ID of the newly created resource. E.g.,
postResourceR :: Handler String
postResourceR = do
id <-…

imperfectgrist
- 621
- 4
- 20
1
vote
0 answers
Can you define a maybe property that is not nullable in Yesod Persist?
I am still quite new to Yesod so I might be missing something essential.
My understanding is that adding the Maybe attribute to a Persist Entity field seems to both make the attribute non nullable in the database as well as wrapping it in a Maybe in…

Sam
- 14,642
- 6
- 27
- 39
0
votes
1 answer
Scotty and persistence - type error when using insert function
I've got the following application:
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE…

Henry
- 3,472
- 2
- 12
- 36
0
votes
1 answer
Haskell persistent - getting value via foreign key
Let's assume I've got a very simple db with a Foreign Key (for simplicity of the example: one table with a self reference; context - modelling a financial instrument):
Instrument
ticker String
name String
denomination InstrumentId -- FK…

LA.27
- 1,888
- 19
- 35
0
votes
2 answers
Couldn't match type ‘PersistEntityBackend U’ with ‘SqlBackend’
The following code produces a compile error Couldn't match type ‘PersistEntityBackend U’ with ‘SqlBackend’ arising from a use of ‘insertUser’ due to the commented line:
sampleUser :: Entity User
sampleUser = Entity (toSqlKey 1) $ User
{ userName =…

A Friedrich
- 593
- 6
- 11
0
votes
1 answer
How to create a list of a single column with yesod-persistent?
Given a user, I am trying to select a list of events that are affiliated with organizations that the user is in. The UserOrg table describes which OrgIds correspond to a given UserId.
I have these tables:
User
email Text
name Text
…

Mark Judy
- 3
- 1
0
votes
1 answer
How to set column mapping relation when generating entities in Haskell?
I am trying to use Persistent to access my database. I know that Persistent can generate the code of entity, but there is something I can't generate so easily.
Like I got a table which looks like:
CREATE TABLE `user` (
`user_id` bigint(11) NOT…

Jacky Wong
- 481
- 1
- 3
- 14