Sunday, May 20, 2012

Re: Foreignkeys on multiple fields

On Mon, 21 May 2012 05:58:27 +0530, Vikas Rawal
<vikaslists@agrarianresearch.org> declaimed the following in
gmane.comp.python.django.user:


> I understand this (at least the basics). I do not particularly like that django introduces the limitation of having a single primary key variable when the database backend does not have such a limitation. I was thinking that I could have django have its integer primary key, but do foreignkey lookups on different variables. I now understand that I was wrong in doing so.
>
The problem is that you are trying to use two /independent/ foreign
keys to represent one unique entity. That won't work. The "unique
together" clause essentially builds an /index/, in that table, that is
composed of the two fields concatenated, and ensures that you don't have
duplicate records in which those fields are completely identical.

In the table that makes the reference, the foreign-key field has to
be a /single/ entity that, by itself, identifies the exact record of the
foreign table that is related. As a result, an integer primary key is
the most natural, and recommended, entity to use.





> This is indeed right. But I would like my second table to have two variables, Village and HouseholdNumber, whose values it directly gets from the first table. I know that I could obtain these through a select query as you have illustrated above. But what I am trying to do is that,when I fill data in an inline formset connected to second table, the table should automatically get these two variables based on the relationship you have used in the query above.
>

A proper foreign key relationship (using a single reference to the
primary key of /the/ record with Village/Household) will let you obtain
those values. In Django terms, that may be a ".select_related()" call.

However, if you want an unnormalized table -- in which you ARE
storing the village/household for each individual -- you will likely
have to code some sort of pre-save operation to save the parts INSTEAD
OF a foreign key relationship. I'm not that familiar with Django's
syntax itself (or any object-relational mapping system). In something
like Access, what you are defining is a "lookup-field" which /displays/
a dropdown list of "village | household" pairs but /stores/ the primary
key for the selected pair.
--
Wulfraed Dennis Lee Bieber AF6VN
wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/

--
You received this message because you are subscribed to the Google Groups "Django users" group.
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.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate