Ticket #11226: 11226-auto_m2m_table_fk_names_clash.diff

File 11226-auto_m2m_table_fk_names_clash.diff, 2.7 KB (added by Ramiro Morales, 15 years ago)

Fix by pkoch plus a (simple) test as suggested by Russell

  • django/db/models/fields/related.py

    diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py
    a b  
    815815                    if hasattr(f,'rel') and f.rel and f.rel.to == related.model:
    816816                        self._m2m_column_name_cache = f.column
    817817                        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:
    820821                self._m2m_column_name_cache = 'from_' + related.model._meta.object_name.lower() + '_id'
    821822            else:
    822823                self._m2m_column_name_cache = related.model._meta.object_name.lower() + '_id'
     
    846847                        else:
    847848                            self._m2m_reverse_name_cache = f.column
    848849                            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:
    851853                self._m2m_reverse_name_cache = 'to_' + related.parent_model._meta.object_name.lower() + '_id'
    852854            else:
    853855                self._m2m_reverse_name_cache = related.parent_model._meta.object_name.lower() + '_id'
  • tests/regressiontests/m2m_regress/models.py

    diff --git a/tests/regressiontests/m2m_regress/models.py b/tests/regressiontests/m2m_regress/models.py
    a b  
    11from django.db import models
     2from django.contrib import auth
    23
    34# No related name is needed here, since symmetrical relations are not
    45# explicitly reversible.
     
    3334class SelfReferChildSibling(SelfRefer):
    3435    pass
    3536
     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)
     40class User(models.Model):
     41    name = models.CharField(max_length=30)
     42    friends = models.ManyToManyField(auth.models.User)
     43
    3644__test__ = {"regressions": """
    3745# Multiple m2m references to the same model or a different model must be
    3846# distinguished when accessing the relations through an instance attribute.
Back to Top