from Hacker News

Using Postgres Arrays in Django

by g3orge on 11/6/12, 3:14 PM with 9 comments

  • by SoftwareMaven on 11/6/12, 5:20 PM

    I'm curious about the performance characteristic of Postgres arrays and how to decide when to use an array versus when to use a many-to-many join.

    For instance, I have an app with a "project" with users that have different roles (owners and members). Right now, I'm modeling that with a join table that contains the role information. Would those be better as arrays? (I'm inclined to think not.)

    Alternatively, I also have a messaging system in place, where the recipients probably would be better modeled with an array.

    Anyway, this is great information, and it gives another, nice tool in the quiver. Thanks!

  • by thanatos519 on 11/6/12, 6:44 PM

    A very sensible implementation, but the syntax tastes clunky and exposes the SQL data type.

    Instead of letting the SQL leak into my code, as in texts = ArrayField(dbtype="text", dimension=2) I want to write text = models.TextField() ''.Array(dimension=2) so can't djorm_pgarray.fields (or any other array-supporting ORM magic) add Array() to the Field base class?

  • by waterside81 on 11/6/12, 5:31 PM

    This looks like one of those "sometimes it's better, sometimes it's not" features. If you're using the Django admin, then the many-to-many relationship would be preferable because it's a better way to manage lists. If you're not and you're just storing lists of simple data, then this might be better. YMMV.
  • by cbsmith on 11/6/12, 6:13 PM