﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
13803	Model attribute cannot be named pk and set primary_key=True	Jon Gales	hjeffrey	"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."	Bug	closed	Database layer (models, ORM)	dev	Normal	fixed	inspectdb	hjeffrey	Accepted	1	0	1	1	0	0
