diff --git a/tests/_utils/setup.sql b/tests/_utils/setup.sql index c883b8bf3a9f..94c797335d7f 100644 --- a/tests/_utils/setup.sql +++ b/tests/_utils/setup.sql @@ -571,3 +571,22 @@ CREATE TABLE `bulk_create_relatedmodel_bigautofieldmodel` ( KEY (`relatedmodel_id`), KEY (`bigautofieldmodel_id`) ); + +-- backends +CREATE TABLE `backends_verylongmodelnamezzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz_person` ( + `verylongmodelnamezzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz_id` BIGINT NOT NULL, + `person_id` BIGINT NOT NULL, + SHARD KEY (`verylongmodelnamezzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz_id`), + UNIQUE KEY (`verylongmodelnamezzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz_id`, `person_id`), + KEY (`verylongmodelnamezzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz_id`), + KEY (`person_id`) +); + +CREATE TABLE `backends_object_object` ( + `from_object_id` BIGINT NOT NULL, + `to_object_id` BIGINT NOT NULL, + SHARD KEY (`from_object_id`), + UNIQUE KEY (`from_object_id`, `to_object_id`), + KEY (`from_object_id`), + KEY (`to_object_id`) +); diff --git a/tests/backends/models.py b/tests/backends/models.py index 99e9e86f44d2..4aa88da9a768 100644 --- a/tests/backends/models.py +++ b/tests/backends/models.py @@ -40,10 +40,22 @@ class VeryLongModelNameZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ(models.Model): max_length=100 ) m2m_also_quite_long_zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz = ( - models.ManyToManyField(Person, blank=True) + models.ManyToManyField(Person, blank=True, + through="VeryLongModelNameZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZPerson") ) +class VeryLongModelNameZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZPerson(models.Model): + verylongmodelnamezzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz = models.ForeignKey( + VeryLongModelNameZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ, on_delete=models.CASCADE + ) + person = models.ForeignKey(Person, on_delete=models.CASCADE) + + class Meta: + unique_together = (('verylongmodelnamezzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz', 'person'),) + db_table = "backends_verylongmodelnamezzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz_person" + + class Tag(models.Model): name = models.CharField(max_length=30) content_type = models.ForeignKey( @@ -102,14 +114,23 @@ def __str__(self): class Object(models.Model): related_objects = models.ManyToManyField( - "self", db_constraint=False, symmetrical=False - ) + "self", symmetrical=False, through="ObjectFriend" + ) obj_ref = models.ForeignKey("ObjectReference", models.CASCADE, null=True) def __str__(self): return str(self.id) +class ObjectFriend(models.Model): + from_object = models.ForeignKey(Object, on_delete=models.CASCADE, related_name="from_object") + to_object = models.ForeignKey(Object, on_delete=models.CASCADE, related_name="to_object") + + class Meta: + unique_together = (('from_object', 'to_object'),) + db_table = "backends_object_object" + + class ObjectReference(models.Model): obj = models.ForeignKey(Object, models.CASCADE, db_constraint=False) @@ -118,12 +139,12 @@ def __str__(self): class ObjectSelfReference(models.Model): - key = models.CharField(max_length=3, unique=True) + key = models.CharField(max_length=3, primary_key=True) obj = models.ForeignKey("ObjectSelfReference", models.SET_NULL, null=True) class CircularA(models.Model): - key = models.CharField(max_length=3, unique=True) + key = models.CharField(max_length=3, primary_key=True) obj = models.ForeignKey("CircularB", models.SET_NULL, null=True) def natural_key(self): @@ -131,7 +152,7 @@ def natural_key(self): class CircularB(models.Model): - key = models.CharField(max_length=3, unique=True) + key = models.CharField(max_length=3, primary_key=True) obj = models.ForeignKey("CircularA", models.SET_NULL, null=True) def natural_key(self): @@ -143,7 +164,7 @@ class RawData(models.Model): class Author(models.Model): - name = models.CharField(max_length=255, unique=True) + name = models.CharField(max_length=255, primary_key=True) class Book(models.Model): diff --git a/tests/backends/test_utils.py b/tests/backends/test_utils.py index 03d4b036fdfd..aad82bfc597b 100644 --- a/tests/backends/test_utils.py +++ b/tests/backends/test_utils.py @@ -2,6 +2,7 @@ from decimal import Decimal, Rounded from django.db import NotSupportedError, connection +from django.db.utils import OperationalError from django.db.backends.utils import ( format_number, split_identifier, @@ -94,6 +95,12 @@ class CursorWrapperTests(TransactionTestCase): available_apps = [] def _test_procedure(self, procedure_sql, params, param_types, kparams=None): + # make sure procedure is not already defined + try: + with connection.schema_editor() as editor: + editor.remove_procedure("test_procedure", param_types) + except OperationalError: + pass with connection.cursor() as cursor: cursor.execute(procedure_sql) # Use a new cursor because in MySQL a procedure can't be used in the