[Haskell-cafe] Top-down inserts in Persistent

Adam Bergmark adam at bergmark.nl
Sun Dec 29 17:33:38 UTC 2013


Hi Michael,

The entity definitions in persistent is very close to the SQL schema, in a
1-to-many relation you must have the foreign key relation defined in the
many table.

You should preferably not insert a car before it's owner is inserted, that
would give you a null reference. So if possible you should insert people
first which will return their id and you can then do the insertion of cars
safely. You can also construct keys manually, this is kind of an hack since
you may construct invalid IDs.

In a relational schema you can make the name of the person the primary key.
There has been some work in adding arbitrarily typed primary keys to
persistent, but I'm not sure if it has been released or is on master.
Either way, using a person name as a primary key may be a bad idea because
of collisions.

Having some mismatch when moving things to relational storage is common. A
lot of times I end up creating intermediary types that contain the data in
a format that makes it easier to work with. But I don't mind this at all,
Haskell makes it very safe to add proxy types and refactor them. You
sometimes end up having to do more queries to the DB than seems necessary,
but this is only a problem if it turns out to be a bottle neck.

HTH,
Adam



On Thu, Dec 26, 2013 at 12:17 PM, Michael Orlitzky <michael at orlitzky.com>wrote:

> I'm loading data from XML into a Haskell data type, and I'd like to use
> Persistent to save it to a database. The examples from the Yesod book
> have you manually define a FooId field and create the relationships
> yourself from the bottom up. For example, "a person has many cars":
>
>   blah blah [persistLowerCase|
>     Person
>       name String
>     Car
>       ownerId PersonId Eq
>       name String
>   |]
>
> This works well if you're responsible for creating every person/car
> manually. But what if the data are given to you? If I were to parse
> people from an XML file, the cars wouldn't have people_ids in them.
> Instead I'd get,
>
>   blah blah [persistLowerCase|
>     Person
>       name String
>       cars [Car]
>     Car
>       name String
>   |]
>
> As long as the cars list contains another Persistent type, it seems like
> I should be able to insert a person and have it insert the cars, with
> proper foreign keys, automatically. Doing it manually isn't
> straight-forward because I can't add the "ownerId" field to my Car type
> and still expect to parse it from the XML (which has no such field).
>
> Any ideas? I'm not married to Persistent yet; I just want to read in
> some XML and save it to a database without having to specify the names
> and types in three places (preferred place: in Haskell). I don't care
> too much about the schema I get as long as it's relational.
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20131229/fba21c12/attachment.html>


More information about the Haskell-Cafe mailing list