65810

Use of `get` and `toSqlKey` in persistent

Question:

I'm trying to use persistent-postgresql with servant.

I have a User model.

I want to have an endpoint that takes an id and returns the user with that id.

According to other answers I can use toSqlKey to turn an Int64 into a Key to feed to get.

My function looks like:

oneUser :: Int64 -> App (Entity User) oneUser userId = do maybeUser <- runDb $ get $ toSqlKey userId case maybeUser of Nothing -> throwError err404 Just user -> return user

When I try to compile I get the error Couldn't match expected type ‘PersistEntityBackend (Entity User)’ with actual type ‘SqlBackend’

Use of selectList works fine.

allUsers :: App [Entity User] allUsers = runDb $ selectList [] []

Please tell me what I'm doing wrong and where I should look in the future for stuff like this. I couldn't find get on hackage/the right version of the library on stackage etc.

runDb looks like:

runDb :: (MonadReader Config m, MonadIO m) => SqlPersistT IO b -> m b runDb query = do pool <- asks getPool liftIO $ runSqlPool query pool

taken from <a href="https://github.com/parsonsmatt/servant-persistent/blob/master/src/Models.hs" rel="nofollow">this github project</a>.

Answer1:

The difference is that get ... returns a plain User not an Entity User, so this will work:

altSingleUser :: Int64 -> App User altSingleUser userid = do let foo = get (toSqlKey userid) :: SqlPersistT IO (Maybe User) maybeUser <- runDb $ foo case maybeUser of Nothing -> throwError err404 Just person -> return person

If you want to return an Entity User, just change the last return statement to:

return $ Entity { entityKey = toSqlKey userid, entityVal = person }

Recommend

  • N+1 in rails relations with active records?
  • JSON data - Group by days of the week(Sun,Mon,Tue, Wed,Thu,Fri,Sat) using Javascript/Jquery
  • Distribute numbers to two “containers” and minimize their difference of sum
  • How to order the ties in data so that the previously observed value appears first
  • Find string between two substrings AND between string and the end of file
  • Sprite animation wobbly / jumping in IE11
  • Populating a database table with returned JSON
  • Yii Bootstrap not loading JS files
  • Defer unused CSS
  • How to get the Owner of the ContextMenu (from Silverlight 4 toolkit)?
  • Android studio import problems. (Apktool)
  • Checking for valid enum types from protobufs
  • Connect to a local database from phpmyadmin with R
  • Compiling multiple source files in Rcpp
  • 'Edit' function for forum posts and such
  • How to resolve this in PHPUnit where it is asking me to set KERNEL_DIR in my phpunit.xml?
  • Set WebClient.Builder.exchangeStrategies() without losing Spring Jackson configuration
  • SQL Server 2012 not showing unicode character in results
  • select an input by value?
  • Regex not working in java 1.5
  • When to use the tag in the head and body section of a html page? [duplicate]
  • Tensorflow Dataset API restore Iterator after completing one epoch
  • How to turn off notice reporting in xampp?
  • How to change user identity when git pushing via ssh?
  • Bind selectedDates Aggregation for Calendar
  • php “page caching” solution suggestions for CMS Applications
  • Update cell query for Excel ADO from Delphi
  • How to get rgb from transparent pixel in js
  • VLOOKUP in IMPORTRANGE
  • Call Microservice from another Microservice within Docker
  • Cross compile glibc for arm, got undefined reference to some unwind functions
  • How to use FirstOrDefault inside Include
  • How do I use TagLib-Sharp to write custom (PRIV) ID3 frames?
  • PHP Permalinks.. how to change?
  • CAS 4 - Not able to retrieve the LDAP groups after successful authentication
  • Running R's aov() mixed effects model from Python using rpy2
  • ReferenceError: TextEncoder is not defined
  • JavaScript RegExp Replace