Creating a multi-column index

1,090 views
Skip to first unread message

Matt Doran

unread,
Mar 9, 2009, 12:58:16 AM3/9/09
to Django users
Hi there,

I'm porting another system across to Django, and I need to create a
multi-column unique index. I previously used SQLObject which has a
syntax for doing this.

I came across this old post that said it wasn't possible and it
referred to creating custom backend SQL.


http://groups.google.com/group/django-users/browse_thread/thread/b789e7fa56d778ca/efd5f2f6d766e785?lnk=gst&q=multiple+column+index#efd5f2f6d766e785

The link to the documentation, no longer seems to contain the right
info. I found this:

http://docs.djangoproject.com/en/dev/howto/initial-data/

... which seems to do the job, but means I need to define the SQL for
each backend type.

Is this the best way to achieve this? Or is there something built-in
to django now?

Thanks,
Matt

Alex Gaynor

unread,
Mar 9, 2009, 1:00:35 AM3/9/09
to django...@googlegroups.com
Django can't handle a multiple column primary key(see ticket #373) however it can handle a multiple column unique index: http://docs.djangoproject.com/en/dev/ref/models/options/#unique-together.

Alex

--
"I disapprove of what you say, but I will defend to the death your right to say it." --Voltaire
"The people's good is the highest law."--Cicero

Malcolm Tredinnick

unread,
Mar 9, 2009, 1:03:50 AM3/9/09
to django...@googlegroups.com
On Sun, 2009-03-08 at 21:58 -0700, Matt Doran wrote:
> Hi there,
>
> I'm porting another system across to Django, and I need to create a
> multi-column unique index. I previously used SQLObject which has a
> syntax for doing this.
>
> I came across this old post that said it wasn't possible and it
> referred to creating custom backend SQL.
>
>
> http://groups.google.com/group/django-users/browse_thread/thread/b789e7fa56d778ca/efd5f2f6d766e785?lnk=gst&q=multiple+column+index#efd5f2f6d766e785
>
> The link to the documentation, no longer seems to contain the right
> info. I found this:
>
> http://docs.djangoproject.com/en/dev/howto/initial-data/
>
> ... which seems to do the job, but means I need to define the SQL for
> each backend type.

Your application is only going to run on one backend, so focus on that.

Also, if you're doing anything complex in custom SQL, use the
post_syncdb signal, rather than the initial data hooks. The latter hooks
don't contain a full SQL parser (and we have no intention to extend it
more than it is), so it can be confused for complicated things. For
index creation, it's probably fine. But if you get to the point of
wanting to add triggers or something, then use post_syncdb signal
handlers.

Regards,
Malcolm


Matt Doran

unread,
Mar 9, 2009, 2:28:13 AM3/9/09
to Django users
Hi Alex + Malcolm,

On Mar 9, 4:03 pm, Malcolm Tredinnick <malc...@pointy-stick.com>
wrote:
> On Sun, 2009-03-08 at 21:58 -0700, Matt Doran wrote:
> > Hi there,
>
> > I'm porting another system across to Django, and I need to create a
> > multi-column unique index.  I previously used SQLObject which has a
> > syntax for doing this.
>
>
> Also, if you're doing anything complex in custom SQL, use the
> post_syncdb signal, rather than the initial data hooks. The latter hooks
> don't contain a full SQL parser (and we have no intention to extend it
> more than it is), so it can be confused for complicated things. For
> index creation, it's probably fine. But if you get to the point of
> wanting to add triggers or something, then use post_syncdb signal
> handlers.
>

Thanks guys .... looks like "unique_together" is what I'm after for
this simple case.

Regards,
Matt
Reply all
Reply to author
Forward
0 new messages