parsonsmatt on master
Add comment explaining transact… Encourage use of runSqlConn ove… Merge pull request #951 from in… (compare)
Hey guys. I'm using
Database.Persist.Postgresql and struggling with relations in persistent. Perhaps you guys can give me some help, please :) I have the two following entities:
Schedule json trainId Int stationId StationId srcStationId Int dstStationId Int arrivalTime UTCTime Maybe departureTime UTCTime Maybe deriving Show Station json name Text crsCode Text sqltype=varchar(3) UniqueCode crsCode tiplicCode deriving Show
I'm trying to get all schedule trains for a given station. So far this is what I have:
getScheduleForStation :: Text -> App [Entity Schedule] getScheduleForStation crsCode = do station <- runDb (selectFirst [StationId ==. crsCode] ) runDb (selectList [ScheduleStationId ==. station] )
and this is the error I'm getting:
/Users/rap/code/thesis/TicketReservation/src/Api/Schedule.hs:29:34: error: • Couldn't match type ‘Text’ with ‘Key Station’ arising from a use of ‘StationId’ • In the first argument of ‘(==.)’, namely ‘StationId’ In the expression: StationId ==. crsCode In the first argument of ‘selectFirst’, namely ‘[StationId ==. crsCode]’ /Users/rap/code/thesis/TicketReservation/src/Api/Schedule.hs:30:22: error: • Couldn't match type ‘Maybe (Entity Station)’ with ‘Key Station’ arising from a use of ‘ScheduleStationId’ • In the first argument of ‘(==.)’, namely ‘ScheduleStationId’ In the expression: ScheduleStationId ==. station In the first argument of ‘selectList’, namely ‘[ScheduleStationId ==. station]’
can anyone please help me understand what's wrong with the relation? Thanks in advance.
a beginner here
I am looking for the best haskell ORM to use for abstracting SQL and NoSQL backends.
I have been using/working with Yesod's persistent and made progress; however, I am having problems connecting to an existing database in sqlite3 using persistent.
Is it possible to create an index on an sql table column? I don't mean a unique index. Just a regular index without constraints.
share [mkPersist sqlSettings, mkMigrate "migrateAll"] [persistLowerCase| MyObject myValue String UniqueMyValue myValue |]
would create a unique index on the column
myValue, but how would I create a regular index?
I made a class with a type family for this issue:
class EsqueletoValued m where type ValuesResult m unValueTuple :: m -> ValuesResult m
But maybe there is an existing solution in Esqueleto
selectUnvalue, so I can use the values directly, without having to pattern match the constructor. The
(Value x,...)is something that really makes me feel not DRY at all. Needless to say, this only applies for tuples without
reifyRoles, but it doesn’t seem obvious to me.
re: instrumentation - @MaxGabriel such a
runDB wrapper would only show how long database call(s) took. What would be ideal is to capture stats of each call (sql + duration). Sometimes an inefficient database query does not manifest itself until it's already in Production (an unusual amount of data added to a table that used to be small).
I can think of couple of options
quick and dirty: bracket each database query with timing and log it along with the query.
persistent users either then manually
grep through logs or use a log processor to push to say statsD.
persistent adds ability to track the timing+sql of each database query (ex: in an IORef) and exposes an API to get the latest stats (ex: getStats or runDbWithStats).
@MaxGabriel Saw your change to Yesod wiki - https://github.com/yesodweb/yesod/wiki/ghci/_compare/02fd78f%5E...02fd78f
IIRC, that page has been migrated to
yesod-cookbook repo previously. We really need to fix up the yesod wiki (Just like how persistent wiki was cleaned up some months ago).