[Haskell-cafe] Top-down inserts in Persistent

Michael Orlitzky michael at orlitzky.com
Thu Dec 26 17:17:56 UTC 2013


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.


More information about the Haskell-Cafe mailing list