mirror of
https://github.com/django/django.git
synced 2025-03-12 10:22:37 +00:00
Fixed #33823 -- Made inspectdb generate unique related_name when reverse accessor clashes.
This commit is contained in:
parent
877c800f25
commit
3926e35aa8
@ -127,12 +127,14 @@ class Command(BaseCommand):
|
|||||||
yield "# The error was: %s" % e
|
yield "# The error was: %s" % e
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
model_name = table2model(table_name)
|
||||||
yield ""
|
yield ""
|
||||||
yield ""
|
yield ""
|
||||||
yield "class %s(models.Model):" % table2model(table_name)
|
yield "class %s(models.Model):" % model_name
|
||||||
known_models.append(table2model(table_name))
|
known_models.append(model_name)
|
||||||
used_column_names = [] # Holds column names used in the table so far
|
used_column_names = [] # Holds column names used in the table so far
|
||||||
column_to_field_name = {} # Maps column names to names of model fields
|
column_to_field_name = {} # Maps column names to names of model fields
|
||||||
|
used_relations = set() # Holds foreign relations used in the table.
|
||||||
for row in table_description:
|
for row in table_description:
|
||||||
comment_notes = (
|
comment_notes = (
|
||||||
[]
|
[]
|
||||||
@ -186,6 +188,12 @@ class Command(BaseCommand):
|
|||||||
field_type = "%s(%s" % (rel_type, rel_to)
|
field_type = "%s(%s" % (rel_type, rel_to)
|
||||||
else:
|
else:
|
||||||
field_type = "%s('%s'" % (rel_type, rel_to)
|
field_type = "%s('%s'" % (rel_type, rel_to)
|
||||||
|
if rel_to in used_relations:
|
||||||
|
extra_params["related_name"] = "%s_%s_set" % (
|
||||||
|
model_name.lower(),
|
||||||
|
att_name,
|
||||||
|
)
|
||||||
|
used_relations.add(rel_to)
|
||||||
else:
|
else:
|
||||||
# Calling `get_field_type` to get the field type string and any
|
# Calling `get_field_type` to get the field type string and any
|
||||||
# additional parameters and notes.
|
# additional parameters and notes.
|
||||||
|
@ -9,6 +9,7 @@ class People(models.Model):
|
|||||||
|
|
||||||
class Message(models.Model):
|
class Message(models.Model):
|
||||||
from_field = models.ForeignKey(People, models.CASCADE, db_column="from_id")
|
from_field = models.ForeignKey(People, models.CASCADE, db_column="from_id")
|
||||||
|
author = models.ForeignKey(People, models.CASCADE, related_name="message_authors")
|
||||||
|
|
||||||
|
|
||||||
class PeopleData(models.Model):
|
class PeopleData(models.Model):
|
||||||
|
@ -433,6 +433,15 @@ class InspectDBTestCase(TestCase):
|
|||||||
# The error message depends on the backend
|
# The error message depends on the backend
|
||||||
self.assertIn("# The error was:", output)
|
self.assertIn("# The error was:", output)
|
||||||
|
|
||||||
|
def test_same_relations(self):
|
||||||
|
out = StringIO()
|
||||||
|
call_command("inspectdb", "inspectdb_message", stdout=out)
|
||||||
|
self.assertIn(
|
||||||
|
"author = models.ForeignKey('InspectdbPeople', models.DO_NOTHING, "
|
||||||
|
"related_name='inspectdbmessage_author_set')",
|
||||||
|
out.getvalue(),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class InspectDBTransactionalTests(TransactionTestCase):
|
class InspectDBTransactionalTests(TransactionTestCase):
|
||||||
available_apps = ["inspectdb"]
|
available_apps = ["inspectdb"]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user