[Haskell-cafe] Groundhog - Given entity, how to retrieve autokey value?

lykahb at gmail.com lykahb at gmail.com
Tue Apr 1 03:07:20 UTC 2014


Hi Ross,

The primary key if it is generated by a database is not part of the entity. 
However, you can store it in your entity if you supply it yourself. It is a 
good approach if your entity has a natural primary key (like username). 
Only default key can be auto-incremented. Here is more detailed description 
of how keys work in Groundhog 
https://www.fpcomplete.com/user/lykahb/groundhog#keys-and-references 

Also you may want to use project instead of select. For example, "project 
(AutoKeyField, MyConstructor) $ MyField ==. myValue" would return id and 
the entity. 

Thank you,
Boris Lykah


On Sunday, March 30, 2014 12:41:40 AM UTC-4, Ross Pokorny wrote:
>
> Hello,
>
> I am trying to build a RESTful web application using Groundhog for 
> persistence.  In order to generate URLs for my entities (e.g. for the 
> Location HTTP header), I need a way, given that I have an entity, to obtain 
> its id (its AutoKey).  From what I've gathered so far, in normal Groundhog 
> usage, the primary key is not part of the actual entity datatype.  The only 
> place where I've seen it returned is from the 'insert' functions in 
> Database.Groundhog.  I need the id in cases where I have retrieved an 
> already-existing entity using 'select', but I cannot find a way to obtain 
> it.
>
> I gather that this might not be possible with the current API, and that 
> instead, I may need to explicitly define a field on the entity type to 
> store the id instead of using the auto-generated field that Groundhog 
> normally provides.  In that case, my question is how do I set up that field 
> to be an auto-incrementing key, and not simply a value with a unique 
> constraint.  Also, would Groundhog recognize that field as the AutoKey, and 
> would it return it from 'insert' calls?
>
> Thanks,
>
> Ross Pokorny
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20140331/ad54ad4e/attachment.html>


More information about the Haskell-Cafe mailing list