Opened 14 years ago
Last modified 11 years ago
#13803 closed
Mmodel attribute cannot be named pk and set primary_key=True — at Initial Version
Reported by: | Jon Gales | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | dev |
Severity: | Normal | Keywords: | inspectdb |
Cc: | hjeffrey | Triage Stage: | Accepted |
Has patch: | yes | Needs documentation: | no |
Needs tests: | yes | Patch needs improvement: | yes |
Easy pickings: | no | UI/UX: | no |
Description
I had an existing database that I wanted to manipulate with Django and ran in through the inspectdb management command. Opening up the admin to try and edit the table led to a consistent Python crash (OS X popped up the crash log which I attached). After trial and error I narrowed it down to the name of the model attribute: "pk". Changing the attribute name to something else and then setting db_column='pk' allowed everything to function normally.
When accessing the model through the shell Python doesn't crash, but no data is returned on queries and nothing can be inserted. Here's an example model that shows the problem, run syncdb to set it up and then try to interact with it.
from django.db import models
class Example(models.Model):
pk = models.AutoField(primary_key=True, db_column='pk')
---
test = Example()
File "/Users/jonknee/src/envs/django_1.2/lib/python2.6/site-packages/django/db/models/base.py", line 403, in _set_pk_val
return setattr(self, self._meta.pk.attname, value)
RuntimeError: maximum recursion depth exceeded while calling a Python object
Changing the attribute name makes everything work normally:
class Example(models.Model):
pkey = models.AutoField(primary_key=True, db_column='pk')
This is an issue in both 1.1 and 1.2, I'm using Python 2.6.1.
Python crash log