how to refer a foreign key to a non primary column from other model

4,108 views
Skip to first unread message

lokeshm...@gmail.com

unread,
May 16, 2009, 9:46:28 AM5/16/09
to Django users
Hi All,

Is it possible to create a foreign key to a model referring to a non
primary column from other model.

class details(models.Model):
username = ????????

?????? -> how I can refer this field as a foreign key to username
column which is from auth_user table.

Thanks in advance.

Regards,
Lokesh

Alex Gaynor

unread,
May 16, 2009, 10:15:52 AM5/16/09
to django...@googlegroups.com
You can do this using the "to_field" option as documented here: http://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.ForeignKey.to_field

Alex

--
"I disapprove of what you say, but I will defend to the death your right to say it." --Voltaire
"The people's good is the highest law."--Cicero

lokeshm...@gmail.com

unread,
May 16, 2009, 10:25:05 AM5/16/09
to Django users
Hi All,

Need help in creating foreign key column in a table.

In the below model I would like to create a foreign key for the field
'new_foreign_col' which should refer to 'username' column (non primary
column) from auth_user table.

class details(models.Model):
new_foreign_col = ????????

Joshua Russo

unread,
May 16, 2009, 10:30:36 AM5/16/09
to Django users
Foreign keys only point to primary key values. That's just how
relational DBs work.

It looks like you just want to link details to a user. Is that
correct?

lokeshm...@gmail.com

unread,
May 16, 2009, 10:38:23 AM5/16/09
to Django users
Hi joshua,

Yes, I just want to link username to details table.

Regards,
Lokesh

On May 16, 7:30 pm, Joshua Russo <joshua.rupp...@gmail.com> wrote:
> Foreign keys only point to primary key values. That's just how
> relational DBs work
>

Joshua Russo

unread,
May 16, 2009, 10:52:24 AM5/16/09
to Django users
You want to link to the id field in the auth_user table. Try the
following:

from django.contrib.auth.models import User

class details(models.Model):
user = models.ForeignKey(User)

Joshua Russo

unread,
May 16, 2009, 11:03:57 AM5/16/09
to Django users
I should probably explain that better. Inside the DB foreign keys can
only be made between a field in a child/related table to a primary key
field. Generally the primary key fields don't change as often, though
you can change them just like any other field it's highly
unrecommended.

If you try to change a primary key value that already has other
records pointing to it via foreign keys you will generally get an
error. There are ways to setup the DB to cascade updates made to
primary keys to the foreign keys but the functionality is not
universal to all types of DBs and has it's own limitations.

Does this make sense?

Alex Gaynor

unread,
May 16, 2009, 11:13:31 AM5/16/09
to django...@googlegroups.com
This is simply not true, you can have foreign keys to non primary key fields.  As a linked the "to_field" option allows you to do just that.

Joshua Russo

unread,
May 16, 2009, 12:04:39 PM5/16/09
to Django users
Ok, apologize. I just tried it myself and MySQL did accept the alter
table. It is certainly not an operation that I have been able to
perform in MS SQL Server or Oracle, though I wonder if it's something
that is changing industry wide. MySQL seems to allow a foreign key to
any unique index.

Joshua Russo

unread,
May 16, 2009, 1:10:53 PM5/16/09
to Django users
"I apologize" that is

Antoni Aloy

unread,
May 16, 2009, 5:08:54 PM5/16/09
to django...@googlegroups.com
2009/5/16 <lokeshm...@gmail.com>:

>
> Hi All,
>
> Need help in creating foreign key column in a table.
>
> In the below model I would like to create a foreign key for the field
> 'new_foreign_col' which should refer to 'username' column (non primary
> column) from auth_user table.
>
> class details(models.Model):
>   new_foreign_col = ????????
>

http://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.ForeignKey
Check: ForeignKey.to_field

Hope it helps!

--
Antoni Aloy López
Blog: http://trespams.com
Site: http://apsl.net

PeterN

unread,
May 17, 2009, 6:53:01 AM5/17/09
to Django users
This is not true for Oracle either.
A foreign key can certainly reference a unique (not primary) key in
Oracle.
See Oracle documentation, e.g.
http://download.oracle.com/docs/cd/B28359_01/server.111/b28318/data_int.htm#sthref2329,
or simply verify by trying it !

-- PeterN
Reply all
Reply to author
Forward
0 new messages