mirror of
				https://github.com/django/django.git
				synced 2025-10-31 09:41:08 +00:00 
			
		
		
		
	[1.10.x] Refs #27558 -- Isolated indexes test on MySQL.
MySQL schema changes must be done in TransactionTestCase.
Backport of f94475e526 from master
			
			
This commit is contained in:
		| @@ -3,7 +3,7 @@ from unittest import skipUnless | |||||||
| from django.db import connection | from django.db import connection | ||||||
| from django.db.models.deletion import CASCADE | from django.db.models.deletion import CASCADE | ||||||
| from django.db.models.fields.related import ForeignKey | from django.db.models.fields.related import ForeignKey | ||||||
| from django.test import TestCase | from django.test import TestCase, TransactionTestCase | ||||||
|  |  | ||||||
| from .models import Article, ArticleTranslation, IndexTogetherSingleList | from .models import Article, ArticleTranslation, IndexTogetherSingleList | ||||||
|  |  | ||||||
| @@ -60,7 +60,11 @@ class SchemaIndexesTests(TestCase): | |||||||
|         index_sql = connection.schema_editor()._model_indexes_sql(Article) |         index_sql = connection.schema_editor()._model_indexes_sql(Article) | ||||||
|         self.assertEqual(len(index_sql), 1) |         self.assertEqual(len(index_sql), 1) | ||||||
|  |  | ||||||
|     @skipUnless(connection.vendor == 'mysql', "This is a mysql-specific issue") |  | ||||||
|  | @skipUnless(connection.vendor == 'mysql', 'MySQL tests') | ||||||
|  | class SchemaIndexesMySQLTests(TransactionTestCase): | ||||||
|  |     available_apps = ['indexes'] | ||||||
|  |  | ||||||
|     def test_no_index_for_foreignkey(self): |     def test_no_index_for_foreignkey(self): | ||||||
|         """ |         """ | ||||||
|         MySQL on InnoDB already creates indexes automatically for foreign keys. |         MySQL on InnoDB already creates indexes automatically for foreign keys. | ||||||
| @@ -79,6 +83,8 @@ class SchemaIndexesTests(TestCase): | |||||||
|  |  | ||||||
|         # The index also shouldn't be created if the ForeignKey is added after |         # The index also shouldn't be created if the ForeignKey is added after | ||||||
|         # the model was created. |         # the model was created. | ||||||
|  |         field_created = False | ||||||
|  |         try: | ||||||
|             with connection.schema_editor() as editor: |             with connection.schema_editor() as editor: | ||||||
|                 new_field = ForeignKey(Article, CASCADE) |                 new_field = ForeignKey(Article, CASCADE) | ||||||
|                 new_field.set_attributes_from_name('new_foreign_key') |                 new_field.set_attributes_from_name('new_foreign_key') | ||||||
| @@ -88,3 +94,7 @@ class SchemaIndexesTests(TestCase): | |||||||
|                     'ADD CONSTRAINT `indexes_articl_new_foreign_key_id_d27a9146_fk_indexes_article_id` ' |                     'ADD CONSTRAINT `indexes_articl_new_foreign_key_id_d27a9146_fk_indexes_article_id` ' | ||||||
|                     'FOREIGN KEY (`new_foreign_key_id`) REFERENCES `indexes_article` (`id`)' |                     'FOREIGN KEY (`new_foreign_key_id`) REFERENCES `indexes_article` (`id`)' | ||||||
|                 ]) |                 ]) | ||||||
|  |         finally: | ||||||
|  |             if field_created: | ||||||
|  |                 with connection.schema_editor() as editor: | ||||||
|  |                     editor.remove_field(ArticleTranslation, new_field) | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user