Wrestling with how to design a schema for a Django app

January 17, 2011

I've finally gotten to do something with Django, and naturally this has left me wrestling with schema design. This is the first time I've used an ORM and I find that I'm being pulled two ways, neither of which I think are right.

(I'll pause here to note that the data I have to keep track of in this application is tiny, on the order of a few hundred rows in total. I'm using Django's ORM support because this gives me a bunch of functionality for free, I have to keep track of the data somehow, I don't feel like writing yet another set of file parsing and loading and saving code, and using an SQLite backend makes configuration trivial.)

On the one hand are my SQL instincts. With them, I see the problem through the lens of database tables and joins and I think about things like how to normalize everything. I'm pretty sure that this view is wrong in the end, partly because I don't have direct access to SQL joins and so on and partly because it feels like complete overkill to normalize a table that has perhaps 50 rows.

On the other hand are my Python object design instincts; with them I see how the objects would look if I wasn't using an ORM at all, just writing a straight Python program that was loading all of the data from files in some convenient format and then manipulating it (and saving it) without having to think like a database at all. These objects are full of dicts and arrays and names, with 'this defaults to that if not specified' behavior that's easy in code but stupid in databases. They relate to each other in a spiderweb of perfectly sensible relationships that have nothing to do with set operations.

I'm totally sure that the object oriented view is just as wrong as the pure SQL one, because it turns Django's ORM layer into a dumb object store (well, a set of hash tables more or less). I could use the ORM this way if I wanted to; at my tiny data sizes, brute force will work perfectly well.

Somewhere in the middle is an ORM schema design that is sensible both from a Python perspective and a database perspective. I just have to find out what it is, which probably means thinking about the problem more. (Maybe I should write it up for WanderingThoughts, as a form of talking to the duck. That could get me back to first principles, instead of thinking about how to solve in it Python or SQL.)

PS: I consider all of this a valuable learning experience, not something to get frustrated over. I'm certainly glad to be doing a project with a modern web framework instead of banging HTML and CGIs together by hand.


Comments on this page:

From 124.171.214.96 at 2011-01-18 02:30:53:

It sounds like SQL+ORM really is overkill for you here. Have you considered just modelling everything as nice Python classes and then cramming them into a shelf?

By cks at 2011-01-18 10:55:51:

Giving up on Django's ORM means either giving up on all of the Django model-based things that it gives me for free or writing my own 'database' backend layer for the ORM. I don't like the first and I suspect that the second is more work than figuring out how to do what I want inside the ORM.

Written on 17 January 2011.
« More on Linux FHS and /var
What I am trying to do with Django »

Page tools: View Source, View Normal, Add Comment.
Search:
Login: Password:
Atom Syndication: Recent Comments.

Last modified: Mon Jan 17 22:10:32 2011
This dinky wiki is brought to you by the Insane Hackers Guild, Python sub-branch.