#25027: two relations on the same model with same related_name break the model
-------------------------------+--------------------------------------
     Reporter:  karyon         |                    Owner:  nobody
         Type:  Bug            |                   Status:  new
    Component:  Uncategorized  |                  Version:  1.8
     Severity:  Normal         |               Resolution:
     Keywords:                 |             Triage Stage:  Unreviewed
    Has patch:  0              |      Needs documentation:  0
  Needs tests:  0              |  Patch needs improvement:  0
Easy pickings:  0              |                    UI/UX:  0
-------------------------------+--------------------------------------
Description changed by karyon:

Old description:

> consider the following models:
> {{{
> class UserProfile(models.Model):
>     pass
>
> class Course(models.Model):
>     participants = models.ManyToManyField(UserProfile,
> related_name='foo')
>
> class Document(models.Model):
>     last_modified_user = models.ForeignKey(UserProfile,
> related_name='foo')
> }}}
>
> and the following query:
> {{{
> >>> str(Course.objects.first().participants.all().query)
> 'SELECT "testapp_userprofile"."id" FROM "testapp_userprofile" INNER JOIN
> "testapp_document" ON ( "testapp_userprofile"."id" =
> "testapp_document"."last_modified_user_id" ) WHERE
> "testapp_document"."id" = 1'
> }}}
>
> the join doesn't make any sense to me. it makes both assertions in the
> following test fail:
>
> {{{
> def test_related_name(self):
>     user = UserProfile.objects.create()
>     course = Course.objects.create()
>     course.participants = UserProfile.objects.all()
>     self.assertEqual(course.participants.count(), 1)
>     self.assertEqual(list(course.participants.all()), [user])
> }}}
>

> changing the related name in one of the models to something else fixes
> this.
>
> i would have expected to get some warning that i'm using the same related
> name on the same model. especially i expected this to not break when i'm
> not even using the related name in the test.
>
> this also breaks when using '+' as related name. in this case, i would
> expect everything to just work.

New description:

 consider the following models:
 {{{
 class UserProfile(models.Model):
     pass

 class Course(models.Model):
     participants = models.ManyToManyField(UserProfile, related_name='+')

 class Document(models.Model):
     last_modified_user = models.ForeignKey(UserProfile, related_name='+')
 }}}

 and the following query:
 {{{
 >>> str(Course.objects.first().participants.all().query)
 'SELECT "testapp_userprofile"."id" FROM "testapp_userprofile" INNER JOIN
 "testapp_document" ON ( "testapp_userprofile"."id" =
 "testapp_document"."last_modified_user_id" ) WHERE "testapp_document"."id"
 = 1'
 }}}

 the join doesn't make any sense to me. it makes both assertions in the
 following test fail:

 {{{
 def test_related_name(self):
     user = UserProfile.objects.create()
     course = Course.objects.create()
     course.participants = UserProfile.objects.all()
     self.assertEqual(course.participants.count(), 1)
     self.assertEqual(list(course.participants.all()), [user])
 }}}


 changing the related name in one of the models to something else fixes
 this.

 if using a related name other than '+', the test still fails as before and
 the check command does not complain either, but at least makemigrations
 will spot the mistake.

--

--
Ticket URL: <https://code.djangoproject.com/ticket/25027#comment:2>
Django <https://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 unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/064.69fe72c6b6012bf6f5ef6e5b9f06d9f9%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to