diff options
| author | Berker Peksag <berker.peksag@gmail.com> | 2016-06-03 12:55:30 -0700 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2016-06-10 10:57:22 -0400 |
| commit | 0bce2f102c5734d0c7ff1ebf0b10a316d58ed5ce (patch) | |
| tree | 54a2a9d3088e8d70255a4aa99dbc255bb2bfa81e /django | |
| parent | faeeb84edfebecf5a5f40df9ef816e5f1cd457c6 (diff) | |
Fixed #12810 -- Added a check for clashing ManyToManyField.db_table names.
Diffstat (limited to 'django')
| -rw-r--r-- | django/db/models/fields/related.py | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py index 4b8b1804b5..0639bf3d79 100644 --- a/django/db/models/fields/related.py +++ b/django/db/models/fields/related.py @@ -1194,6 +1194,7 @@ class ManyToManyField(RelatedField): errors.extend(self._check_unique(**kwargs)) errors.extend(self._check_relationship_model(**kwargs)) errors.extend(self._check_ignored_options(**kwargs)) + errors.extend(self._check_table_uniqueness(**kwargs)) return errors def _check_unique(self, **kwargs): @@ -1429,6 +1430,36 @@ class ManyToManyField(RelatedField): return errors + def _check_table_uniqueness(self, **kwargs): + if isinstance(self.remote_field.through, six.string_types): + return [] + registered_tables = { + model._meta.db_table: model + for model in self.opts.apps.get_models(include_auto_created=True) + if model != self.remote_field.through + } + m2m_db_table = self.m2m_db_table() + if m2m_db_table in registered_tables: + model = registered_tables[m2m_db_table] + if model._meta.auto_created: + def _get_field_name(model): + for field in model._meta.auto_created._meta.many_to_many: + if field.remote_field.through is model: + return field.name + opts = model._meta.auto_created._meta + clashing_obj = '%s.%s' % (opts.label, _get_field_name(model)) + else: + clashing_obj = '%s' % model._meta.label + return [ + checks.Error( + "The field's intermediary table '%s' clashes with the " + "table name of '%s'." % (m2m_db_table, clashing_obj), + obj=self, + id='fields.E340', + ) + ] + return [] + def deconstruct(self): name, path, args, kwargs = super(ManyToManyField, self).deconstruct() # Handle the simpler arguments. |
