mirror of
https://github.com/django/django.git
synced 2025-06-03 18:49:12 +00:00
Fixed #36052 -- Supported CompositePrimaryKey in inspectdb.
This commit is contained in:
parent
ddefc3fed1
commit
a8e4fd11ef
@ -106,9 +106,12 @@ class Command(BaseCommand):
|
|||||||
connection.introspection.get_primary_key_columns(
|
connection.introspection.get_primary_key_columns(
|
||||||
cursor, table_name
|
cursor, table_name
|
||||||
)
|
)
|
||||||
|
or []
|
||||||
)
|
)
|
||||||
primary_key_column = (
|
primary_key_column = (
|
||||||
primary_key_columns[0] if primary_key_columns else None
|
primary_key_columns[0]
|
||||||
|
if len(primary_key_columns) == 1
|
||||||
|
else None
|
||||||
)
|
)
|
||||||
unique_columns = [
|
unique_columns = [
|
||||||
c["columns"][0]
|
c["columns"][0]
|
||||||
@ -128,6 +131,11 @@ class Command(BaseCommand):
|
|||||||
yield ""
|
yield ""
|
||||||
yield "class %s(models.Model):" % model_name
|
yield "class %s(models.Model):" % model_name
|
||||||
known_models.append(model_name)
|
known_models.append(model_name)
|
||||||
|
|
||||||
|
if len(primary_key_columns) > 1:
|
||||||
|
fields = ", ".join([f"'{col}'" for col in primary_key_columns])
|
||||||
|
yield f" pk = models.CompositePrimaryKey({fields})"
|
||||||
|
|
||||||
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.
|
used_relations = set() # Holds foreign relations used in the table.
|
||||||
@ -151,12 +159,6 @@ class Command(BaseCommand):
|
|||||||
# Add primary_key and unique, if necessary.
|
# Add primary_key and unique, if necessary.
|
||||||
if column_name == primary_key_column:
|
if column_name == primary_key_column:
|
||||||
extra_params["primary_key"] = True
|
extra_params["primary_key"] = True
|
||||||
if len(primary_key_columns) > 1:
|
|
||||||
comment_notes.append(
|
|
||||||
"The composite primary key (%s) found, that is not "
|
|
||||||
"supported. The first column is selected."
|
|
||||||
% ", ".join(primary_key_columns)
|
|
||||||
)
|
|
||||||
elif column_name in unique_columns:
|
elif column_name in unique_columns:
|
||||||
extra_params["unique"] = True
|
extra_params["unique"] = True
|
||||||
|
|
||||||
|
@ -655,11 +655,10 @@ class InspectDBTransactionalTests(TransactionTestCase):
|
|||||||
call_command("inspectdb", table_name, stdout=out)
|
call_command("inspectdb", table_name, stdout=out)
|
||||||
output = out.getvalue()
|
output = out.getvalue()
|
||||||
self.assertIn(
|
self.assertIn(
|
||||||
f"column_1 = models.{field_type}(primary_key=True) # The composite "
|
"pk = models.CompositePrimaryKey('column_1', 'column_2')",
|
||||||
f"primary key (column_1, column_2) found, that is not supported. The "
|
|
||||||
f"first column is selected.",
|
|
||||||
output,
|
output,
|
||||||
)
|
)
|
||||||
|
self.assertIn(f"column_1 = models.{field_type}()", output)
|
||||||
self.assertIn(
|
self.assertIn(
|
||||||
"column_2 = models.%s()"
|
"column_2 = models.%s()"
|
||||||
% connection.features.introspected_field_types["IntegerField"],
|
% connection.features.introspected_field_types["IntegerField"],
|
||||||
|
Loading…
x
Reference in New Issue
Block a user