dealing with legacy tables without primary key

540 views
Skip to first unread message

Eric

unread,
Oct 2, 2008, 4:31:59 AM10/2/08
to Django developers
Hi,
I saw few questions from people who wonder how to manage tables
without primary key (when using inspectdb on a legacy db);
A great hope is born: I just discovered that this problem could be
adressed, at least it works with sqlite:

just add this field (rowid or oid is the sqlite automatic pk) in the
model:

rowid = models.IntegerField(primary_key=True, editable=False)

It works ! it's so simple ! I love django !

Maybe this tip could be added in the doc; for others sgbd, there must
be something similar.
And why not put this in the inspectdb command ...

cheers,
Eric

PS: I am testing this on the "Django Hacks Trac" project (and I am
testing the new kenai forge too): http://kenai.com/projects/django-trac

Ludvig Ericson

unread,
Oct 2, 2008, 10:51:53 AM10/2/08
to django-d...@googlegroups.com
On Oct 2, 2008, at 10:31, Eric wrote:

>
> Hi,
> I saw few questions from people who wonder how to manage tables
> without primary key (when using inspectdb on a legacy db);
> A great hope is born: I just discovered that this problem could be
> adressed, at least it works with sqlite:
>
> just add this field (rowid or oid is the sqlite automatic pk) in the
> model:
>
> rowid = models.IntegerField(primary_key=True, editable=False)
>
> It works ! it's so simple ! I love django !
>
> Maybe this tip could be added in the doc; for others sgbd, there must
> be something similar.
> And why not put this in the inspectdb command ...

Because it's a dirty, dirty hack.

Eric

unread,
Oct 2, 2008, 4:21:36 PM10/2/08
to Django developers
It is your opinion; what is your solution ? it should interest me,
because this "dirty hack" can only work with sqlite, oracle (and
postgres 8+ with a special configuration), but not with mysql.

David Durham, Jr.

unread,
Oct 2, 2008, 4:30:23 PM10/2/08
to django-d...@googlegroups.com
>> > Maybe this tip could be added in the doc; for others sgbd, there must
>> > be something similar.
>> > And why not put this in the inspectdb command ...
>>
>> Because it's a dirty, dirty hack.
> It is your opinion; what is your solution ? it should interest me,
> because this "dirty hack" can only work with sqlite, oracle (and
> postgres 8+ with a special configuration), but not with mysql.

I think the solutions are 1) use primary keys or 2) use dirty hacks.

-Dave

Ian Kelly

unread,
Oct 2, 2008, 5:34:53 PM10/2/08
to django-d...@googlegroups.com

I would be surprised if it works with Oracle, unless you replace
IntegerField with CharField(max_length=18).

Eric

unread,
Oct 4, 2008, 1:09:50 PM10/4/08
to Django developers
> I would be surprised if it works with Oracle, unless you replace
> IntegerField with CharField(max_length=18).

I made tests only on sqlite (a trac db).
I mean it ''should'' works with Oracle because Oracle support oid's;
but I have made no test. If someone want to ...
Reply all
Reply to author
Forward
0 new messages