[Haskell-cafe] ORM for haskell?

Marc Weber marco-oweber at gmx.de
Tue Jun 30 13:52:08 EDT 2009


Some time ago I stumbled upon SQLAlchemy which is a great ORM wrapper
library for python. It has a nice syntax I'd like to see in a haskell
library as well.

SQLAlchemy already provides some lazy features such as loading subitems
on access etc.

All haskell SQL libraries I know only let you run SQL statements but not
much more. To start real business you no longer want to write many SQL
commands.

Example why it matters:
schools - 1:n - teachers - 1:n - pupils

If you want to list all schools which have a pupil with age > 3 you'd
write an sql query like this:

  SELECT dictinct * FROM schools as s JOIN teachers t ON (t.school_id = s.id) JOIN pupils as p ON (p.teacher_id = t.id) WHERE p.age > 3

  in SQLAlchemy it looks like this:
  session.query(School).join(School.teachers).join(Teacher.pupils).filter(Pupil.age > 3).all()

  difference? Because SQLAlchemy knows about the relations you don't have
  to remember alias names. So there is no chance to get that wrong.


Another example: Updating the age of a pupil:

  row = SELECT * FROM pupils where age = 13;
  UPDATE pupils SET age = 14 WHERE id = <the id you got above>

  p = session.query(Pupil).filter(Pupil.age==13).one().age=14
  session.commit()

  difference?
  You don't have to care about ids. you just assign a new value and tell
  the engine that it should commit.
  So again less chances to get something wrong.


What about trees (eg web site navigation)

 id   |  title    | parent_id
  1   |  top      | null
  2   |  submenu  | 1
  3   |  submenu2 | 1

should result in

top
  - submenu
  - submenu2

using SQLAlchemy you can just do

parent = session.query('nodes').filter(Node.id = 1)

def print(node):
  print node.title
  print node.subnodes # this will run a subquery automatically for you returning submenu{,2}

Again no sql. No chance to get something wrong?

You can skim the manual to get a better idea how SQLAlchemy works
http://www.sqlalchemy.org/docs/05/sqlalchemy_0_5_5.pdf

I have to admit that I haven't used SQLAlchemy in a real project yet.
However I can imagine doing so. Comparing this to what we have on
hackage I'd say some work has to be done to get close to SQLAlchemy.

The backend doesn't have to be a relational database. However I'd like
to use this kind of abstraction in haskell.

Is there anyone interested in helping building a library which
a) let's you define kind of model of you data
b) let's you store you model in any backend (maybe a relational
    database)
c) does static checking of your queries at compilation time?

Right now I'd say the best way to go is define the model in the
application and not get the scheme from an existing database because
there is not way to store all scheme details within a relational model.
I think SQLAlchemy does it right by providing a way to define the model
in python.

Of course haskell doesn't have "objects" to store. But GADTs could be
stored (data Foo = ...)

So are there any volunteers who are interested in helping writing this
kind of storage solution for haskell which could be used in real world
business apps?

Maybe this does already exist and I've missed it?

Marc Weber


More information about the Haskell-Cafe mailing list