#15164: Filtering queryset via ForeignKey to legacy model with OneToOneField as
primary doesn't evaluate primary key
------------------------------------------+---------------------------------
 Reporter:  GDorn                         |       Owner:  nobody    
   Status:  new                           |   Milestone:            
Component:  Database layer (models, ORM)  |     Version:  1.2       
 Keywords:                                |       Stage:  Unreviewed
Has_patch:  0                             |  
------------------------------------------+---------------------------------
 If you have one model with a OneToOneField as primary_key and also specify
 a db_column and another model with a foreign key to the first, like this:
 {{{
 class UserProfile(models.Model):
     user = models.OneToOneField(User, primary_key=True, db_column='uid')
     email = models.CharField(max_length=255, unique=True)
     username = models.CharField(unique=True, max_length=150)
     class Meta:
         db_table = u'legacy_user'

 class OtherModel(models.Model):
     user = models.ForeignKey('my_app.UserProfile', db_column='uid')
     some_data = models.IntegerField()
     another_model = models.ForeignKey('other_app.AnotherModel',
 db_column='related')
     class Meta:
         db_table = u'legacy_other_model'
 }}}

 When you perform this queryset you get weird SQL:

 {{{
 my_user = UserProfile.objects.get(username='foo')
 count = OtherModel.objects.filter(user=my_user).count()
 SELECT COUNT(*) FROM `legacy_other_model` WHERE `legacy_other_model`.`uid`
 = None
 }}}

 But if you explicitly filter on the object's pk, it works:

 {{{
 count = OtherModel.objects.filter(user=my_user.pk).count()
 SELECT COUNT(*) FROM `legacy_other_model` WHERE `legacy_other_model`.`uid`
 = 12345
 }}}

 This doesn't seem to be the expected behavior, looking at:
 http://docs.djangoproject.com/en/dev/topics/db/queries/#queries-over-
 related-objects

-- 
Ticket URL: <http://code.djangoproject.com/ticket/15164>
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 django-updates@googlegroups.com.
To unsubscribe from this group, send email to 
django-updates+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to