Wednesday, November 28, 2012

Re: Newbie -- ForeignKey to auth_user?

Great thank about this!

On Friday, September 12, 2008 5:38:50 AM UTC+8, Karen Tracey wrote:
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



On Thu, Sep 11, 2008 at 4:09 PM, Malcolm Tredinnick <mal...@pointy-stick.com> wrote:


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

>







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

No comments:

Post a Comment