Django

Code

Ticket #7214 (closed: duplicate)

Opened 4 months ago

Last modified 4 months ago

Bug in OneToOneField cache

Reported by: m.gajda@paranoja.pl Assigned to: nobody
Milestone: Component: Core framework
Version: SVN Keywords: one to one cache bug
Cc: Triage Stage: Unreviewed
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

Description

There is a fatal bug in OneToOneField? cacheing subsystem. Consider following models:

class A( models.Model ):
  foo = models.IntegerField()

  def __unicode__( self ):
    return "%d" % self.foo

class B( A ):
    a = models.OneToOneField( A , related_name = "b" , parent_link = True )

class C( A ):
    a = models.OneToOneField( A , related_name = "c" , parent_link = True )

> B(foo=1).save()

> C(foo=2).save()

> A.objects.all()
[<A: 1>, <A: 2>]

> a = A.objects.filter( foo = 1 )[0]

> a.b
<B: 1>

> a.c
<B: 1>

In both cases (b and c fields pointing to different subclasses) is returns the same object, but in the second case an exception should be throwed.

The reason is cacheing subsystem in SingleRelatedObjectDescriptor? class of the module django.db.models.fields.related. In the constructor it creates twice cache with the same so called cache name: self.cache_name = '_%s_cache' % related.field.name because related.field.name points to the same string in both cases. In my opinion it should point to different strings. After aplying attached patch, which uses just related.name instead of related.field.name everything seems to work correctly (cache still works but separately for both fields).

> A.objects.all()
[<A: 1>, <A: 2>]

> a = A.objects.filter( foo = 1 )[0]

> a.b
<B: 1>

> a.c
<class 'megasite.test.models.DoesNotExist'>: C matching query does not exist.

Attachments

a (0.5 kB) - added by m.gajda@paranoja.pl on 05/10/08 16:58:41.

Change History

05/10/08 16:58:41 changed by m.gajda@paranoja.pl

  • attachment a added.

05/11/08 21:56:04 changed by gav

  • needs_better_patch changed.
  • needs_tests changed.
  • needs_docs changed.

I believe this is a dupe of #7173. I also believe that the patch on #7173 is a better solution, as it's using the proper function from the RelatedField?, get_accessor_name(). Recommending for resolution of duplicate.

05/12/08 05:19:51 changed by ubernostrum

  • status changed from new to closed.
  • resolution set to duplicate.

Yup, it's a dupe of #7173.


Add/Change #7214 (Bug in OneToOneField cache)




Change Properties
Action