user confused with select_related and a one_to_one field
I'm using python 2.4 and django 1.2.4 with MySQL 5.0 FWIW I have the following models (code deleted to simplify) class Message(models.Model): facility= models.CharField(max_length=2, null=True, blank=True) msgtype = models.CharField(max_length=3) class Pv(models.Model): message = models.OneToOneField(Message, primary_key=True, db_column='id') location= models.CharField(max_length=6, null=True, blank=True) priorfacility = models.CharField(max_length=2, null=True, blank=True) Now I've discovered that on rare occasions Pv does not exists and thus referencing it like this for message in models.Message.objects.select_related(): new = [message.pv.facility, ... throws django.db.models.base.DoesNotExist: Pv matching query does not exist. When I encounter a missing PV record. Is this my improper use of OneToOneField and I should have used ForeignKeyField ? Is there a way to see if the PV object exists other than surrounding it with a try/except clause ? The times when Pv is missing are rare; would it make more sense to just create a record with nulls and maintain the 1-to-1 relationship? -- 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+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: Confused by select_related()
On Thu, Oct 23, 2008 at 11:21 AM, AndyB <[EMAIL PROTECTED]> wrote: > > Apologies for not using DPaste. I thought the snippets were small > enough. And I thought my question was cleared than it turned out to > be! > I wasn't saying what you posted would have been better at dpaste.com, what you posted was fine. I was saying your model definitions might work better at dpaste.com than inline. > To clarify (where someobject_set is a related-object queryset): > > someobject_set.all().query.as_sql() > someobject_set.select_related().query.as_sql() > > Both show identical SQL is being generated - which indicates that > select_related() isn't following any relationships. > What select_related does, exactly, is dependent on the model definitions. It doesn't follow relations which can be null, for example. Which is why I asked for your model definitions. You're also applying the select_related to a related set, which may be coming into play (this is not code I'm terribly familiar with), but when I try analogs to your queries on my own models, I do get different sql reflecting select_related is having an effect. Which is why I asked for your model definitions. Without seeing your models I haven't the vaguest idea how what you are doing with your models is different from what I am doing. Karen --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Confused by select_related()
Apologies for not using DPaste. I thought the snippets were small enough. And I thought my question was cleared than it turned out to be! To clarify (where someobject_set is a related-object queryset): someobject_set.all().query.as_sql() someobject_set.select_related().query.as_sql() Both show identical SQL is being generated - which indicates that select_related() isn't following any relationships. One of the fields is a foreign key to another so I would have expect the second query to result in a join. (incidentally - the problem I am trying to solve is to sort by a field that is in a related table but that question can wait until I can understand the reason for the above issue) --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Confused by select_related()
On Thu, Oct 23, 2008 at 10:44 AM, AndyB <[EMAIL PROTECTED]> wrote: > > >>> p.residentialunitmix_set.all().query.as_sql() > > (u'SELECT `tbl_ResidentialUnitMix`.`id`, > `tbl_ResidentialUnitMix`.`Planning-id`, > `tbl_ResidentialUnitMix`.`quantity`, > `tbl_ResidentialUnitMix`.`Bedrooms`, `tbl_ResidentialUnitMix`.`Type`, > `tbl_ResidentialUnitMix`.`Tenure` FROM `tbl_ResidentialUnitMix` WHERE > `tbl_ResidentialUnitMix`.`Planning-id` = %s ' , (82)) > > >>> p.residentialunitmix_set.select_related().query.as_sql() > > (u'SELECT `tbl_ResidentialUnitMix`.`id`, > `tbl_ResidentialUnitMix`.`Planning-id`, > `tbl_ResidentialUnitMix`.`quantity`, > `tbl_ResidentialUnitMix`.`Bedrooms`, `tbl_ResidentialUnitMix`.`Type`, > `tbl_ResidentialUnitMix`.`Tenure` FROM `tbl_ResidentialUnitMix` WHERE > `tbl_ResidentialUnitMix`.`Planning-id` = %s ' , (82)) > > Tenure is a foreign key to another tables. How come both queries are > identical? > > I am trying to sort by a field in the Tenure table and: > > residentialunitmix_set.select_related().order_by('tbl_lkp_ResidentialUnitMix_Tenure.sort') > > just gives an error. Looking at the queries explained why. > > (Apologies for the table names. It's a Django interface to something > developed in Access) > You've rather jumped to the end problem without telling us the beginning (model definitions). Now it's maybe possible to puzzle out what's going on based on the queries alone, but why make it so hard on us? Ideally you'd whittle it down to a minimal example that illustrates the problem you are seeing (which by the way, you should spell out in as great a detail as is presented to you -- "just gives an error" is about as useless a description of a problem as "doesn't work"), but if you can't manage that at least post the models you're actually working with. On dpaste.com, not inline in the post, if they are big/involved enough that the email//groups interface will make them hard to read. Karen --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Confused by select_related()
>>> p.residentialunitmix_set.all().query.as_sql() (u'SELECT `tbl_ResidentialUnitMix`.`id`, `tbl_ResidentialUnitMix`.`Planning-id`, `tbl_ResidentialUnitMix`.`quantity`, `tbl_ResidentialUnitMix`.`Bedrooms`, `tbl_ResidentialUnitMix`.`Type`, `tbl_ResidentialUnitMix`.`Tenure` FROM `tbl_ResidentialUnitMix` WHERE `tbl_ResidentialUnitMix`.`Planning-id` = %s ' , (82)) >>> p.residentialunitmix_set.select_related().query.as_sql() (u'SELECT `tbl_ResidentialUnitMix`.`id`, `tbl_ResidentialUnitMix`.`Planning-id`, `tbl_ResidentialUnitMix`.`quantity`, `tbl_ResidentialUnitMix`.`Bedrooms`, `tbl_ResidentialUnitMix`.`Type`, `tbl_ResidentialUnitMix`.`Tenure` FROM `tbl_ResidentialUnitMix` WHERE `tbl_ResidentialUnitMix`.`Planning-id` = %s ' , (82)) Tenure is a foreign key to another tables. How come both queries are identical? I am trying to sort by a field in the Tenure table and: residentialunitmix_set.select_related().order_by('tbl_lkp_ResidentialUnitMix_Tenure.sort') just gives an error. Looking at the queries explained why. (Apologies for the table names. It's a Django interface to something developed in Access) --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---