1
0
mirror of https://github.com/django/django.git synced 2025-06-15 16:39:13 +00:00

[5.2.x] Refs #36052, #32234 -- Removed create_test_table_with_composite_primary_key flag in favor of using CompositePrimaryKey.

Now that Django properly supports creating models with composite primary
keys, the tests should use a `CompositePrimaryKey` field instead of a
feature flag to inline backend specific SQL for creating a composite PK.

Specifcially, the inspectdb's test_composite_primary_key was adjusted to
use schema editor instead of per-backend raw SQL.

Backport of 4c75858135589f3a00e32eb4d476074536371a32 from main.
This commit is contained in:
Simon Charette 2025-04-28 22:46:13 -04:00 committed by Natalia
parent 7f6a5fbe2e
commit 5d03c71b7a
7 changed files with 7 additions and 37 deletions

View File

@ -278,10 +278,6 @@ class BaseDatabaseFeatures:
create_test_procedure_without_params_sql = None
create_test_procedure_with_int_param_sql = None
# SQL to create a table with a composite primary key for use by the Django
# test suite.
create_test_table_with_composite_primary_key = None
# Does the backend support keyword parameters for cursor.callproc()?
supports_callproc_kwargs = False

View File

@ -41,13 +41,6 @@ class DatabaseFeatures(BaseDatabaseFeatures):
SET V_I = P_I;
END;
"""
create_test_table_with_composite_primary_key = """
CREATE TABLE test_table_composite_pk (
column_1 INTEGER NOT NULL,
column_2 INTEGER NOT NULL,
PRIMARY KEY(column_1, column_2)
)
"""
# Neither MySQL nor MariaDB support partial indexes.
supports_partial_indexes = False
# COLLATE must be wrapped in parentheses because MySQL treats COLLATE as an

View File

@ -60,13 +60,6 @@ class DatabaseFeatures(BaseDatabaseFeatures):
V_I := P_I;
END;
"""
create_test_table_with_composite_primary_key = """
CREATE TABLE test_table_composite_pk (
column_1 NUMBER(11) NOT NULL,
column_2 NUMBER(11) NOT NULL,
PRIMARY KEY (column_1, column_2)
)
"""
supports_callproc_kwargs = True
supports_over_clause = True
supports_frame_range_fixed_distance = True

View File

@ -52,13 +52,6 @@ class DatabaseFeatures(BaseDatabaseFeatures):
V_I := P_I;
END;
$$ LANGUAGE plpgsql;"""
create_test_table_with_composite_primary_key = """
CREATE TABLE test_table_composite_pk (
column_1 INTEGER NOT NULL,
column_2 INTEGER NOT NULL,
PRIMARY KEY(column_1, column_2)
)
"""
requires_casted_case_in_updates = True
supports_over_clause = True
supports_frame_exclusion = True

View File

@ -51,13 +51,6 @@ class DatabaseFeatures(BaseDatabaseFeatures):
# Date/DateTime fields and timedeltas.
"expressions.tests.FTimeDeltaTests.test_mixed_comparisons1",
}
create_test_table_with_composite_primary_key = """
CREATE TABLE test_table_composite_pk (
column_1 INTEGER NOT NULL,
column_2 INTEGER NOT NULL,
PRIMARY KEY(column_1, column_2)
)
"""
insert_test_table_with_defaults = 'INSERT INTO {} ("null") VALUES (1)'
supports_default_keyword_in_insert = False
supports_unlimited_charfield = True

View File

@ -155,3 +155,9 @@ class DbComment(models.Model):
class Meta:
db_table_comment = "Custom table comment"
required_db_features = {"supports_comments"}
class CompositePrimaryKeyModel(models.Model):
pk = models.CompositePrimaryKey("column_1", "column_2")
column_1 = models.IntegerField()
column_2 = models.IntegerField()

View File

@ -626,17 +626,13 @@ class InspectDBTransactionalTests(TransactionTestCase):
self.assertIn(foreign_table_model, output)
self.assertIn(foreign_table_managed, output)
@skipUnlessDBFeature("create_test_table_with_composite_primary_key")
def test_composite_primary_key(self):
table_name = "test_table_composite_pk"
cursor_execute(connection.features.create_test_table_with_composite_primary_key)
self.addCleanup(cursor_execute, "DROP TABLE %s" % table_name)
out = StringIO()
if connection.vendor == "sqlite":
field_type = connection.features.introspected_field_types["AutoField"]
else:
field_type = connection.features.introspected_field_types["IntegerField"]
call_command("inspectdb", table_name, stdout=out)
call_command("inspectdb", "inspectdb_compositeprimarykeymodel", stdout=out)
output = out.getvalue()
self.assertIn(
"pk = models.CompositePrimaryKey('column_1', 'column_2')",