diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py
a
|
b
|
|
815 | 815 | if hasattr(f,'rel') and f.rel and f.rel.to == related.model: |
816 | 816 | self._m2m_column_name_cache = f.column |
817 | 817 | break |
818 | | # If this is an m2m relation to self, avoid the inevitable name clash |
819 | | elif related.model == related.parent_model: |
| 818 | # If this is an m2m relation to self, avoid the inevitable name clash, |
| 819 | # this also happens when the related models have the same name |
| 820 | elif related.model._meta.object_name == related.parent_model._meta.object_name: |
820 | 821 | self._m2m_column_name_cache = 'from_' + related.model._meta.object_name.lower() + '_id' |
821 | 822 | else: |
822 | 823 | self._m2m_column_name_cache = related.model._meta.object_name.lower() + '_id' |
… |
… |
|
846 | 847 | else: |
847 | 848 | self._m2m_reverse_name_cache = f.column |
848 | 849 | break |
849 | | # If this is an m2m relation to self, avoid the inevitable name clash |
850 | | elif related.model == related.parent_model: |
| 850 | # If this is an m2m relation to self, avoid the inevitable name clash, |
| 851 | # this also happens when the related models have the same name |
| 852 | elif related.model._meta.object_name == related.parent_model._meta.object_name: |
851 | 853 | self._m2m_reverse_name_cache = 'to_' + related.parent_model._meta.object_name.lower() + '_id' |
852 | 854 | else: |
853 | 855 | self._m2m_reverse_name_cache = related.parent_model._meta.object_name.lower() + '_id' |
diff --git a/tests/regressiontests/m2m_regress/models.py b/tests/regressiontests/m2m_regress/models.py
a
|
b
|
|
1 | 1 | from django.db import models |
| 2 | from django.contrib import auth |
2 | 3 | |
3 | 4 | # No related name is needed here, since symmetrical relations are not |
4 | 5 | # explicitly reversible. |
… |
… |
|
33 | 34 | class SelfReferChildSibling(SelfRefer): |
34 | 35 | pass |
35 | 36 | |
| 37 | # A model with the same name that another one to which it has a m2m relation, |
| 38 | # This shouldn't cause a name clash between the automatically created m2m |
| 39 | # intermediary table FK field names when running syncdb (#11226) |
| 40 | class User(models.Model): |
| 41 | name = models.CharField(max_length=30) |
| 42 | friends = models.ManyToManyField(auth.models.User) |
| 43 | |
36 | 44 | __test__ = {"regressions": """ |
37 | 45 | # Multiple m2m references to the same model or a different model must be |
38 | 46 | # distinguished when accessing the relations through an instance attribute. |