#8279: Models with multiple ManyToManyField to 'self' use the last table for
every
m2m
-------------------------------------------------------------+--------------
Reporter: Alberto GarcĂa Hierro <[EMAIL PROTECTED]> |
Owner: nobody
Status: new | Milestone:
1.0
Component: Database wrapper | Version:
SVN
Resolution: | Keywords:
Stage: Accepted | Has_patch:
0
Needs_docs: 0 | Needs_tests:
0
Needs_better_patch: 0 |
-------------------------------------------------------------+--------------
Old description:
> class Entry(models.Model):
> more_fields
> related = models.ManyToManyField('self', verbose_name=_('related
> entries'))
> translations = models.ManyToManyField('self',
> verbose_name=_('translations'))
>
> From the database (postgres):
>
> blango=# select * from blango_entry_related;
> id | from_entry_id | to_entry_id
> ----+---------------+-------------
> 1 | 27 | 29
> 2 | 29 | 27
> 3 | 27 | 30
> 4 | 30 | 27
> 5 | 27 | 38
> 6 | 38 | 27
> (6 rows)
>
> blango=# select * from blango_entry_translations;
> id | from_entry_id | to_entry_id
> ----+---------------+-------------
> 3 | 4 | 3
> 4 | 3 | 4
> 7 | 8 | 7
> 8 | 7 | 8
> 11 | 13 | 12
> 12 | 12 | 13
> (6 rows)
>
> blango=#
>
> And from ./manage.py shell:
> In [2]: from blango.models import
> *;Entry.objects.get(pk=27).related.all()
> Out[2]: []
> In [3]: from blango.models import
> *;Entry.objects.get(pk=27).translations.all()
> Out[3]: []
>
> The database query log indicates that Django is using the wrong table:
> 2008-08-12 22:38:10 CEST LOG: statement: SELECT "blango_entry"."id",
> "blango_entry"."title", "blango_entry"."slug",
> "blango_entry"."author_id", "blango_entry"."language_id",
> "blango_entry"."body", "blango_entry"."body_html",
> "blango_entry"."pub_date", "blango_entry"."draft",
> "blango_entry"."allow_comments" FROM "blango_entry" INNER JOIN
> "blango_entry_translations" ON ("blango_entry"."id" =
> "blango_entry_translations"."to_entry_id") WHERE
> "blango_entry_translations"."from_entry_id" = 27
>
> If I change the order of the m2m in the model to:
>
> class Entry(models.Model):
> more_fields
> translations = models.ManyToManyField('self',
> verbose_name=_('translations'))
> related = models.ManyToManyField('self', verbose_name=_('related
> entries'))
>
> Then I get all the related entries as related and as translations.
>
> If it serves as help, I've added a print statement in
> django/db/models/fields/related.py at line 776:
>
> ...
> 775 else:
> 776 print '%s_%s' % (opts.db_table, self.name)
> 777 return '%s_%s' % (opts.db_table, self.name)
> ...
>
> Which prints:
>
> In [1]: from blango.models import
> *;Entry.objects.get(pk=27).related.all()
> blango_entry_related
> blango_entry_translations
> Out[1]: []
>
> In [2]: from blango.models import
> *;Entry.objects.get(pk=27).related.all()
> blango_entry_related
> Out[2]: []
New description:
{{{
#!python
class Entry(models.Model):
more_fields
related = models.ManyToManyField('self', verbose_name=_('related
entries'))
translations = models.ManyToManyField('self',
verbose_name=_('translations'))
}}}
From the database (postgres):
{{{
blango=# select * from blango_entry_related;
id | from_entry_id | to_entry_id
----+---------------+-------------
1 | 27 | 29
2 | 29 | 27
3 | 27 | 30
4 | 30 | 27
5 | 27 | 38
6 | 38 | 27
(6 rows)
blango=# select * from blango_entry_translations;
id | from_entry_id | to_entry_id
----+---------------+-------------
3 | 4 | 3
4 | 3 | 4
7 | 8 | 7
8 | 7 | 8
11 | 13 | 12
12 | 12 | 13
(6 rows)
blango=#
}}}
And from ./manage.py shell:
{{{
In [2]: from blango.models import *;Entry.objects.get(pk=27).related.all()
Out[2]: []
In [3]: from blango.models import
*;Entry.objects.get(pk=27).translations.all()
Out[3]: []
}}}
The database query log indicates that Django is using the wrong table:
{{{
2008-08-12 22:38:10 CEST LOG: statement: SELECT "blango_entry"."id",
"blango_entry"."title", "blango_entry"."slug", "blango_entry"."author_id",
"blango_entry"."language_id", "blango_entry"."body",
"blango_entry"."body_html", "blango_entry"."pub_date",
"blango_entry"."draft", "blango_entry"."allow_comments" FROM
"blango_entry" INNER JOIN "blango_entry_translations" ON
("blango_entry"."id" = "blango_entry_translations"."to_entry_id") WHERE
"blango_entry_translations"."from_entry_id" = 27
}}}
If I change the order of the m2m in the model to:
{{{
#!python
class Entry(models.Model):
more_fields
translations = models.ManyToManyField('self',
verbose_name=_('translations'))
related = models.ManyToManyField('self', verbose_name=_('related
entries'))
}}}
Then I get all the related entries as related and as translations.
If it serves as help, I've added a print statement in
django/db/models/fields/related.py at line 776:
{{{
...
775 else:
776 print '%s_%s' % (opts.db_table, self.name)
777 return '%s_%s' % (opts.db_table, self.name)
...
}}}
Which prints:
{{{
In [1]: from blango.models import *;Entry.objects.get(pk=27).related.all()
blango_entry_related
blango_entry_translations
Out[1]: []
In [2]: from blango.models import *;Entry.objects.get(pk=27).related.all()
blango_entry_related
Out[2]: []
}}}
Comment (by mtredinnick):
(Fixed description formatting).
--
Ticket URL: <http://code.djangoproject.com/ticket/8279#comment:7>
Django Code <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 [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---