﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
14334	Queries don't ensure that comparison objects are the correct type	Randy Barlow	ANUBHAV JOSHI	"This is best demonstrated by an example. Suppose you have the following models:

{{{
class A(models.Model):
    pass

class B(models.Model):
    pass

class C(models.Model):
    b = models.ForeignKey(B)
}}}

Then suppose you did this:

{{{
        a = models.A.objects.create()
        b = models.B.objects.create()
        # This works, because the following assertion is True
        self.assertEquals(a.id, b.id)
        c = models.C.objects.create(b=b)

        # Let's try to query for a c where b==b. Works as expected
        self.assertEquals(models.C.objects.get(b=b).b, b)

        # Uh oh, why can I query for b=a? This first one shouldn't pass
        self.assertEquals(models.C.objects.get(b=a).b, b)
        # This one should fail, but it should fail at the .get() by
        # raising an exception, I think
        self.assertEquals(models.C.objects.get(b=a).b, a)
}}}

My proposal is that Django should complain when I pass in an instance of A for comparison with an FK to B, since a isn't the same as b. The query seems to just compare their primary keys, and not also ensure that the types are what is expected.

I've created a small django project that demonstrates this problem."	Bug	closed	Database layer (models, ORM)	dev	Normal	fixed			Ready for checkin	1	0	0	0	0	0
