AutoFields must have primary_key=True?

709 views
Skip to first unread message

Roy Smith

unread,
Apr 16, 2012, 11:17:47 AM4/16/12
to Django users
I need an integer field which generates increasing integers. But I don't want it to be my primary key. In fact, it can't be, because I want the ability to manually reset it to something else. The auto-increment is just a default value.

If I do:

> class Image(Model):
> serial = AutoField()

I get an error when I do syncdb or schemamigration:

> AssertionError: AutoFields must have primary_key=True.

Is this a bug?

---
Roy Smith
r...@panix.com

Gerald Klein

unread,
Apr 16, 2012, 11:20:22 AM4/16/12
to django...@googlegroups.com
Auto-incrementing fields are meant to be primary keys that is their purpose. I have never tried it but a formula in the default value for an integer field to increment it might work


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.




--

Gerald Klein DBA

Cont...@geraldklein.com

www.geraldklein.com

j...@zognet.com

708-599-0352


Linux registered user #548580 



Marcin Tustin

unread,
Apr 16, 2012, 11:21:11 AM4/16/12
to django...@googlegroups.com
I came up against this also. My work-around was to create a model that exists explicitly for the purpose of generating sequential integers (I also need them to be unique, so it's acceptable for my purposes). You might like to have two fields to accomplish the same thing with duplicates (which I admit is less than ideal).

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.




--
Marcin Tustin
Tel: 07773 787 105

Masklinn

unread,
Apr 16, 2012, 11:49:14 AM4/16/12
to django...@googlegroups.com

On 2012-04-16, at 17:17 , Roy Smith wrote:
>> AssertionError: AutoFields must have primary_key=True.
>
> Is this a bug?

Unlikely considering the assertion was specifically added to AutoField as part of a commit noting:

> Refactored the way save() works so that non-integer primary keys are
> now possible. custom_pk unit tests (from [458]) now pass. Refs #81.

There doesn't seem to be any more information than that, but a quick
search on the tracker[0] many databases seem to have… issues with
non-key autofields (let alone multiple autofields in a single table)

So the solution would be to either use a sequences-using DB backend (aka
not MySQL unless you want to hack that horribly via a dedicated table
with an autoinc PK) and create your own sequence field (which may or may
not be easy, I don't know how complex the SQL underpinnings of a Django
model field can be), or to just do it "by hand" in your model's
overridden save.

[0] https://code.djangoproject.com/ticket/8576

Reply all
Reply to author
Forward
0 new messages