#15513: CharField ForeignKey forced to int during lookup in get_prep_value in
django.db.models.fields
--------------------------------------------------------+-------------------
Reporter: harijay | Owner: nobody
Status: closed | Milestone:
Component: Database layer (models, ORM) | Version: SVN
Resolution: invalid | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 |
--------------------------------------------------------+-------------------
Changes (by kmtracey):
* status: new => closed
* needs_better_patch: => 0
* resolution: => invalid
* needs_tests: => 0
* needs_docs: => 0
Comment:
The cause of the exception you are seeing is that per your Django models,
the target column of the !ForeignKey field in your Child model is the
primary key field of Parent, not the ssn field. Primary key field of
parent is an integer, so attempting to lookup a non-integer value raises
an exception.
In order to tell Django that the target column for the !ForeignKey field
in child is the parent's ssn field, you need to specify to_field='ssn' on
that !ForeignKey definition. See:
http://docs.djangoproject.com/en/1.2/ref/models/fields/#django.db.models.ForeignKey.to_field
Note though that your existing table definitions don't meet the
requirements for a !ForeignKey field here, because the Parent ssn field is
not unique. Django's !ForeignKey is a many-to-one relation, so the target
column must be unique. See #11702. If you are not actually creating the
tables via syncdb you may not initially see any error related to this
failure to meet the requirements for a !ForeignKey -- but if in fact you
have duplicated values in that target field, you may see errors later on.
Your Django models also show evidence of bug #5725. The max_length values
for all your !CharFields are 3x higher than they should be. Easiest fix is
to manually correct them to be the right value.
--
Ticket URL: <http://code.djangoproject.com/ticket/15513#comment:1>
Django <http://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-updates?hl=en.