Newbie -- ForeignKey to auth_user?

2,456 views
Skip to first unread message

Chris Stromberger

unread,
Sep 11, 2008, 5:01:37 PM9/11/08
to django...@googlegroups.com
I would like to include a foreign key in a table that links to a user in Django's auth_user table.  Or maybe this is a dumb idea--if so, interested in hearing why.

So the table ("restaurant") with the foreign key includes (mysql):

staff_id int(11) NOT NULL,
foreign key(staff_id) references auth_user(id) on delete no action on update cascade,

If I include this in my model:

from django.contrib.auth.models import User
staff_id = models.ForeignKey(User, db_column = 'id')

it seems to screw things up.  If I start a python shell, import my model, then run Restaurant.objects.all(), I get

OperationalError: (1054, "Unknown column 'restaurant.id' in 'field list'")

Not sure what is going on.  The model has an explicit primary key defined (this is a legacy db):

  restaurant_id = models.AutoField(primary_key = True)

Thanks for any help,
Chris

Malcolm Tredinnick

unread,
Sep 11, 2008, 5:09:30 PM9/11/08
to django...@googlegroups.com

On Thu, 2008-09-11 at 16:01 -0500, Chris Stromberger wrote:
> I would like to include a foreign key in a table that links to a user
> in Django's auth_user table. Or maybe this is a dumb idea--if so,
> interested in hearing why.
>
>
> So the table ("restaurant") with the foreign key includes (mysql):
>
>
> staff_id int(11) NOT NULL,
> foreign key(staff_id) references auth_user(id) on delete no action on
> update cascade,
>
>
>
> If I include this in my model:
>
>
>
> from django.contrib.auth.models import User
>
> staff_id = models.ForeignKey(User, db_column = 'id')

This probably isn't what you inteded to write. The db_column attribute
specifies what the name of the database column in *this* table will be
called. The name of the column in the table it refers to is worked out
automatically (since it's almost always the primary key of that table
and for other cases, Django has the to_field attribute).

Regards,
Malcolm

>

Chris Stromberger

unread,
Sep 11, 2008, 5:20:33 PM9/11/08
to django...@googlegroups.com
Oh, thanks.  Ok, I just tried taking that out (so model now says "staff_id = models.ForeignKey(User)"), but that gave this error:

OperationalError: (1054, "Unknown column 'restaurant.staff_id_id' in 'field list'")

?

Chris Stromberger

unread,
Sep 11, 2008, 5:24:20 PM9/11/08
to django...@googlegroups.com
And if I comment out the staff_id line in the model class, my test in the console works fine (Restaurant.objects.all()).

Karen Tracey

unread,
Sep 11, 2008, 5:38:50 PM9/11/08
to django...@googlegroups.com
On Thu, Sep 11, 2008 at 5:20 PM, Chris Stromberger <chris.st...@gmail.com> wrote:
Oh, thanks.  Ok, I just tried taking that out (so model now says "staff_id = models.ForeignKey(User)"), but that gave this error:

OperationalError: (1054, "Unknown column 'restaurant.staff_id_id' in 'field list'")

?

Try:

staff  = models.ForeignKey(User)

Django automatically adds _id to the database field name for ForeignKeys unless you specify db_column, as described in the paragraph that begins "Behind the scenes" below here:

http://docs.djangoproject.com/en/dev/ref/models/fields/#foreignkey

So if your field is actually named 'staff_id' either declare it as 'staff' and do not specify db_column or declare it as whatever you want and specify db_column = 'staff_id'.

Karen

Chris Stromberger

unread,
Sep 11, 2008, 5:42:13 PM9/11/08
to django...@googlegroups.com
That works!  Thanks for the help.
Reply all
Reply to author
Forward
0 new messages