Debian Codegraph @

tests/schema/tests.py

raw
1 import datetime
2 import itertools
3 import unittest
4 from copy import copy
5 from decimal import Decimal
6 from unittest import mock
7
8 from django.core.exceptions import FieldError
9 from django.core.management.color import no_style
10 from django.core.serializers.json import DjangoJSONEncoder
11 from django.db import (
12 DatabaseError,
13 DataError,
14 IntegrityError,
15 OperationalError,
16 connection,
17 )
18 from django.db.backends.utils import truncate_name
19 from django.db.models import (
20 CASCADE,
21 PROTECT,
22 AutoField,
23 BigAutoField,
24 BigIntegerField,
25 BinaryField,
26 BooleanField,
27 CharField,
28 CheckConstraint,
29 DateField,
30 DateTimeField,
31 DecimalField,
32 DurationField,
33 F,
34 FloatField,
35 ForeignKey,
36 ForeignObject,
37 GeneratedField,
38 Index,
39 IntegerField,
40 JSONField,
41 ManyToManyField,
42 Model,
43 OneToOneField,
44 OrderBy,
45 PositiveIntegerField,
46 Q,
47 SlugField,
48 SmallAutoField,
49 SmallIntegerField,
50 TextField,
51 TimeField,
52 UniqueConstraint,
53 UUIDField,
54 Value,
55 )
56 from django.db.models.fields.json import KT, KeyTextTransform
57 from django.db.models.functions import (
58 Abs,
59 Cast,
60 Collate,
61 Concat,
62 Lower,
63 Random,
64 Round,
65 Upper,
66 )
67 from django.db.models.indexes import IndexExpression
68 from django.db.transaction import TransactionManagementError, atomic
69 from django.test import TransactionTestCase, skipIfDBFeature, skipUnlessDBFeature
70 from django.test.utils import CaptureQueriesContext, isolate_apps, register_lookup
71
72 from .fields import CustomManyToManyField, InheritedManyToManyField, MediumBlobField
73 from .models import (
74 Author,
75 AuthorCharFieldWithIndex,
76 AuthorTextFieldWithIndex,
77 AuthorWithDefaultHeight,
78 AuthorWithEvenLongerName,
79 AuthorWithIndexedName,
80 AuthorWithUniqueName,
81 AuthorWithUniqueNameAndBirthday,
82 Book,
83 BookForeignObj,
84 BookWeak,
85 BookWithLongName,
86 BookWithO2O,
87 BookWithoutAuthor,
88 BookWithSlug,
89 IntegerPK,
90 Node,
91 Note,
92 NoteRename,
93 Tag,
94 TagM2MTest,
95 TagUniqueRename,
96 Thing,
97 UniqueTest,
98 new_apps,
99 )
100
101
102 class SchemaTests(TransactionTestCase):
103 """
104 Tests for the schema-alteration code.
105
106 Be aware that these tests are more liable than most to false results,
107 as sometimes the code to check if a test has worked is almost as complex
108 as the code it is testing.
109 """
110
111 available_apps = []
112
113 models = [
114 Author,
115 AuthorCharFieldWithIndex,
116 AuthorTextFieldWithIndex,
117 AuthorWithDefaultHeight,
118 AuthorWithEvenLongerName,
119 Book,
120 BookWeak,
121 BookWithLongName,
122 BookWithO2O,
123 BookWithSlug,
124 IntegerPK,
125 Node,
126 Note,
127 Tag,
128 TagM2MTest,
129 TagUniqueRename,
130 Thing,
131 UniqueTest,
132 ]
133
134 # Utility functions
135
136 def setUp(self):
137 # local_models should contain test dependent model classes that will be
138 # automatically removed from the app cache on test tear down.
139 self.local_models = []
140 # isolated_local_models contains models that are in test methods
141 # decorated with @isolate_apps.
142 self.isolated_local_models = []
143
144 def tearDown(self):
145 # Delete any tables made for our models
146 self.delete_tables()
147 new_apps.clear_cache()
148 for model in new_apps.get_models():
149 model._meta._expire_cache()
150 if "schema" in new_apps.all_models:
151 for model in self.local_models:
152 for many_to_many in model._meta.many_to_many:
153 through = many_to_many.remote_field.through
154 if through and through._meta.auto_created:
155 del new_apps.all_models["schema"][through._meta.model_name]
156 del new_apps.all_models["schema"][model._meta.model_name]
157 if self.isolated_local_models:
158 with connection.schema_editor() as editor:
159 for model in self.isolated_local_models:
160 editor.delete_model(model)
161
162 def delete_tables(self):
163 "Deletes all model tables for our models for a clean test environment"
164 converter = connection.introspection.identifier_converter
165 with connection.schema_editor() as editor:
166 connection.disable_constraint_checking()
167 table_names = connection.introspection.table_names()
168 if connection.features.ignores_table_name_case:
169 table_names = [table_name.lower() for table_name in table_names]
170 for model in itertools.chain(SchemaTests.models, self.local_models):
171 tbl = converter(model._meta.db_table)
172 if connection.features.ignores_table_name_case:
173 tbl = tbl.lower()
174 if tbl in table_names:
175 editor.delete_model(model)
176 table_names.remove(tbl)
177 connection.enable_constraint_checking()
178
179 def column_classes(self, model):
180 with connection.cursor() as cursor:
181 columns = {
182 d[0]: (connection.introspection.get_field_type(d[1], d), d)
183 for d in connection.introspection.get_table_description(
184 cursor,
185 model._meta.db_table,
186 )
187 }
188 # SQLite has a different format for field_type
189 for name, (type, desc) in columns.items():
190 if isinstance(type, tuple):
191 columns[name] = (type[0], desc)
192 return columns
193
194 def get_primary_key(self, table):
195 with connection.cursor() as cursor:
196 return connection.introspection.get_primary_key_column(cursor, table)
197
198 def get_indexes(self, table):
199 """
200 Get the indexes on the table using a new cursor.
201 """
202 with connection.cursor() as cursor:
203 return [
204 c["columns"][0]
205 for c in connection.introspection.get_constraints(
206 cursor, table
207 ).values()
208 if c["index"] and len(c["columns"]) == 1
209 ]
210
211 def get_uniques(self, table):
212 with connection.cursor() as cursor:
213 return [
214 c["columns"][0]
215 for c in connection.introspection.get_constraints(
216 cursor, table
217 ).values()
218 if c["unique"] and len(c["columns"]) == 1
219 ]
220
221 def get_constraints(self, table):
222 """
223 Get the constraints on a table using a new cursor.
224 """
225 with connection.cursor() as cursor:
226 return connection.introspection.get_constraints(cursor, table)
227
228 def get_constraints_for_column(self, model, column_name):
229 constraints = self.get_constraints(model._meta.db_table)
230 constraints_for_column = []
231 for name, details in constraints.items():
232 if details["columns"] == [column_name]:
233 constraints_for_column.append(name)
234 return sorted(constraints_for_column)
235
236 def get_constraint_opclasses(self, constraint_name):
237 with connection.cursor() as cursor:
238 sql = """
239 SELECT opcname
240 FROM pg_opclass AS oc
241 JOIN pg_index as i on oc.oid = ANY(i.indclass)
242 JOIN pg_class as c on c.oid = i.indexrelid
243 WHERE c.relname = %s
244 """
245 cursor.execute(sql, [constraint_name])
246 return [row[0] for row in cursor.fetchall()]
247
248 def check_added_field_default(
249 self,
250 schema_editor,
251 model,
252 field,
253 field_name,
254 expected_default,
255 cast_function=None,
256 ):
257 with connection.cursor() as cursor:
258 schema_editor.add_field(model, field)
259 cursor.execute(
260 "SELECT {} FROM {};".format(field_name, model._meta.db_table)
261 )
262 database_default = cursor.fetchall()[0][0]
263 if cast_function and type(database_default) is not type(expected_default):
264 database_default = cast_function(database_default)
265 self.assertEqual(database_default, expected_default)
266
267 def get_constraints_count(self, table, column, fk_to):
268 """
269 Return a dict with keys 'fks', 'uniques, and 'indexes' indicating the
270 number of foreign keys, unique constraints, and indexes on
271 `table`.`column`. The `fk_to` argument is a 2-tuple specifying the
272 expected foreign key relationship's (table, column).
273 """
274 with connection.cursor() as cursor:
275 constraints = connection.introspection.get_constraints(cursor, table)
276 counts = {"fks": 0, "uniques": 0, "indexes": 0}
277 for c in constraints.values():
278 if c["columns"] == [column]:
279 if c["foreign_key"] == fk_to:
280 counts["fks"] += 1
281 if c["unique"]:
282 counts["uniques"] += 1
283 elif c["index"]:
284 counts["indexes"] += 1
285 return counts
286
287 def get_column_collation(self, table, column):
288 with connection.cursor() as cursor:
289 return next(
290 f.collation
291 for f in connection.introspection.get_table_description(cursor, table)
292 if f.name == column
293 )
294
295 def get_column_comment(self, table, column):
296 with connection.cursor() as cursor:
297 return next(
298 f.comment
299 for f in connection.introspection.get_table_description(cursor, table)
300 if f.name == column
301 )
302
303 def get_table_comment(self, table):
304 with connection.cursor() as cursor:
305 return next(
306 t.comment
307 for t in connection.introspection.get_table_list(cursor)
308 if t.name == table
309 )
310
311 def assert_column_comment_not_exists(self, table, column):
312 with connection.cursor() as cursor:
313 columns = connection.introspection.get_table_description(cursor, table)
314 self.assertFalse(any([c.name == column and c.comment for c in columns]))
315
316 def assertIndexOrder(self, table, index, order):
317 constraints = self.get_constraints(table)
318 self.assertIn(index, constraints)
319 index_orders = constraints[index]["orders"]
320 self.assertTrue(
321 all(val == expected for val, expected in zip(index_orders, order))
322 )
323
324 def assertForeignKeyExists(self, model, column, expected_fk_table, field="id"):
325 """
326 Fail if the FK constraint on `model.Meta.db_table`.`column` to
327 `expected_fk_table`.id doesn't exist.
328 """
329 if not connection.features.can_introspect_foreign_keys:
330 return
331 constraints = self.get_constraints(model._meta.db_table)
332 constraint_fk = None
333 for details in constraints.values():
334 if details["columns"] == [column] and details["foreign_key"]:
335 constraint_fk = details["foreign_key"]
336 break
337 self.assertEqual(constraint_fk, (expected_fk_table, field))
338
339 def assertForeignKeyNotExists(self, model, column, expected_fk_table):
340 if not connection.features.can_introspect_foreign_keys:
341 return
342 with self.assertRaises(AssertionError):
343 self.assertForeignKeyExists(model, column, expected_fk_table)
344
345 # Tests
346 def test_creation_deletion(self):
347 """
348 Tries creating a model's table, and then deleting it.
349 """
350 with connection.schema_editor() as editor:
351 # Create the table
352 editor.create_model(Author)
353 # The table is there
354 list(Author.objects.all())
355 # Clean up that table
356 editor.delete_model(Author)
357 # No deferred SQL should be left over.
358 self.assertEqual(editor.deferred_sql, [])
359 # The table is gone
360 with self.assertRaises(DatabaseError):
361 list(Author.objects.all())
362
363 @skipUnlessDBFeature("supports_foreign_keys")
364 def test_fk(self):
365 "Creating tables out of FK order, then repointing, works"
366 # Create the table
367 with connection.schema_editor() as editor:
368 editor.create_model(Book)
369 editor.create_model(Author)
370 editor.create_model(Tag)
371 # Initial tables are there
372 list(Author.objects.all())
373 list(Book.objects.all())
374 # Make sure the FK constraint is present
375 with self.assertRaises(IntegrityError):
376 Book.objects.create(
377 author_id=1,
378 title="Much Ado About Foreign Keys",
379 pub_date=datetime.datetime.now(),
380 )
381 # Repoint the FK constraint
382 old_field = Book._meta.get_field("author")
383 new_field = ForeignKey(Tag, CASCADE)
384 new_field.set_attributes_from_name("author")
385 with connection.schema_editor() as editor:
386 editor.alter_field(Book, old_field, new_field, strict=True)
387 self.assertForeignKeyExists(Book, "author_id", "schema_tag")
388
389 @skipUnlessDBFeature("can_create_inline_fk")
390 def test_inline_fk(self):
391 # Create some tables.
392 with connection.schema_editor() as editor:
393 editor.create_model(Author)
394 editor.create_model(Book)
395 editor.create_model(Note)
396 self.assertForeignKeyNotExists(Note, "book_id", "schema_book")
397 # Add a foreign key from one to the other.
398 with connection.schema_editor() as editor:
399 new_field = ForeignKey(Book, CASCADE)
400 new_field.set_attributes_from_name("book")
401 editor.add_field(Note, new_field)
402 self.assertForeignKeyExists(Note, "book_id", "schema_book")
403 # Creating a FK field with a constraint uses a single statement without
404 # a deferred ALTER TABLE.
405 self.assertFalse(
406 [
407 sql
408 for sql in (str(statement) for statement in editor.deferred_sql)
409 if sql.startswith("ALTER TABLE") and "ADD CONSTRAINT" in sql
410 ]
411 )
412
413 @skipUnlessDBFeature("can_create_inline_fk")
414 def test_add_inline_fk_update_data(self):
415 with connection.schema_editor() as editor:
416 editor.create_model(Node)
417 # Add an inline foreign key and update data in the same transaction.
418 new_field = ForeignKey(Node, CASCADE, related_name="new_fk", null=True)
419 new_field.set_attributes_from_name("new_parent_fk")
420 parent = Node.objects.create()
421 with connection.schema_editor() as editor:
422 editor.add_field(Node, new_field)
423 editor.execute("UPDATE schema_node SET new_parent_fk_id = %s;", [parent.pk])
424 assertIndex = (
425 self.assertIn
426 if connection.features.indexes_foreign_keys
427 else self.assertNotIn
428 )
429 assertIndex("new_parent_fk_id", self.get_indexes(Node._meta.db_table))
430
431 @skipUnlessDBFeature(
432 "can_create_inline_fk",
433 "allows_multiple_constraints_on_same_fields",
434 )
435 @isolate_apps("schema")
436 def test_add_inline_fk_index_update_data(self):
437 class Node(Model):
438 class Meta:
439 app_label = "schema"
440
441 with connection.schema_editor() as editor:
442 editor.create_model(Node)
443 # Add an inline foreign key, update data, and an index in the same
444 # transaction.
445 new_field = ForeignKey(Node, CASCADE, related_name="new_fk", null=True)
446 new_field.set_attributes_from_name("new_parent_fk")
447 parent = Node.objects.create()
448 with connection.schema_editor() as editor:
449 editor.add_field(Node, new_field)
450 Node._meta.add_field(new_field)
451 editor.execute("UPDATE schema_node SET new_parent_fk_id = %s;", [parent.pk])
452 editor.add_index(
453 Node, Index(fields=["new_parent_fk"], name="new_parent_inline_fk_idx")
454 )
455 self.assertIn("new_parent_fk_id", self.get_indexes(Node._meta.db_table))
456
457 @skipUnlessDBFeature("supports_foreign_keys")
458 def test_char_field_with_db_index_to_fk(self):
459 # Create the table
460 with connection.schema_editor() as editor:
461 editor.create_model(Author)
462 editor.create_model(AuthorCharFieldWithIndex)
463 # Change CharField to FK
464 old_field = AuthorCharFieldWithIndex._meta.get_field("char_field")
465 new_field = ForeignKey(Author, CASCADE, blank=True)
466 new_field.set_attributes_from_name("char_field")
467 with connection.schema_editor() as editor:
468 editor.alter_field(
469 AuthorCharFieldWithIndex, old_field, new_field, strict=True
470 )
471 self.assertForeignKeyExists(
472 AuthorCharFieldWithIndex, "char_field_id", "schema_author"
473 )
474
475 @skipUnlessDBFeature("supports_foreign_keys")
476 @skipUnlessDBFeature("supports_index_on_text_field")
477 def test_text_field_with_db_index_to_fk(self):
478 # Create the table
479 with connection.schema_editor() as editor:
480 editor.create_model(Author)
481 editor.create_model(AuthorTextFieldWithIndex)
482 # Change TextField to FK
483 old_field = AuthorTextFieldWithIndex._meta.get_field("text_field")
484 new_field = ForeignKey(Author, CASCADE, blank=True)
485 new_field.set_attributes_from_name("text_field")
486 with connection.schema_editor() as editor:
487 editor.alter_field(
488 AuthorTextFieldWithIndex, old_field, new_field, strict=True
489 )
490 self.assertForeignKeyExists(
491 AuthorTextFieldWithIndex, "text_field_id", "schema_author"
492 )
493
494 @isolate_apps("schema")
495 def test_char_field_pk_to_auto_field(self):
496 class Foo(Model):
497 id = CharField(max_length=255, primary_key=True)
498
499 class Meta:
500 app_label = "schema"
501
502 with connection.schema_editor() as editor:
503 editor.create_model(Foo)
504 self.isolated_local_models = [Foo]
505 old_field = Foo._meta.get_field("id")
506 new_field = AutoField(primary_key=True)
507 new_field.set_attributes_from_name("id")
508 new_field.model = Foo
509 with connection.schema_editor() as editor:
510 editor.alter_field(Foo, old_field, new_field, strict=True)
511
512 @skipUnlessDBFeature("supports_foreign_keys")
513 def test_fk_to_proxy(self):
514 "Creating a FK to a proxy model creates database constraints."
515
516 class AuthorProxy(Author):
517 class Meta:
518 app_label = "schema"
519 apps = new_apps
520 proxy = True
521
522 class AuthorRef(Model):
523 author = ForeignKey(AuthorProxy, on_delete=CASCADE)
524
525 class Meta:
526 app_label = "schema"
527 apps = new_apps
528
529 self.local_models = [AuthorProxy, AuthorRef]
530
531 # Create the table
532 with connection.schema_editor() as editor:
533 editor.create_model(Author)
534 editor.create_model(AuthorRef)
535 self.assertForeignKeyExists(AuthorRef, "author_id", "schema_author")
536
537 @skipUnlessDBFeature("supports_foreign_keys", "can_introspect_foreign_keys")
538 def test_fk_db_constraint(self):
539 "The db_constraint parameter is respected"
540 # Create the table
541 with connection.schema_editor() as editor:
542 editor.create_model(Tag)
543 editor.create_model(Author)
544 editor.create_model(BookWeak)
545 # Initial tables are there
546 list(Author.objects.all())
547 list(Tag.objects.all())
548 list(BookWeak.objects.all())
549 self.assertForeignKeyNotExists(BookWeak, "author_id", "schema_author")
550 # Make a db_constraint=False FK
551 new_field = ForeignKey(Tag, CASCADE, db_constraint=False)
552 new_field.set_attributes_from_name("tag")
553 with connection.schema_editor() as editor:
554 editor.add_field(Author, new_field)
555 self.assertForeignKeyNotExists(Author, "tag_id", "schema_tag")
556 # Alter to one with a constraint
557 new_field2 = ForeignKey(Tag, CASCADE)
558 new_field2.set_attributes_from_name("tag")
559 with connection.schema_editor() as editor:
560 editor.alter_field(Author, new_field, new_field2, strict=True)
561 self.assertForeignKeyExists(Author, "tag_id", "schema_tag")
562 # Alter to one without a constraint again
563 new_field2 = ForeignKey(Tag, CASCADE)
564 new_field2.set_attributes_from_name("tag")
565 with connection.schema_editor() as editor:
566 editor.alter_field(Author, new_field2, new_field, strict=True)
567 self.assertForeignKeyNotExists(Author, "tag_id", "schema_tag")
568
569 @isolate_apps("schema")
570 def test_no_db_constraint_added_during_primary_key_change(self):
571 """
572 When a primary key that's pointed to by a ForeignKey with
573 db_constraint=False is altered, a foreign key constraint isn't added.
574 """
575
576 class Author(Model):
577 class Meta:
578 app_label = "schema"
579
580 class BookWeak(Model):
581 author = ForeignKey(Author, CASCADE, db_constraint=False)
582
583 class Meta:
584 app_label = "schema"
585
586 with connection.schema_editor() as editor:
587 editor.create_model(Author)
588 editor.create_model(BookWeak)
589 self.assertForeignKeyNotExists(BookWeak, "author_id", "schema_author")
590 old_field = Author._meta.get_field("id")
591 new_field = BigAutoField(primary_key=True)
592 new_field.model = Author
593 new_field.set_attributes_from_name("id")
594 # @isolate_apps() and inner models are needed to have the model
595 # relations populated, otherwise this doesn't act as a regression test.
596 self.assertEqual(len(new_field.model._meta.related_objects), 1)
597 with connection.schema_editor() as editor:
598 editor.alter_field(Author, old_field, new_field, strict=True)
599 self.assertForeignKeyNotExists(BookWeak, "author_id", "schema_author")
600
601 def _test_m2m_db_constraint(self, M2MFieldClass):
602 class LocalAuthorWithM2M(Model):
603 name = CharField(max_length=255)
604
605 class Meta:
606 app_label = "schema"
607 apps = new_apps
608
609 self.local_models = [LocalAuthorWithM2M]
610
611 # Create the table
612 with connection.schema_editor() as editor:
613 editor.create_model(Tag)
614 editor.create_model(LocalAuthorWithM2M)
615 # Initial tables are there
616 list(LocalAuthorWithM2M.objects.all())
617 list(Tag.objects.all())
618 # Make a db_constraint=False FK
619 new_field = M2MFieldClass(Tag, related_name="authors", db_constraint=False)
620 new_field.contribute_to_class(LocalAuthorWithM2M, "tags")
621 # Add the field
622 with connection.schema_editor() as editor:
623 editor.add_field(LocalAuthorWithM2M, new_field)
624 self.assertForeignKeyNotExists(
625 new_field.remote_field.through, "tag_id", "schema_tag"
626 )
627
628 @skipUnlessDBFeature("supports_foreign_keys")
629 def test_m2m_db_constraint(self):
630 self._test_m2m_db_constraint(ManyToManyField)
631
632 @skipUnlessDBFeature("supports_foreign_keys")
633 def test_m2m_db_constraint_custom(self):
634 self._test_m2m_db_constraint(CustomManyToManyField)
635
636 @skipUnlessDBFeature("supports_foreign_keys")
637 def test_m2m_db_constraint_inherited(self):
638 self._test_m2m_db_constraint(InheritedManyToManyField)
639
640 def test_add_field(self):
641 """
642 Tests adding fields to models
643 """
644 # Create the table
645 with connection.schema_editor() as editor:
646 editor.create_model(Author)
647 # Ensure there's no age field
648 columns = self.column_classes(Author)
649 self.assertNotIn("age", columns)
650 # Add the new field
651 new_field = IntegerField(null=True)
652 new_field.set_attributes_from_name("age")
653 with (
654 CaptureQueriesContext(connection) as ctx,
655 connection.schema_editor() as editor,
656 ):
657 editor.add_field(Author, new_field)
658 drop_default_sql = editor.sql_alter_column_no_default % {
659 "column": editor.quote_name(new_field.name),
660 }
661 self.assertFalse(
662 any(drop_default_sql in query["sql"] for query in ctx.captured_queries)
663 )
664 # Table is not rebuilt.
665 self.assertIs(
666 any("CREATE TABLE" in query["sql"] for query in ctx.captured_queries), False
667 )
668 self.assertIs(
669 any("DROP TABLE" in query["sql"] for query in ctx.captured_queries), False
670 )
671 columns = self.column_classes(Author)
672 self.assertEqual(
673 columns["age"][0],
674 connection.features.introspected_field_types["IntegerField"],
675 )
676 self.assertTrue(columns["age"][1][6])
677
678 def test_add_field_remove_field(self):
679 """
680 Adding a field and removing it removes all deferred sql referring to it.
681 """
682 with connection.schema_editor() as editor:
683 # Create a table with a unique constraint on the slug field.
684 editor.create_model(Tag)
685 # Remove the slug column.
686 editor.remove_field(Tag, Tag._meta.get_field("slug"))
687 self.assertEqual(editor.deferred_sql, [])
688
689 def test_add_field_temp_default(self):
690 """
691 Tests adding fields to models with a temporary default
692 """
693 # Create the table
694 with connection.schema_editor() as editor:
695 editor.create_model(Author)
696 # Ensure there's no age field
697 columns = self.column_classes(Author)
698 self.assertNotIn("age", columns)
699 # Add some rows of data
700 Author.objects.create(name="Andrew", height=30)
701 Author.objects.create(name="Andrea")
702 # Add a not-null field
703 new_field = CharField(max_length=30, default="Godwin")
704 new_field.set_attributes_from_name("surname")
705 with connection.schema_editor() as editor:
706 editor.add_field(Author, new_field)
707 columns = self.column_classes(Author)
708 self.assertEqual(
709 columns["surname"][0],
710 connection.features.introspected_field_types["CharField"],
711 )
712 self.assertEqual(
713 columns["surname"][1][6],
714 connection.features.interprets_empty_strings_as_nulls,
715 )
716
717 def test_add_field_temp_default_boolean(self):
718 """
719 Tests adding fields to models with a temporary default where
720 the default is False. (#21783)
721 """
722 # Create the table
723 with connection.schema_editor() as editor:
724 editor.create_model(Author)
725 # Ensure there's no age field
726 columns = self.column_classes(Author)
727 self.assertNotIn("age", columns)
728 # Add some rows of data
729 Author.objects.create(name="Andrew", height=30)
730 Author.objects.create(name="Andrea")
731 # Add a not-null field
732 new_field = BooleanField(default=False)
733 new_field.set_attributes_from_name("awesome")
734 with connection.schema_editor() as editor:
735 editor.add_field(Author, new_field)
736 columns = self.column_classes(Author)
737 # BooleanField are stored as TINYINT(1) on MySQL.
738 field_type = columns["awesome"][0]
739 self.assertEqual(
740 field_type, connection.features.introspected_field_types["BooleanField"]
741 )
742
743 def test_add_field_default_transform(self):
744 """
745 Tests adding fields to models with a default that is not directly
746 valid in the database (#22581)
747 """
748
749 class TestTransformField(IntegerField):
750 # Weird field that saves the count of items in its value
751 def get_default(self):
752 return self.default
753
754 def get_prep_value(self, value):
755 if value is None:
756 return 0
757 return len(value)
758
759 # Create the table
760 with connection.schema_editor() as editor:
761 editor.create_model(Author)
762 # Add some rows of data
763 Author.objects.create(name="Andrew", height=30)
764 Author.objects.create(name="Andrea")
765 # Add the field with a default it needs to cast (to string in this case)
766 new_field = TestTransformField(default={1: 2})
767 new_field.set_attributes_from_name("thing")
768 with connection.schema_editor() as editor:
769 editor.add_field(Author, new_field)
770 # Ensure the field is there
771 columns = self.column_classes(Author)
772 field_type, field_info = columns["thing"]
773 self.assertEqual(
774 field_type, connection.features.introspected_field_types["IntegerField"]
775 )
776 # Make sure the values were transformed correctly
777 self.assertEqual(Author.objects.extra(where=["thing = 1"]).count(), 2)
778
779 def test_add_field_o2o_nullable(self):
780 with connection.schema_editor() as editor:
781 editor.create_model(Author)
782 editor.create_model(Note)
783 new_field = OneToOneField(Note, CASCADE, null=True)
784 new_field.set_attributes_from_name("note")
785 with connection.schema_editor() as editor:
786 editor.add_field(Author, new_field)
787 columns = self.column_classes(Author)
788 self.assertIn("note_id", columns)
789 self.assertTrue(columns["note_id"][1][6])
790
791 def test_add_field_binary(self):
792 """
793 Tests binary fields get a sane default (#22851)
794 """
795 # Create the table
796 with connection.schema_editor() as editor:
797 editor.create_model(Author)
798 # Add the new field
799 new_field = BinaryField(blank=True)
800 new_field.set_attributes_from_name("bits")
801 with connection.schema_editor() as editor:
802 editor.add_field(Author, new_field)
803 columns = self.column_classes(Author)
804 # MySQL annoyingly uses the same backend, so it'll come back as one of
805 # these two types.
806 self.assertIn(columns["bits"][0], ("BinaryField", "TextField"))
807
808 def test_add_field_durationfield_with_default(self):
809 with connection.schema_editor() as editor:
810 editor.create_model(Author)
811 new_field = DurationField(default=datetime.timedelta(minutes=10))
812 new_field.set_attributes_from_name("duration")
813 with connection.schema_editor() as editor:
814 editor.add_field(Author, new_field)
815 columns = self.column_classes(Author)
816 self.assertEqual(
817 columns["duration"][0],
818 connection.features.introspected_field_types["DurationField"],
819 )
820
821 @unittest.skipUnless(connection.vendor == "mysql", "MySQL specific")
822 def test_add_binaryfield_mediumblob(self):
823 """
824 Test adding a custom-sized binary field on MySQL (#24846).
825 """
826 # Create the table
827 with connection.schema_editor() as editor:
828 editor.create_model(Author)
829 # Add the new field with default
830 new_field = MediumBlobField(blank=True, default=b"123")
831 new_field.set_attributes_from_name("bits")
832 with connection.schema_editor() as editor:
833 editor.add_field(Author, new_field)
834 columns = self.column_classes(Author)
835 # Introspection treats BLOBs as TextFields
836 self.assertEqual(columns["bits"][0], "TextField")
837
838 @isolate_apps("schema")
839 @skipUnlessDBFeature("supports_json_field", "supports_stored_generated_columns")
840 def test_add_generated_field_with_kt_model(self):
841 class GeneratedFieldKTModel(Model):
842 data = JSONField()
843 status = GeneratedField(
844 expression=KT("data__status"),
845 output_field=TextField(),
846 db_persist=True,
847 )
848
849 class Meta:
850 app_label = "schema"
851
852 with CaptureQueriesContext(connection) as ctx:
853 with connection.schema_editor() as editor:
854 editor.create_model(GeneratedFieldKTModel)
855 self.assertIs(
856 any("None" in query["sql"] for query in ctx.captured_queries),
857 False,
858 )
859
860 @isolate_apps("schema")
861 @skipUnlessDBFeature("supports_virtual_generated_columns")
862 def test_add_generated_boolean_field(self):
863 class GeneratedBooleanFieldModel(Model):
864 value = IntegerField(null=True)
865 has_value = GeneratedField(
866 expression=Q(value__isnull=False),
867 output_field=BooleanField(),
868 db_persist=False,
869 )
870
871 class Meta:
872 app_label = "schema"
873
874 with connection.schema_editor() as editor:
875 editor.create_model(GeneratedBooleanFieldModel)
876 obj = GeneratedBooleanFieldModel.objects.create()
877 self.assertIs(obj.has_value, False)
878 obj = GeneratedBooleanFieldModel.objects.create(value=1)
879 self.assertIs(obj.has_value, True)
880
881 @isolate_apps("schema")
882 @skipUnlessDBFeature("supports_stored_generated_columns")
883 def test_add_generated_field(self):
884 class GeneratedFieldOutputFieldModel(Model):
885 price = DecimalField(max_digits=7, decimal_places=2)
886 vat_price = GeneratedField(
887 expression=Round(F("price") * Value(Decimal("1.22")), 2),
888 db_persist=True,
889 output_field=DecimalField(max_digits=8, decimal_places=2),
890 )
891
892 class Meta:
893 app_label = "schema"
894
895 with connection.schema_editor() as editor:
896 editor.create_model(GeneratedFieldOutputFieldModel)
897
898 @isolate_apps("schema")
899 @skipUnlessDBFeature("supports_stored_generated_columns")
900 def test_add_generated_field_contains(self):
901 class GeneratedFieldContainsModel(Model):
902 text = TextField(default="foo")
903 generated = GeneratedField(
904 expression=Concat("text", Value("%")),
905 db_persist=True,
906 output_field=TextField(),
907 )
908
909 class Meta:
910 app_label = "schema"
911
912 with connection.schema_editor() as editor:
913 editor.create_model(GeneratedFieldContainsModel)
914
915 field = GeneratedField(
916 expression=Q(text__contains="foo"),
917 db_persist=True,
918 output_field=BooleanField(),
919 )
920 field.contribute_to_class(GeneratedFieldContainsModel, "contains_foo")
921
922 with connection.schema_editor() as editor:
923 editor.add_field(GeneratedFieldContainsModel, field)
924
925 obj = GeneratedFieldContainsModel.objects.create()
926 obj.refresh_from_db()
927 self.assertEqual(obj.text, "foo")
928 self.assertEqual(obj.generated, "foo%")
929 self.assertIs(obj.contains_foo, True)
930
931 @isolate_apps("schema")
932 @skipUnlessDBFeature("supports_stored_generated_columns")
933 def test_alter_generated_field(self):
934 class GeneratedFieldIndexedModel(Model):
935 number = IntegerField(default=1)
936 generated = GeneratedField(
937 expression=F("number"),
938 db_persist=True,
939 output_field=IntegerField(),
940 )
941
942 class Meta:
943 app_label = "schema"
944
945 with connection.schema_editor() as editor:
946 editor.create_model(GeneratedFieldIndexedModel)
947
948 old_field = GeneratedFieldIndexedModel._meta.get_field("generated")
949 new_field = GeneratedField(
950 expression=F("number"),
951 db_persist=True,
952 db_index=True,
953 output_field=IntegerField(),
954 )
955 new_field.contribute_to_class(GeneratedFieldIndexedModel, "generated")
956
957 with connection.schema_editor() as editor:
958 editor.alter_field(GeneratedFieldIndexedModel, old_field, new_field)
959
960 self.assertIn(
961 "generated", self.get_indexes(GeneratedFieldIndexedModel._meta.db_table)
962 )
963
964 @isolate_apps("schema")
965 def test_add_auto_field(self):
966 class AddAutoFieldModel(Model):
967 name = CharField(max_length=255, primary_key=True)
968
969 class Meta:
970 app_label = "schema"
971
972 with connection.schema_editor() as editor:
973 editor.create_model(AddAutoFieldModel)
974 self.isolated_local_models = [AddAutoFieldModel]
975 old_field = AddAutoFieldModel._meta.get_field("name")
976 new_field = CharField(max_length=255)
977 new_field.set_attributes_from_name("name")
978 new_field.model = AddAutoFieldModel
979 with connection.schema_editor() as editor:
980 editor.alter_field(AddAutoFieldModel, old_field, new_field)
981 new_auto_field = AutoField(primary_key=True)
982 new_auto_field.set_attributes_from_name("id")
983 new_auto_field.model = AddAutoFieldModel()
984 with connection.schema_editor() as editor:
985 editor.add_field(AddAutoFieldModel, new_auto_field)
986 # Crashes on PostgreSQL when the GENERATED BY suffix is missing.
987 AddAutoFieldModel.objects.create(name="test")
988
989 def test_remove_field(self):
990 with connection.schema_editor() as editor:
991 editor.create_model(Author)
992 with CaptureQueriesContext(connection) as ctx:
993 editor.remove_field(Author, Author._meta.get_field("name"))
994 columns = self.column_classes(Author)
995 self.assertNotIn("name", columns)
996 if getattr(connection.features, "can_alter_table_drop_column", True):
997 # Table is not rebuilt.
998 self.assertIs(
999 any("CREATE TABLE" in query["sql"] for query in ctx.captured_queries),
1000 False,
1001 )
1002 self.assertIs(
1003 any("DROP TABLE" in query["sql"] for query in ctx.captured_queries),
1004 False,
1005 )
1006
1007 def test_remove_indexed_field(self):
1008 with connection.schema_editor() as editor:
1009 editor.create_model(AuthorCharFieldWithIndex)
1010 with connection.schema_editor() as editor:
1011 editor.remove_field(
1012 AuthorCharFieldWithIndex,
1013 AuthorCharFieldWithIndex._meta.get_field("char_field"),
1014 )
1015 columns = self.column_classes(AuthorCharFieldWithIndex)
1016 self.assertNotIn("char_field", columns)
1017
1018 def test_alter(self):
1019 """
1020 Tests simple altering of fields
1021 """
1022 # Create the table
1023 with connection.schema_editor() as editor:
1024 editor.create_model(Author)
1025 # Ensure the field is right to begin with
1026 columns = self.column_classes(Author)
1027 self.assertEqual(
1028 columns["name"][0],
1029 connection.features.introspected_field_types["CharField"],
1030 )
1031 self.assertEqual(
1032 bool(columns["name"][1][6]),
1033 bool(connection.features.interprets_empty_strings_as_nulls),
1034 )
1035 # Alter the name field to a TextField
1036 old_field = Author._meta.get_field("name")
1037 new_field = TextField(null=True)
1038 new_field.set_attributes_from_name("name")
1039 with connection.schema_editor() as editor:
1040 editor.alter_field(Author, old_field, new_field, strict=True)
1041 columns = self.column_classes(Author)
1042 self.assertEqual(columns["name"][0], "TextField")
1043 self.assertTrue(columns["name"][1][6])
1044 # Change nullability again
1045 new_field2 = TextField(null=False)
1046 new_field2.set_attributes_from_name("name")
1047 with connection.schema_editor() as editor:
1048 editor.alter_field(Author, new_field, new_field2, strict=True)
1049 columns = self.column_classes(Author)
1050 self.assertEqual(columns["name"][0], "TextField")
1051 self.assertEqual(
1052 bool(columns["name"][1][6]),
1053 bool(connection.features.interprets_empty_strings_as_nulls),
1054 )
1055
1056 def test_alter_auto_field_to_integer_field(self):
1057 # Create the table
1058 with connection.schema_editor() as editor:
1059 editor.create_model(Author)
1060 # Change AutoField to IntegerField
1061 old_field = Author._meta.get_field("id")
1062 new_field = IntegerField(primary_key=True)
1063 new_field.set_attributes_from_name("id")
1064 new_field.model = Author
1065 with connection.schema_editor() as editor:
1066 editor.alter_field(Author, old_field, new_field, strict=True)
1067 # Now that ID is an IntegerField, the database raises an error if it
1068 # isn't provided.
1069 if not connection.features.supports_unspecified_pk:
1070 with self.assertRaises(DatabaseError):
1071 Author.objects.create()
1072
1073 def test_alter_auto_field_to_char_field(self):
1074 # Create the table
1075 with connection.schema_editor() as editor:
1076 editor.create_model(Author)
1077 # Change AutoField to CharField
1078 old_field = Author._meta.get_field("id")
1079 new_field = CharField(primary_key=True, max_length=50)
1080 new_field.set_attributes_from_name("id")
1081 new_field.model = Author
1082 with connection.schema_editor() as editor:
1083 editor.alter_field(Author, old_field, new_field, strict=True)
1084
1085 @isolate_apps("schema")
1086 def test_alter_auto_field_quoted_db_column(self):
1087 class Foo(Model):
1088 id = AutoField(primary_key=True, db_column='"quoted_id"')
1089
1090 class Meta:
1091 app_label = "schema"
1092
1093 with connection.schema_editor() as editor:
1094 editor.create_model(Foo)
1095 self.isolated_local_models = [Foo]
1096 old_field = Foo._meta.get_field("id")
1097 new_field = BigAutoField(primary_key=True)
1098 new_field.model = Foo
1099 new_field.db_column = '"quoted_id"'
1100 new_field.set_attributes_from_name("id")
1101 with connection.schema_editor() as editor:
1102 editor.alter_field(Foo, old_field, new_field, strict=True)
1103 Foo.objects.create()
1104
1105 def test_alter_not_unique_field_to_primary_key(self):
1106 # Create the table.
1107 with connection.schema_editor() as editor:
1108 editor.create_model(Author)
1109 # Change UUIDField to primary key.
1110 old_field = Author._meta.get_field("uuid")
1111 new_field = UUIDField(primary_key=True)
1112 new_field.set_attributes_from_name("uuid")
1113 new_field.model = Author
1114 with connection.schema_editor() as editor:
1115 editor.remove_field(Author, Author._meta.get_field("id"))
1116 editor.alter_field(Author, old_field, new_field, strict=True)
1117 # Redundant unique constraint is not added.
1118 count = self.get_constraints_count(
1119 Author._meta.db_table,
1120 Author._meta.get_field("uuid").column,
1121 None,
1122 )
1123 self.assertLessEqual(count["uniques"], 1)
1124
1125 @isolate_apps("schema")
1126 def test_alter_primary_key_quoted_db_table(self):
1127 class Foo(Model):
1128 class Meta:
1129 app_label = "schema"
1130 db_table = '"foo"'
1131
1132 with connection.schema_editor() as editor:
1133 editor.create_model(Foo)
1134 self.isolated_local_models = [Foo]
1135 old_field = Foo._meta.get_field("id")
1136 new_field = BigAutoField(primary_key=True)
1137 new_field.model = Foo
1138 new_field.set_attributes_from_name("id")
1139 with connection.schema_editor() as editor:
1140 editor.alter_field(Foo, old_field, new_field, strict=True)
1141 Foo.objects.create()
1142
1143 def test_alter_text_field(self):
1144 # Regression for "BLOB/TEXT column 'info' can't have a default value")
1145 # on MySQL.
1146 # Create the table
1147 with connection.schema_editor() as editor:
1148 editor.create_model(Note)
1149 old_field = Note._meta.get_field("info")
1150 new_field = TextField(blank=True)
1151 new_field.set_attributes_from_name("info")
1152 with connection.schema_editor() as editor:
1153 editor.alter_field(Note, old_field, new_field, strict=True)
1154
1155 def test_alter_text_field_to_not_null_with_default_value(self):
1156 with connection.schema_editor() as editor:
1157 editor.create_model(Note)
1158 note = Note.objects.create(address=None)
1159 old_field = Note._meta.get_field("address")
1160 new_field = TextField(blank=True, default="", null=False)
1161 new_field.set_attributes_from_name("address")
1162 with connection.schema_editor() as editor:
1163 editor.alter_field(Note, old_field, new_field, strict=True)
1164 note.refresh_from_db()
1165 self.assertEqual(note.address, "")
1166
1167 @skipUnlessDBFeature("can_defer_constraint_checks", "can_rollback_ddl")
1168 def test_alter_fk_checks_deferred_constraints(self):
1169 """
1170 #25492 - Altering a foreign key's structure and data in the same
1171 transaction.
1172 """
1173 with connection.schema_editor() as editor:
1174 editor.create_model(Node)
1175 old_field = Node._meta.get_field("parent")
1176 new_field = ForeignKey(Node, CASCADE)
1177 new_field.set_attributes_from_name("parent")
1178 parent = Node.objects.create()
1179 with connection.schema_editor() as editor:
1180 # Update the parent FK to create a deferred constraint check.
1181 Node.objects.update(parent=parent)
1182 editor.alter_field(Node, old_field, new_field, strict=True)
1183
1184 @isolate_apps("schema")
1185 def test_alter_null_with_default_value_deferred_constraints(self):
1186 class Publisher(Model):
1187 class Meta:
1188 app_label = "schema"
1189
1190 class Article(Model):
1191 publisher = ForeignKey(Publisher, CASCADE)
1192 title = CharField(max_length=50, null=True)
1193 description = CharField(max_length=100, null=True)
1194
1195 class Meta:
1196 app_label = "schema"
1197
1198 with connection.schema_editor() as editor:
1199 editor.create_model(Publisher)
1200 editor.create_model(Article)
1201 self.isolated_local_models = [Article, Publisher]
1202
1203 publisher = Publisher.objects.create()
1204 Article.objects.create(publisher=publisher)
1205
1206 old_title = Article._meta.get_field("title")
1207 new_title = CharField(max_length=50, null=False, default="")
1208 new_title.set_attributes_from_name("title")
1209 old_description = Article._meta.get_field("description")
1210 new_description = CharField(max_length=100, null=False, default="")
1211 new_description.set_attributes_from_name("description")
1212 with connection.schema_editor() as editor:
1213 editor.alter_field(Article, old_title, new_title, strict=True)
1214 editor.alter_field(Article, old_description, new_description, strict=True)
1215
1216 def test_alter_text_field_to_date_field(self):
1217 """
1218 #25002 - Test conversion of text field to date field.
1219 """
1220 with connection.schema_editor() as editor:
1221 editor.create_model(Note)
1222 Note.objects.create(info="1988-05-05")
1223 old_field = Note._meta.get_field("info")
1224 new_field = DateField(blank=True)
1225 new_field.set_attributes_from_name("info")
1226 with connection.schema_editor() as editor:
1227 editor.alter_field(Note, old_field, new_field, strict=True)
1228 # Make sure the field isn't nullable
1229 columns = self.column_classes(Note)
1230 self.assertFalse(columns["info"][1][6])
1231
1232 def test_alter_text_field_to_datetime_field(self):
1233 """
1234 #25002 - Test conversion of text field to datetime field.
1235 """
1236 with connection.schema_editor() as editor:
1237 editor.create_model(Note)
1238 Note.objects.create(info="1988-05-05 3:16:17.4567")
1239 old_field = Note._meta.get_field("info")
1240 new_field = DateTimeField(blank=True)
1241 new_field.set_attributes_from_name("info")
1242 with connection.schema_editor() as editor:
1243 editor.alter_field(Note, old_field, new_field, strict=True)
1244 # Make sure the field isn't nullable
1245 columns = self.column_classes(Note)
1246 self.assertFalse(columns["info"][1][6])
1247
1248 def test_alter_text_field_to_time_field(self):
1249 """
1250 #25002 - Test conversion of text field to time field.
1251 """
1252 with connection.schema_editor() as editor:
1253 editor.create_model(Note)
1254 Note.objects.create(info="3:16:17.4567")
1255 old_field = Note._meta.get_field("info")
1256 new_field = TimeField(blank=True)
1257 new_field.set_attributes_from_name("info")
1258 with connection.schema_editor() as editor:
1259 editor.alter_field(Note, old_field, new_field, strict=True)
1260 # Make sure the field isn't nullable
1261 columns = self.column_classes(Note)
1262 self.assertFalse(columns["info"][1][6])
1263
1264 @skipIfDBFeature("interprets_empty_strings_as_nulls")
1265 def test_alter_textual_field_keep_null_status(self):
1266 """
1267 Changing a field type shouldn't affect the not null status.
1268 """
1269 with connection.schema_editor() as editor:
1270 editor.create_model(Note)
1271 with self.assertRaises(IntegrityError):
1272 Note.objects.create(info=None)
1273 old_field = Note._meta.get_field("info")
1274 new_field = CharField(max_length=50)
1275 new_field.set_attributes_from_name("info")
1276 with connection.schema_editor() as editor:
1277 editor.alter_field(Note, old_field, new_field, strict=True)
1278 with self.assertRaises(IntegrityError):
1279 Note.objects.create(info=None)
1280
1281 @skipUnlessDBFeature("interprets_empty_strings_as_nulls")
1282 def test_alter_textual_field_not_null_to_null(self):
1283 """
1284 Nullability for textual fields is preserved on databases that
1285 interpret empty strings as NULLs.
1286 """
1287 with connection.schema_editor() as editor:
1288 editor.create_model(Author)
1289 columns = self.column_classes(Author)
1290 # Field is nullable.
1291 self.assertTrue(columns["uuid"][1][6])
1292 # Change to NOT NULL.
1293 old_field = Author._meta.get_field("uuid")
1294 new_field = SlugField(null=False, blank=True)
1295 new_field.set_attributes_from_name("uuid")
1296 with connection.schema_editor() as editor:
1297 editor.alter_field(Author, old_field, new_field, strict=True)
1298 columns = self.column_classes(Author)
1299 # Nullability is preserved.
1300 self.assertTrue(columns["uuid"][1][6])
1301
1302 def test_alter_numeric_field_keep_null_status(self):
1303 """
1304 Changing a field type shouldn't affect the not null status.
1305 """
1306 with connection.schema_editor() as editor:
1307 editor.create_model(UniqueTest)
1308 with self.assertRaises(IntegrityError):
1309 UniqueTest.objects.create(year=None, slug="aaa")
1310 old_field = UniqueTest._meta.get_field("year")
1311 new_field = BigIntegerField()
1312 new_field.set_attributes_from_name("year")
1313 with connection.schema_editor() as editor:
1314 editor.alter_field(UniqueTest, old_field, new_field, strict=True)
1315 with self.assertRaises(IntegrityError):
1316 UniqueTest.objects.create(year=None, slug="bbb")
1317
1318 def test_alter_null_to_not_null(self):
1319 """
1320 #23609 - Tests handling of default values when altering from NULL to NOT NULL.
1321 """
1322 # Create the table
1323 with connection.schema_editor() as editor:
1324 editor.create_model(Author)
1325 # Ensure the field is right to begin with
1326 columns = self.column_classes(Author)
1327 self.assertTrue(columns["height"][1][6])
1328 # Create some test data
1329 Author.objects.create(name="Not null author", height=12)
1330 Author.objects.create(name="Null author")
1331 # Verify null value
1332 self.assertEqual(Author.objects.get(name="Not null author").height, 12)
1333 self.assertIsNone(Author.objects.get(name="Null author").height)
1334 # Alter the height field to NOT NULL with default
1335 old_field = Author._meta.get_field("height")
1336 new_field = PositiveIntegerField(default=42)
1337 new_field.set_attributes_from_name("height")
1338 with connection.schema_editor() as editor:
1339 editor.alter_field(Author, old_field, new_field, strict=True)
1340 columns = self.column_classes(Author)
1341 self.assertFalse(columns["height"][1][6])
1342 # Verify default value
1343 self.assertEqual(Author.objects.get(name="Not null author").height, 12)
1344 self.assertEqual(Author.objects.get(name="Null author").height, 42)
1345
1346 def test_alter_charfield_to_null(self):
1347 """
1348 #24307 - Should skip an alter statement on databases with
1349 interprets_empty_strings_as_nulls when changing a CharField to null.
1350 """
1351 # Create the table
1352 with connection.schema_editor() as editor:
1353 editor.create_model(Author)
1354 # Change the CharField to null
1355 old_field = Author._meta.get_field("name")
1356 new_field = copy(old_field)
1357 new_field.null = True
1358 with connection.schema_editor() as editor:
1359 editor.alter_field(Author, old_field, new_field, strict=True)
1360
1361 @unittest.skipUnless(connection.vendor == "postgresql", "PostgreSQL specific")
1362 def test_alter_char_field_decrease_length(self):
1363 # Create the table.
1364 with connection.schema_editor() as editor:
1365 editor.create_model(Author)
1366 Author.objects.create(name="x" * 255)
1367 # Change max_length of CharField.
1368 old_field = Author._meta.get_field("name")
1369 new_field = CharField(max_length=254)
1370 new_field.set_attributes_from_name("name")
1371 with connection.schema_editor() as editor:
1372 msg = "value too long for type character varying(254)"
1373 with self.assertRaisesMessage(DataError, msg):
1374 editor.alter_field(Author, old_field, new_field, strict=True)
1375
1376 @unittest.skipUnless(connection.vendor == "postgresql", "PostgreSQL specific")
1377 def test_alter_field_with_custom_db_type(self):
1378 from django.contrib.postgres.fields import ArrayField
1379
1380 class Foo(Model):
1381 field = ArrayField(CharField(max_length=255))
1382
1383 class Meta:
1384 app_label = "schema"
1385
1386 with connection.schema_editor() as editor:
1387 editor.create_model(Foo)
1388 self.isolated_local_models = [Foo]
1389 old_field = Foo._meta.get_field("field")
1390 new_field = ArrayField(CharField(max_length=16))
1391 new_field.set_attributes_from_name("field")
1392 new_field.model = Foo
1393 with connection.schema_editor() as editor:
1394 editor.alter_field(Foo, old_field, new_field, strict=True)
1395
1396 @isolate_apps("schema")
1397 @unittest.skipUnless(connection.vendor == "postgresql", "PostgreSQL specific")
1398 def test_alter_array_field_decrease_base_field_length(self):
1399 from django.contrib.postgres.fields import ArrayField
1400
1401 class ArrayModel(Model):
1402 field = ArrayField(CharField(max_length=16))
1403
1404 class Meta:
1405 app_label = "schema"
1406
1407 with connection.schema_editor() as editor:
1408 editor.create_model(ArrayModel)
1409 self.isolated_local_models = [ArrayModel]
1410 ArrayModel.objects.create(field=["x" * 16])
1411 old_field = ArrayModel._meta.get_field("field")
1412 new_field = ArrayField(CharField(max_length=15))
1413 new_field.set_attributes_from_name("field")
1414 new_field.model = ArrayModel
1415 with connection.schema_editor() as editor:
1416 msg = "value too long for type character varying(15)"
1417 with self.assertRaisesMessage(DataError, msg):
1418 editor.alter_field(ArrayModel, old_field, new_field, strict=True)
1419
1420 @isolate_apps("schema")
1421 @unittest.skipUnless(connection.vendor == "postgresql", "PostgreSQL specific")
1422 def test_alter_array_field_decrease_nested_base_field_length(self):
1423 from django.contrib.postgres.fields import ArrayField
1424
1425 class ArrayModel(Model):
1426 field = ArrayField(ArrayField(CharField(max_length=16)))
1427
1428 class Meta:
1429 app_label = "schema"
1430
1431 with connection.schema_editor() as editor:
1432 editor.create_model(ArrayModel)
1433 self.isolated_local_models = [ArrayModel]
1434 ArrayModel.objects.create(field=[["x" * 16]])
1435 old_field = ArrayModel._meta.get_field("field")
1436 new_field = ArrayField(ArrayField(CharField(max_length=15)))
1437 new_field.set_attributes_from_name("field")
1438 new_field.model = ArrayModel
1439 with connection.schema_editor() as editor:
1440 msg = "value too long for type character varying(15)"
1441 with self.assertRaisesMessage(DataError, msg):
1442 editor.alter_field(ArrayModel, old_field, new_field, strict=True)
1443
1444 def _add_ci_collation(self):
1445 ci_collation = "case_insensitive"
1446
1447 def drop_collation():
1448 with connection.cursor() as cursor:
1449 cursor.execute(f"DROP COLLATION IF EXISTS {ci_collation}")
1450
1451 with connection.cursor() as cursor:
1452 cursor.execute(
1453 f"CREATE COLLATION IF NOT EXISTS {ci_collation} (provider=icu, "
1454 f"locale='und-u-ks-level2', deterministic=false)"
1455 )
1456 self.addCleanup(drop_collation)
1457 return ci_collation
1458
1459 @isolate_apps("schema")
1460 @unittest.skipUnless(connection.vendor == "postgresql", "PostgreSQL specific")
1461 @skipUnlessDBFeature(
1462 "supports_collation_on_charfield",
1463 "supports_non_deterministic_collations",
1464 )
1465 def test_db_collation_arrayfield(self):
1466 from django.contrib.postgres.fields import ArrayField
1467
1468 ci_collation = self._add_ci_collation()
1469 cs_collation = "en-x-icu"
1470
1471 class ArrayModel(Model):
1472 field = ArrayField(CharField(max_length=16, db_collation=ci_collation))
1473
1474 class Meta:
1475 app_label = "schema"
1476
1477 # Create the table.
1478 with connection.schema_editor() as editor:
1479 editor.create_model(ArrayModel)
1480 self.isolated_local_models = [ArrayModel]
1481 self.assertEqual(
1482 self.get_column_collation(ArrayModel._meta.db_table, "field"),
1483 ci_collation,
1484 )
1485 # Alter collation.
1486 old_field = ArrayModel._meta.get_field("field")
1487 new_field_cs = ArrayField(CharField(max_length=16, db_collation=cs_collation))
1488 new_field_cs.set_attributes_from_name("field")
1489 new_field_cs.model = ArrayField
1490 with connection.schema_editor() as editor:
1491 editor.alter_field(ArrayModel, old_field, new_field_cs, strict=True)
1492 self.assertEqual(
1493 self.get_column_collation(ArrayModel._meta.db_table, "field"),
1494 cs_collation,
1495 )
1496
1497 @isolate_apps("schema")
1498 @unittest.skipUnless(connection.vendor == "postgresql", "PostgreSQL specific")
1499 @skipUnlessDBFeature(
1500 "supports_collation_on_charfield",
1501 "supports_non_deterministic_collations",
1502 )
1503 def test_unique_with_collation_charfield(self):
1504 ci_collation = self._add_ci_collation()
1505
1506 class CiCharModel(Model):
1507 field = CharField(max_length=16, db_collation=ci_collation, unique=True)
1508
1509 class Meta:
1510 app_label = "schema"
1511
1512 # Create the table.
1513 with connection.schema_editor() as editor:
1514 editor.create_model(CiCharModel)
1515 self.isolated_local_models = [CiCharModel]
1516 self.assertEqual(
1517 self.get_column_collation(CiCharModel._meta.db_table, "field"),
1518 ci_collation,
1519 )
1520 self.assertIn("field", self.get_uniques(CiCharModel._meta.db_table))
1521
1522 @isolate_apps("schema")
1523 @unittest.skipUnless(connection.vendor == "postgresql", "PostgreSQL specific")
1524 @skipUnlessDBFeature("supports_collation_on_charfield")
1525 def test_unique_with_deterministic_collation_charfield(self):
1526 deterministic_collation = connection.features.test_collations.get(
1527 "deterministic"
1528 )
1529 if not deterministic_collation:
1530 self.skipTest("This backend does not support deterministic collations.")
1531
1532 class CharModel(Model):
1533 field = CharField(db_collation=deterministic_collation, unique=True)
1534
1535 class Meta:
1536 app_label = "schema"
1537
1538 # Create the table.
1539 with connection.schema_editor() as editor:
1540 editor.create_model(CharModel)
1541 self.isolated_local_models = [CharModel]
1542 constraints = self.get_constraints_for_column(
1543 CharModel, CharModel._meta.get_field("field").column
1544 )
1545 self.assertIn("schema_charmodel_field_8b338dea_like", constraints)
1546 self.assertIn(
1547 "varchar_pattern_ops",
1548 self.get_constraint_opclasses("schema_charmodel_field_8b338dea_like"),
1549 )
1550 self.assertEqual(
1551 self.get_column_collation(CharModel._meta.db_table, "field"),
1552 deterministic_collation,
1553 )
1554 self.assertIn("field", self.get_uniques(CharModel._meta.db_table))
1555
1556 @isolate_apps("schema")
1557 @unittest.skipUnless(connection.vendor == "postgresql", "PostgreSQL specific")
1558 @skipUnlessDBFeature(
1559 "supports_collation_on_charfield",
1560 "supports_non_deterministic_collations",
1561 )
1562 def test_relation_to_collation_charfield(self):
1563 ci_collation = self._add_ci_collation()
1564
1565 class CiCharModel(Model):
1566 field = CharField(max_length=16, db_collation=ci_collation, unique=True)
1567
1568 class Meta:
1569 app_label = "schema"
1570
1571 class RelationModel(Model):
1572 field = OneToOneField(CiCharModel, CASCADE, to_field="field")
1573
1574 class Meta:
1575 app_label = "schema"
1576
1577 # Create the table.
1578 with connection.schema_editor() as editor:
1579 editor.create_model(CiCharModel)
1580 editor.create_model(RelationModel)
1581 self.isolated_local_models = [CiCharModel, RelationModel]
1582 self.assertEqual(
1583 self.get_column_collation(RelationModel._meta.db_table, "field_id"),
1584 ci_collation,
1585 )
1586 self.assertEqual(
1587 self.get_column_collation(CiCharModel._meta.db_table, "field"),
1588 ci_collation,
1589 )
1590 self.assertIn("field_id", self.get_uniques(RelationModel._meta.db_table))
1591
1592 @isolate_apps("schema")
1593 @unittest.skipUnless(connection.vendor == "postgresql", "PostgreSQL specific")
1594 @skipUnlessDBFeature("supports_collation_on_charfield")
1595 def test_relation_to_deterministic_collation_charfield(self):
1596 deterministic_collation = connection.features.test_collations.get(
1597 "deterministic"
1598 )
1599 if not deterministic_collation:
1600 self.skipTest("This backend does not support deterministic collations.")
1601
1602 class CharModel(Model):
1603 field = CharField(db_collation=deterministic_collation, unique=True)
1604
1605 class Meta:
1606 app_label = "schema"
1607
1608 class RelationModel(Model):
1609 field = OneToOneField(CharModel, CASCADE, to_field="field")
1610
1611 class Meta:
1612 app_label = "schema"
1613
1614 # Create the table.
1615 with connection.schema_editor() as editor:
1616 editor.create_model(CharModel)
1617 editor.create_model(RelationModel)
1618 self.isolated_local_models = [CharModel, RelationModel]
1619 constraints = self.get_constraints_for_column(
1620 CharModel, CharModel._meta.get_field("field").column
1621 )
1622 self.assertIn("schema_charmodel_field_8b338dea_like", constraints)
1623 self.assertIn(
1624 "varchar_pattern_ops",
1625 self.get_constraint_opclasses("schema_charmodel_field_8b338dea_like"),
1626 )
1627 rel_constraints = self.get_constraints_for_column(
1628 RelationModel, RelationModel._meta.get_field("field").column
1629 )
1630 self.assertIn("schema_relationmodel_field_id_395fbb08_like", rel_constraints)
1631 self.assertIn(
1632 "varchar_pattern_ops",
1633 self.get_constraint_opclasses(
1634 "schema_relationmodel_field_id_395fbb08_like"
1635 ),
1636 )
1637 self.assertEqual(
1638 self.get_column_collation(RelationModel._meta.db_table, "field_id"),
1639 deterministic_collation,
1640 )
1641 self.assertEqual(
1642 self.get_column_collation(CharModel._meta.db_table, "field"),
1643 deterministic_collation,
1644 )
1645 self.assertIn("field_id", self.get_uniques(RelationModel._meta.db_table))
1646
1647 def test_alter_textfield_to_null(self):
1648 """
1649 #24307 - Should skip an alter statement on databases with
1650 interprets_empty_strings_as_nulls when changing a TextField to null.
1651 """
1652 # Create the table
1653 with connection.schema_editor() as editor:
1654 editor.create_model(Note)
1655 # Change the TextField to null
1656 old_field = Note._meta.get_field("info")
1657 new_field = copy(old_field)
1658 new_field.null = True
1659 with connection.schema_editor() as editor:
1660 editor.alter_field(Note, old_field, new_field, strict=True)
1661
1662 def test_alter_null_to_not_null_keeping_default(self):
1663 """
1664 #23738 - Can change a nullable field with default to non-nullable
1665 with the same default.
1666 """
1667 # Create the table
1668 with connection.schema_editor() as editor:
1669 editor.create_model(AuthorWithDefaultHeight)
1670 # Ensure the field is right to begin with
1671 columns = self.column_classes(AuthorWithDefaultHeight)
1672 self.assertTrue(columns["height"][1][6])
1673 # Alter the height field to NOT NULL keeping the previous default
1674 old_field = AuthorWithDefaultHeight._meta.get_field("height")
1675 new_field = PositiveIntegerField(default=42)
1676 new_field.set_attributes_from_name("height")
1677 with connection.schema_editor() as editor:
1678 editor.alter_field(
1679 AuthorWithDefaultHeight, old_field, new_field, strict=True
1680 )
1681 columns = self.column_classes(AuthorWithDefaultHeight)
1682 self.assertFalse(columns["height"][1][6])
1683
1684 @skipUnlessDBFeature("supports_foreign_keys")
1685 def test_alter_fk(self):
1686 """
1687 Tests altering of FKs
1688 """
1689 # Create the table
1690 with connection.schema_editor() as editor:
1691 editor.create_model(Author)
1692 editor.create_model(Book)
1693 # Ensure the field is right to begin with
1694 columns = self.column_classes(Book)
1695 self.assertEqual(
1696 columns["author_id"][0],
1697 connection.features.introspected_field_types["IntegerField"],
1698 )
1699 self.assertForeignKeyExists(Book, "author_id", "schema_author")
1700 # Alter the FK
1701 old_field = Book._meta.get_field("author")
1702 new_field = ForeignKey(Author, CASCADE, editable=False)
1703 new_field.set_attributes_from_name("author")
1704 with connection.schema_editor() as editor:
1705 editor.alter_field(Book, old_field, new_field, strict=True)
1706 columns = self.column_classes(Book)
1707 self.assertEqual(
1708 columns["author_id"][0],
1709 connection.features.introspected_field_types["IntegerField"],
1710 )
1711 self.assertForeignKeyExists(Book, "author_id", "schema_author")
1712
1713 @skipUnlessDBFeature("supports_foreign_keys")
1714 def test_alter_to_fk(self):
1715 """
1716 #24447 - Tests adding a FK constraint for an existing column
1717 """
1718
1719 class LocalBook(Model):
1720 author = IntegerField()
1721 title = CharField(max_length=100, db_index=True)
1722 pub_date = DateTimeField()
1723
1724 class Meta:
1725 app_label = "schema"
1726 apps = new_apps
1727
1728 self.local_models = [LocalBook]
1729
1730 # Create the tables
1731 with connection.schema_editor() as editor:
1732 editor.create_model(Author)
1733 editor.create_model(LocalBook)
1734 # Ensure no FK constraint exists
1735 constraints = self.get_constraints(LocalBook._meta.db_table)
1736 for details in constraints.values():
1737 if details["foreign_key"]:
1738 self.fail(
1739 "Found an unexpected FK constraint to %s" % details["columns"]
1740 )
1741 old_field = LocalBook._meta.get_field("author")
1742 new_field = ForeignKey(Author, CASCADE)
1743 new_field.set_attributes_from_name("author")
1744 with connection.schema_editor() as editor:
1745 editor.alter_field(LocalBook, old_field, new_field, strict=True)
1746 self.assertForeignKeyExists(LocalBook, "author_id", "schema_author")
1747
1748 @skipUnlessDBFeature("supports_foreign_keys", "can_introspect_foreign_keys")
1749 def test_alter_o2o_to_fk(self):
1750 """
1751 #24163 - Tests altering of OneToOneField to ForeignKey
1752 """
1753 # Create the table
1754 with connection.schema_editor() as editor:
1755 editor.create_model(Author)
1756 editor.create_model(BookWithO2O)
1757 # Ensure the field is right to begin with
1758 columns = self.column_classes(BookWithO2O)
1759 self.assertEqual(
1760 columns["author_id"][0],
1761 connection.features.introspected_field_types["IntegerField"],
1762 )
1763 # Ensure the field is unique
1764 author = Author.objects.create(name="Joe")
1765 BookWithO2O.objects.create(
1766 author=author, title="Django 1", pub_date=datetime.datetime.now()
1767 )
1768 with self.assertRaises(IntegrityError):
1769 BookWithO2O.objects.create(
1770 author=author, title="Django 2", pub_date=datetime.datetime.now()
1771 )
1772 BookWithO2O.objects.all().delete()
1773 self.assertForeignKeyExists(BookWithO2O, "author_id", "schema_author")
1774 # Alter the OneToOneField to ForeignKey
1775 old_field = BookWithO2O._meta.get_field("author")
1776 new_field = ForeignKey(Author, CASCADE)
1777 new_field.set_attributes_from_name("author")
1778 with connection.schema_editor() as editor:
1779 editor.alter_field(BookWithO2O, old_field, new_field, strict=True)
1780 columns = self.column_classes(Book)
1781 self.assertEqual(
1782 columns["author_id"][0],
1783 connection.features.introspected_field_types["IntegerField"],
1784 )
1785 # Ensure the field is not unique anymore
1786 Book.objects.create(
1787 author=author, title="Django 1", pub_date=datetime.datetime.now()
1788 )
1789 Book.objects.create(
1790 author=author, title="Django 2", pub_date=datetime.datetime.now()
1791 )
1792 self.assertForeignKeyExists(Book, "author_id", "schema_author")
1793
1794 @skipUnlessDBFeature("supports_foreign_keys", "can_introspect_foreign_keys")
1795 def test_alter_fk_to_o2o(self):
1796 """
1797 #24163 - Tests altering of ForeignKey to OneToOneField
1798 """
1799 # Create the table
1800 with connection.schema_editor() as editor:
1801 editor.create_model(Author)
1802 editor.create_model(Book)
1803 # Ensure the field is right to begin with
1804 columns = self.column_classes(Book)
1805 self.assertEqual(
1806 columns["author_id"][0],
1807 connection.features.introspected_field_types["IntegerField"],
1808 )
1809 # Ensure the field is not unique
1810 author = Author.objects.create(name="Joe")
1811 Book.objects.create(
1812 author=author, title="Django 1", pub_date=datetime.datetime.now()
1813 )
1814 Book.objects.create(
1815 author=author, title="Django 2", pub_date=datetime.datetime.now()
1816 )
1817 Book.objects.all().delete()
1818 self.assertForeignKeyExists(Book, "author_id", "schema_author")
1819 # Alter the ForeignKey to OneToOneField
1820 old_field = Book._meta.get_field("author")
1821 new_field = OneToOneField(Author, CASCADE)
1822 new_field.set_attributes_from_name("author")
1823 with connection.schema_editor() as editor:
1824 editor.alter_field(Book, old_field, new_field, strict=True)
1825 columns = self.column_classes(BookWithO2O)
1826 self.assertEqual(
1827 columns["author_id"][0],
1828 connection.features.introspected_field_types["IntegerField"],
1829 )
1830 # Ensure the field is unique now
1831 BookWithO2O.objects.create(
1832 author=author, title="Django 1", pub_date=datetime.datetime.now()
1833 )
1834 with self.assertRaises(IntegrityError):
1835 BookWithO2O.objects.create(
1836 author=author, title="Django 2", pub_date=datetime.datetime.now()
1837 )
1838 self.assertForeignKeyExists(BookWithO2O, "author_id", "schema_author")
1839
1840 def test_alter_field_fk_to_o2o(self):
1841 with connection.schema_editor() as editor:
1842 editor.create_model(Author)
1843 editor.create_model(Book)
1844 expected_fks = (
1845 1
1846 if connection.features.supports_foreign_keys
1847 and connection.features.can_introspect_foreign_keys
1848 else 0
1849 )
1850 expected_indexes = 1 if connection.features.indexes_foreign_keys else 0
1851
1852 # Check the index is right to begin with.
1853 counts = self.get_constraints_count(
1854 Book._meta.db_table,
1855 Book._meta.get_field("author").column,
1856 (Author._meta.db_table, Author._meta.pk.column),
1857 )
1858 self.assertEqual(
1859 counts,
1860 {"fks": expected_fks, "uniques": 0, "indexes": expected_indexes},
1861 )
1862
1863 old_field = Book._meta.get_field("author")
1864 new_field = OneToOneField(Author, CASCADE)
1865 new_field.set_attributes_from_name("author")
1866 with connection.schema_editor() as editor:
1867 editor.alter_field(Book, old_field, new_field)
1868
1869 counts = self.get_constraints_count(
1870 Book._meta.db_table,
1871 Book._meta.get_field("author").column,
1872 (Author._meta.db_table, Author._meta.pk.column),
1873 )
1874 # The index on ForeignKey is replaced with a unique constraint for
1875 # OneToOneField.
1876 self.assertEqual(counts, {"fks": expected_fks, "uniques": 1, "indexes": 0})
1877
1878 def test_autofield_to_o2o(self):
1879 with connection.schema_editor() as editor:
1880 editor.create_model(Author)
1881 editor.create_model(Note)
1882
1883 # Rename the field.
1884 old_field = Author._meta.get_field("id")
1885 new_field = AutoField(primary_key=True)
1886 new_field.set_attributes_from_name("note_ptr")
1887 new_field.model = Author
1888
1889 with connection.schema_editor() as editor:
1890 editor.alter_field(Author, old_field, new_field, strict=True)
1891 # Alter AutoField to OneToOneField.
1892 new_field_o2o = OneToOneField(Note, CASCADE)
1893 new_field_o2o.set_attributes_from_name("note_ptr")
1894 new_field_o2o.model = Author
1895
1896 with connection.schema_editor() as editor:
1897 editor.alter_field(Author, new_field, new_field_o2o, strict=True)
1898 columns = self.column_classes(Author)
1899 field_type, _ = columns["note_ptr_id"]
1900 self.assertEqual(
1901 field_type, connection.features.introspected_field_types["IntegerField"]
1902 )
1903
1904 def test_alter_field_fk_keeps_index(self):
1905 with connection.schema_editor() as editor:
1906 editor.create_model(Author)
1907 editor.create_model(Book)
1908 expected_fks = (
1909 1
1910 if connection.features.supports_foreign_keys
1911 and connection.features.can_introspect_foreign_keys
1912 else 0
1913 )
1914 expected_indexes = 1 if connection.features.indexes_foreign_keys else 0
1915
1916 # Check the index is right to begin with.
1917 counts = self.get_constraints_count(
1918 Book._meta.db_table,
1919 Book._meta.get_field("author").column,
1920 (Author._meta.db_table, Author._meta.pk.column),
1921 )
1922 self.assertEqual(
1923 counts,
1924 {"fks": expected_fks, "uniques": 0, "indexes": expected_indexes},
1925 )
1926
1927 old_field = Book._meta.get_field("author")
1928 # on_delete changed from CASCADE.
1929 new_field = ForeignKey(Author, PROTECT)
1930 new_field.set_attributes_from_name("author")
1931 with connection.schema_editor() as editor:
1932 editor.alter_field(Book, old_field, new_field, strict=True)
1933
1934 counts = self.get_constraints_count(
1935 Book._meta.db_table,
1936 Book._meta.get_field("author").column,
1937 (Author._meta.db_table, Author._meta.pk.column),
1938 )
1939 # The index remains.
1940 self.assertEqual(
1941 counts,
1942 {"fks": expected_fks, "uniques": 0, "indexes": expected_indexes},
1943 )
1944
1945 def test_alter_field_o2o_to_fk(self):
1946 with connection.schema_editor() as editor:
1947 editor.create_model(Author)
1948 editor.create_model(BookWithO2O)
1949 expected_fks = (
1950 1
1951 if connection.features.supports_foreign_keys
1952 and connection.features.can_introspect_foreign_keys
1953 else 0
1954 )
1955
1956 # Check the unique constraint is right to begin with.
1957 counts = self.get_constraints_count(
1958 BookWithO2O._meta.db_table,
1959 BookWithO2O._meta.get_field("author").column,
1960 (Author._meta.db_table, Author._meta.pk.column),
1961 )
1962 self.assertEqual(counts, {"fks": expected_fks, "uniques": 1, "indexes": 0})
1963
1964 old_field = BookWithO2O._meta.get_field("author")
1965 new_field = ForeignKey(Author, CASCADE)
1966 new_field.set_attributes_from_name("author")
1967 with connection.schema_editor() as editor:
1968 editor.alter_field(BookWithO2O, old_field, new_field)
1969
1970 counts = self.get_constraints_count(
1971 BookWithO2O._meta.db_table,
1972 BookWithO2O._meta.get_field("author").column,
1973 (Author._meta.db_table, Author._meta.pk.column),
1974 )
1975 # The unique constraint on OneToOneField is replaced with an index for
1976 # ForeignKey.
1977 self.assertEqual(counts, {"fks": expected_fks, "uniques": 0, "indexes": 1})
1978
1979 def test_alter_field_o2o_keeps_unique(self):
1980 with connection.schema_editor() as editor:
1981 editor.create_model(Author)
1982 editor.create_model(BookWithO2O)
1983 expected_fks = (
1984 1
1985 if connection.features.supports_foreign_keys
1986 and connection.features.can_introspect_foreign_keys
1987 else 0
1988 )
1989
1990 # Check the unique constraint is right to begin with.
1991 counts = self.get_constraints_count(
1992 BookWithO2O._meta.db_table,
1993 BookWithO2O._meta.get_field("author").column,
1994 (Author._meta.db_table, Author._meta.pk.column),
1995 )
1996 self.assertEqual(counts, {"fks": expected_fks, "uniques": 1, "indexes": 0})
1997
1998 old_field = BookWithO2O._meta.get_field("author")
1999 # on_delete changed from CASCADE.
2000 new_field = OneToOneField(Author, PROTECT)
2001 new_field.set_attributes_from_name("author")
2002 with connection.schema_editor() as editor:
2003 editor.alter_field(BookWithO2O, old_field, new_field, strict=True)
2004
2005 counts = self.get_constraints_count(
2006 BookWithO2O._meta.db_table,
2007 BookWithO2O._meta.get_field("author").column,
2008 (Author._meta.db_table, Author._meta.pk.column),
2009 )
2010 # The unique constraint remains.
2011 self.assertEqual(counts, {"fks": expected_fks, "uniques": 1, "indexes": 0})
2012
2013 @skipUnlessDBFeature("ignores_table_name_case")
2014 def test_alter_db_table_case(self):
2015 # Create the table
2016 with connection.schema_editor() as editor:
2017 editor.create_model(Author)
2018 # Alter the case of the table
2019 old_table_name = Author._meta.db_table
2020 with connection.schema_editor() as editor:
2021 editor.alter_db_table(Author, old_table_name, old_table_name.upper())
2022
2023 def test_alter_implicit_id_to_explicit(self):
2024 """
2025 Should be able to convert an implicit "id" field to an explicit "id"
2026 primary key field.
2027 """
2028 with connection.schema_editor() as editor:
2029 editor.create_model(Author)
2030
2031 old_field = Author._meta.get_field("id")
2032 new_field = AutoField(primary_key=True)
2033 new_field.set_attributes_from_name("id")
2034 new_field.model = Author
2035 with connection.schema_editor() as editor:
2036 editor.alter_field(Author, old_field, new_field, strict=True)
2037 # This will fail if DROP DEFAULT is inadvertently executed on this
2038 # field which drops the id sequence, at least on PostgreSQL.
2039 Author.objects.create(name="Foo")
2040 Author.objects.create(name="Bar")
2041
2042 def test_alter_autofield_pk_to_bigautofield_pk(self):
2043 with connection.schema_editor() as editor:
2044 editor.create_model(Author)
2045 old_field = Author._meta.get_field("id")
2046 new_field = BigAutoField(primary_key=True)
2047 new_field.set_attributes_from_name("id")
2048 new_field.model = Author
2049 with connection.schema_editor() as editor:
2050 editor.alter_field(Author, old_field, new_field, strict=True)
2051
2052 Author.objects.create(name="Foo", pk=1)
2053 with connection.cursor() as cursor:
2054 sequence_reset_sqls = connection.ops.sequence_reset_sql(
2055 no_style(), [Author]
2056 )
2057 if sequence_reset_sqls:
2058 cursor.execute(sequence_reset_sqls[0])
2059 self.assertIsNotNone(Author.objects.create(name="Bar"))
2060
2061 def test_alter_autofield_pk_to_smallautofield_pk(self):
2062 with connection.schema_editor() as editor:
2063 editor.create_model(Author)
2064 old_field = Author._meta.get_field("id")
2065 new_field = SmallAutoField(primary_key=True)
2066 new_field.set_attributes_from_name("id")
2067 new_field.model = Author
2068 with connection.schema_editor() as editor:
2069 editor.alter_field(Author, old_field, new_field, strict=True)
2070
2071 Author.objects.create(name="Foo", pk=1)
2072 with connection.cursor() as cursor:
2073 sequence_reset_sqls = connection.ops.sequence_reset_sql(
2074 no_style(), [Author]
2075 )
2076 if sequence_reset_sqls:
2077 cursor.execute(sequence_reset_sqls[0])
2078 self.assertIsNotNone(Author.objects.create(name="Bar"))
2079
2080 def test_alter_int_pk_to_autofield_pk(self):
2081 """
2082 Should be able to rename an IntegerField(primary_key=True) to
2083 AutoField(primary_key=True).
2084 """
2085 with connection.schema_editor() as editor:
2086 editor.create_model(IntegerPK)
2087
2088 old_field = IntegerPK._meta.get_field("i")
2089 new_field = AutoField(primary_key=True)
2090 new_field.model = IntegerPK
2091 new_field.set_attributes_from_name("i")
2092
2093 with connection.schema_editor() as editor:
2094 editor.alter_field(IntegerPK, old_field, new_field, strict=True)
2095
2096 # A model representing the updated model.
2097 class IntegerPKToAutoField(Model):
2098 i = AutoField(primary_key=True)
2099 j = IntegerField(unique=True)
2100
2101 class Meta:
2102 app_label = "schema"
2103 apps = new_apps
2104 db_table = IntegerPK._meta.db_table
2105
2106 # An id (i) is generated by the database.
2107 obj = IntegerPKToAutoField.objects.create(j=1)
2108 self.assertIsNotNone(obj.i)
2109
2110 def test_alter_int_pk_to_bigautofield_pk(self):
2111 """
2112 Should be able to rename an IntegerField(primary_key=True) to
2113 BigAutoField(primary_key=True).
2114 """
2115 with connection.schema_editor() as editor:
2116 editor.create_model(IntegerPK)
2117
2118 old_field = IntegerPK._meta.get_field("i")
2119 new_field = BigAutoField(primary_key=True)
2120 new_field.model = IntegerPK
2121 new_field.set_attributes_from_name("i")
2122
2123 with connection.schema_editor() as editor:
2124 editor.alter_field(IntegerPK, old_field, new_field, strict=True)
2125
2126 # A model representing the updated model.
2127 class IntegerPKToBigAutoField(Model):
2128 i = BigAutoField(primary_key=True)
2129 j = IntegerField(unique=True)
2130
2131 class Meta:
2132 app_label = "schema"
2133 apps = new_apps
2134 db_table = IntegerPK._meta.db_table
2135
2136 # An id (i) is generated by the database.
2137 obj = IntegerPKToBigAutoField.objects.create(j=1)
2138 self.assertIsNotNone(obj.i)
2139
2140 @isolate_apps("schema")
2141 def test_alter_smallint_pk_to_smallautofield_pk(self):
2142 """
2143 Should be able to rename an SmallIntegerField(primary_key=True) to
2144 SmallAutoField(primary_key=True).
2145 """
2146
2147 class SmallIntegerPK(Model):
2148 i = SmallIntegerField(primary_key=True)
2149
2150 class Meta:
2151 app_label = "schema"
2152
2153 with connection.schema_editor() as editor:
2154 editor.create_model(SmallIntegerPK)
2155 self.isolated_local_models = [SmallIntegerPK]
2156 old_field = SmallIntegerPK._meta.get_field("i")
2157 new_field = SmallAutoField(primary_key=True)
2158 new_field.model = SmallIntegerPK
2159 new_field.set_attributes_from_name("i")
2160 with connection.schema_editor() as editor:
2161 editor.alter_field(SmallIntegerPK, old_field, new_field, strict=True)
2162
2163 # A model representing the updated model.
2164 class IntegerPKToSmallAutoField(Model):
2165 i = SmallAutoField(primary_key=True)
2166
2167 class Meta:
2168 app_label = "schema"
2169 apps = new_apps
2170 db_table = SmallIntegerPK._meta.db_table
2171
2172 # An id (i) is generated by the database.
2173 obj = IntegerPKToSmallAutoField.objects.create()
2174 self.assertIsNotNone(obj.i)
2175
2176 @isolate_apps("schema")
2177 @unittest.skipUnless(connection.vendor == "postgresql", "PostgreSQL specific")
2178 def test_alter_serial_auto_field_to_bigautofield(self):
2179 class SerialAutoField(Model):
2180 id = SmallAutoField(primary_key=True)
2181
2182 class Meta:
2183 app_label = "schema"
2184
2185 table = SerialAutoField._meta.db_table
2186 column = SerialAutoField._meta.get_field("id").column
2187 with connection.cursor() as cursor:
2188 cursor.execute(
2189 f'CREATE TABLE "{table}" '
2190 f'("{column}" smallserial NOT NULL PRIMARY KEY)'
2191 )
2192 try:
2193 old_field = SerialAutoField._meta.get_field("id")
2194 new_field = BigAutoField(primary_key=True)
2195 new_field.model = SerialAutoField
2196 new_field.set_attributes_from_name("id")
2197 with connection.schema_editor() as editor:
2198 editor.alter_field(SerialAutoField, old_field, new_field, strict=True)
2199 sequence_name = f"{table}_{column}_seq"
2200 with connection.cursor() as cursor:
2201 cursor.execute(
2202 "SELECT data_type FROM pg_sequences WHERE sequencename = %s",
2203 [sequence_name],
2204 )
2205 row = cursor.fetchone()
2206 sequence_data_type = row[0] if row and row[0] else None
2207 self.assertEqual(sequence_data_type, "bigint")
2208 # Rename the column.
2209 old_field = new_field
2210 new_field = AutoField(primary_key=True)
2211 new_field.model = SerialAutoField
2212 new_field.set_attributes_from_name("renamed_id")
2213 with connection.schema_editor() as editor:
2214 editor.alter_field(SerialAutoField, old_field, new_field, strict=True)
2215 with connection.cursor() as cursor:
2216 cursor.execute(
2217 "SELECT data_type FROM pg_sequences WHERE sequencename = %s",
2218 [sequence_name],
2219 )
2220 row = cursor.fetchone()
2221 sequence_data_type = row[0] if row and row[0] else None
2222 self.assertEqual(sequence_data_type, "integer")
2223 finally:
2224 with connection.cursor() as cursor:
2225 cursor.execute(f'DROP TABLE "{table}"')
2226
2227 def test_alter_int_pk_to_int_unique(self):
2228 """
2229 Should be able to rename an IntegerField(primary_key=True) to
2230 IntegerField(unique=True).
2231 """
2232 with connection.schema_editor() as editor:
2233 editor.create_model(IntegerPK)
2234 # Delete the old PK
2235 old_field = IntegerPK._meta.get_field("i")
2236 new_field = IntegerField(unique=True)
2237 new_field.model = IntegerPK
2238 new_field.set_attributes_from_name("i")
2239 with connection.schema_editor() as editor:
2240 editor.alter_field(IntegerPK, old_field, new_field, strict=True)
2241 # The primary key constraint is gone. Result depends on database:
2242 # 'id' for SQLite, None for others (must not be 'i').
2243 self.assertIn(self.get_primary_key(IntegerPK._meta.db_table), ("id", None))
2244
2245 # Set up a model class as it currently stands. The original IntegerPK
2246 # class is now out of date and some backends make use of the whole
2247 # model class when modifying a field (such as sqlite3 when remaking a
2248 # table) so an outdated model class leads to incorrect results.
2249 class Transitional(Model):
2250 i = IntegerField(unique=True)
2251 j = IntegerField(unique=True)
2252
2253 class Meta:
2254 app_label = "schema"
2255 apps = new_apps
2256 db_table = "INTEGERPK"
2257
2258 # model requires a new PK
2259 old_field = Transitional._meta.get_field("j")
2260 new_field = IntegerField(primary_key=True)
2261 new_field.model = Transitional
2262 new_field.set_attributes_from_name("j")
2263
2264 with connection.schema_editor() as editor:
2265 editor.alter_field(Transitional, old_field, new_field, strict=True)
2266
2267 # Create a model class representing the updated model.
2268 class IntegerUnique(Model):
2269 i = IntegerField(unique=True)
2270 j = IntegerField(primary_key=True)
2271
2272 class Meta:
2273 app_label = "schema"
2274 apps = new_apps
2275 db_table = "INTEGERPK"
2276
2277 # Ensure unique constraint works.
2278 IntegerUnique.objects.create(i=1, j=1)
2279 with self.assertRaises(IntegrityError):
2280 IntegerUnique.objects.create(i=1, j=2)
2281
2282 def test_rename(self):
2283 """
2284 Tests simple altering of fields
2285 """
2286 # Create the table
2287 with connection.schema_editor() as editor:
2288 editor.create_model(Author)
2289 # Ensure the field is right to begin with
2290 columns = self.column_classes(Author)
2291 self.assertEqual(
2292 columns["name"][0],
2293 connection.features.introspected_field_types["CharField"],
2294 )
2295 self.assertNotIn("display_name", columns)
2296 # Alter the name field's name
2297 old_field = Author._meta.get_field("name")
2298 new_field = CharField(max_length=254)
2299 new_field.set_attributes_from_name("display_name")
2300 with connection.schema_editor() as editor:
2301 editor.alter_field(Author, old_field, new_field, strict=True)
2302 columns = self.column_classes(Author)
2303 self.assertEqual(
2304 columns["display_name"][0],
2305 connection.features.introspected_field_types["CharField"],
2306 )
2307 self.assertNotIn("name", columns)
2308
2309 @isolate_apps("schema")
2310 def test_rename_referenced_field(self):
2311 class Author(Model):
2312 name = CharField(max_length=255, unique=True)
2313
2314 class Meta:
2315 app_label = "schema"
2316
2317 class Book(Model):
2318 author = ForeignKey(Author, CASCADE, to_field="name")
2319
2320 class Meta:
2321 app_label = "schema"
2322
2323 with connection.schema_editor() as editor:
2324 editor.create_model(Author)
2325 editor.create_model(Book)
2326 new_field = CharField(max_length=255, unique=True)
2327 new_field.set_attributes_from_name("renamed")
2328 with connection.schema_editor() as editor:
2329 editor.alter_field(Author, Author._meta.get_field("name"), new_field)
2330 # Ensure the foreign key reference was updated.
2331 self.assertForeignKeyExists(Book, "author_id", "schema_author", "renamed")
2332
2333 @skipIfDBFeature("interprets_empty_strings_as_nulls")
2334 def test_rename_keep_null_status(self):
2335 """
2336 Renaming a field shouldn't affect the not null status.
2337 """
2338 with connection.schema_editor() as editor:
2339 editor.create_model(Note)
2340 with self.assertRaises(IntegrityError):
2341 Note.objects.create(info=None)
2342 old_field = Note._meta.get_field("info")
2343 new_field = TextField()
2344 new_field.set_attributes_from_name("detail_info")
2345 with connection.schema_editor() as editor:
2346 editor.alter_field(Note, old_field, new_field, strict=True)
2347 columns = self.column_classes(Note)
2348 self.assertEqual(columns["detail_info"][0], "TextField")
2349 self.assertNotIn("info", columns)
2350 with self.assertRaises(IntegrityError):
2351 NoteRename.objects.create(detail_info=None)
2352
2353 @isolate_apps("schema")
2354 def test_rename_keep_db_default(self):
2355 """Renaming a field shouldn't affect a database default."""
2356
2357 class AuthorDbDefault(Model):
2358 birth_year = IntegerField(db_default=1985)
2359
2360 class Meta:
2361 app_label = "schema"
2362
2363 self.isolated_local_models = [AuthorDbDefault]
2364 with connection.schema_editor() as editor:
2365 editor.create_model(AuthorDbDefault)
2366 columns = self.column_classes(AuthorDbDefault)
2367 self.assertEqual(columns["birth_year"][1].default, "1985")
2368
2369 old_field = AuthorDbDefault._meta.get_field("birth_year")
2370 new_field = IntegerField(db_default=1985)
2371 new_field.set_attributes_from_name("renamed_year")
2372 new_field.model = AuthorDbDefault
2373 with connection.schema_editor() as editor:
2374 editor.alter_field(AuthorDbDefault, old_field, new_field, strict=True)
2375 columns = self.column_classes(AuthorDbDefault)
2376 self.assertEqual(columns["renamed_year"][1].default, "1985")
2377
2378 @isolate_apps("schema")
2379 def test_add_field_both_defaults_preserves_db_default(self):
2380 class Author(Model):
2381 class Meta:
2382 app_label = "schema"
2383
2384 with connection.schema_editor() as editor:
2385 editor.create_model(Author)
2386
2387 field = IntegerField(default=1985, db_default=1988)
2388 field.set_attributes_from_name("birth_year")
2389 field.model = Author
2390 with connection.schema_editor() as editor:
2391 editor.add_field(Author, field)
2392 columns = self.column_classes(Author)
2393 self.assertEqual(columns["birth_year"][1].default, "1988")
2394
2395 @isolate_apps("schema")
2396 def test_add_text_field_with_db_default(self):
2397 class Author(Model):
2398 description = TextField(db_default="(missing)")
2399
2400 class Meta:
2401 app_label = "schema"
2402
2403 with connection.schema_editor() as editor:
2404 editor.create_model(Author)
2405 columns = self.column_classes(Author)
2406 self.assertIn("(missing)", columns["description"][1].default)
2407
2408 @isolate_apps("schema")
2409 def test_db_default_equivalent_sql_noop(self):
2410 class Author(Model):
2411 name = TextField(db_default=Value("foo"))
2412
2413 class Meta:
2414 app_label = "schema"
2415
2416 with connection.schema_editor() as editor:
2417 editor.create_model(Author)
2418
2419 new_field = TextField(db_default="foo")
2420 new_field.set_attributes_from_name("name")
2421 new_field.model = Author
2422 with connection.schema_editor() as editor, self.assertNumQueries(0):
2423 editor.alter_field(Author, Author._meta.get_field("name"), new_field)
2424
2425 @isolate_apps("schema")
2426 def test_db_default_output_field_resolving(self):
2427 class Author(Model):
2428 data = JSONField(
2429 encoder=DjangoJSONEncoder,
2430 db_default={
2431 "epoch": datetime.datetime(1970, 1, 1, tzinfo=datetime.timezone.utc)
2432 },
2433 )
2434
2435 class Meta:
2436 app_label = "schema"
2437
2438 with connection.schema_editor() as editor:
2439 editor.create_model(Author)
2440
2441 author = Author.objects.create()
2442 author.refresh_from_db()
2443 self.assertEqual(author.data, {"epoch": "1970-01-01T00:00:00Z"})
2444
2445 @skipUnlessDBFeature(
2446 "supports_column_check_constraints", "can_introspect_check_constraints"
2447 )
2448 @isolate_apps("schema")
2449 def test_rename_field_with_check_to_truncated_name(self):
2450 class AuthorWithLongColumn(Model):
2451 field_with_very_looooooong_name = PositiveIntegerField(null=True)
2452
2453 class Meta:
2454 app_label = "schema"
2455
2456 self.isolated_local_models = [AuthorWithLongColumn]
2457 with connection.schema_editor() as editor:
2458 editor.create_model(AuthorWithLongColumn)
2459 old_field = AuthorWithLongColumn._meta.get_field(
2460 "field_with_very_looooooong_name"
2461 )
2462 new_field = PositiveIntegerField(null=True)
2463 new_field.set_attributes_from_name("renamed_field_with_very_long_name")
2464 with connection.schema_editor() as editor:
2465 editor.alter_field(AuthorWithLongColumn, old_field, new_field, strict=True)
2466
2467 new_field_name = truncate_name(
2468 new_field.column, connection.ops.max_name_length()
2469 )
2470 constraints = self.get_constraints(AuthorWithLongColumn._meta.db_table)
2471 check_constraints = [
2472 name
2473 for name, details in constraints.items()
2474 if details["columns"] == [new_field_name] and details["check"]
2475 ]
2476 self.assertEqual(len(check_constraints), 1)
2477
2478 def _test_m2m_create(self, M2MFieldClass):
2479 """
2480 Tests M2M fields on models during creation
2481 """
2482
2483 class LocalBookWithM2M(Model):
2484 author = ForeignKey(Author, CASCADE)
2485 title = CharField(max_length=100, db_index=True)
2486 pub_date = DateTimeField()
2487 tags = M2MFieldClass("TagM2MTest", related_name="books")
2488
2489 class Meta:
2490 app_label = "schema"
2491 apps = new_apps
2492
2493 self.local_models = [LocalBookWithM2M]
2494 # Create the tables
2495 with connection.schema_editor() as editor:
2496 editor.create_model(Author)
2497 editor.create_model(TagM2MTest)
2498 editor.create_model(LocalBookWithM2M)
2499 # Ensure there is now an m2m table there
2500 columns = self.column_classes(
2501 LocalBookWithM2M._meta.get_field("tags").remote_field.through
2502 )
2503 self.assertEqual(
2504 columns["tagm2mtest_id"][0],
2505 connection.features.introspected_field_types["IntegerField"],
2506 )
2507
2508 def test_m2m_create(self):
2509 self._test_m2m_create(ManyToManyField)
2510
2511 def test_m2m_create_custom(self):
2512 self._test_m2m_create(CustomManyToManyField)
2513
2514 def test_m2m_create_inherited(self):
2515 self._test_m2m_create(InheritedManyToManyField)
2516
2517 def _test_m2m_create_through(self, M2MFieldClass):
2518 """
2519 Tests M2M fields on models during creation with through models
2520 """
2521
2522 class LocalTagThrough(Model):
2523 book = ForeignKey("schema.LocalBookWithM2MThrough", CASCADE)
2524 tag = ForeignKey("schema.TagM2MTest", CASCADE)
2525
2526 class Meta:
2527 app_label = "schema"
2528 apps = new_apps
2529
2530 class LocalBookWithM2MThrough(Model):
2531 tags = M2MFieldClass(
2532 "TagM2MTest", related_name="books", through=LocalTagThrough
2533 )
2534
2535 class Meta:
2536 app_label = "schema"
2537 apps = new_apps
2538
2539 self.local_models = [LocalTagThrough, LocalBookWithM2MThrough]
2540
2541 # Create the tables
2542 with connection.schema_editor() as editor:
2543 editor.create_model(LocalTagThrough)
2544 editor.create_model(TagM2MTest)
2545 editor.create_model(LocalBookWithM2MThrough)
2546 # Ensure there is now an m2m table there
2547 columns = self.column_classes(LocalTagThrough)
2548 self.assertEqual(
2549 columns["book_id"][0],
2550 connection.features.introspected_field_types["IntegerField"],
2551 )
2552 self.assertEqual(
2553 columns["tag_id"][0],
2554 connection.features.introspected_field_types["IntegerField"],
2555 )
2556
2557 def test_m2m_create_through(self):
2558 self._test_m2m_create_through(ManyToManyField)
2559
2560 def test_m2m_create_through_custom(self):
2561 self._test_m2m_create_through(CustomManyToManyField)
2562
2563 def test_m2m_create_through_inherited(self):
2564 self._test_m2m_create_through(InheritedManyToManyField)
2565
2566 def test_m2m_through_remove(self):
2567 class LocalAuthorNoteThrough(Model):
2568 book = ForeignKey("schema.Author", CASCADE)
2569 tag = ForeignKey("self", CASCADE)
2570
2571 class Meta:
2572 app_label = "schema"
2573 apps = new_apps
2574
2575 class LocalNoteWithM2MThrough(Model):
2576 authors = ManyToManyField("schema.Author", through=LocalAuthorNoteThrough)
2577
2578 class Meta:
2579 app_label = "schema"
2580 apps = new_apps
2581
2582 self.local_models = [LocalAuthorNoteThrough, LocalNoteWithM2MThrough]
2583 # Create the tables.
2584 with connection.schema_editor() as editor:
2585 editor.create_model(Author)
2586 editor.create_model(LocalAuthorNoteThrough)
2587 editor.create_model(LocalNoteWithM2MThrough)
2588 # Remove the through parameter.
2589 old_field = LocalNoteWithM2MThrough._meta.get_field("authors")
2590 new_field = ManyToManyField("Author")
2591 new_field.set_attributes_from_name("authors")
2592 msg = (
2593 f"Cannot alter field {old_field} into {new_field} - they are not "
2594 f"compatible types (you cannot alter to or from M2M fields, or add or "
2595 f"remove through= on M2M fields)"
2596 )
2597 with connection.schema_editor() as editor:
2598 with self.assertRaisesMessage(ValueError, msg):
2599 editor.alter_field(LocalNoteWithM2MThrough, old_field, new_field)
2600
2601 def _test_m2m(self, M2MFieldClass):
2602 """
2603 Tests adding/removing M2M fields on models
2604 """
2605
2606 class LocalAuthorWithM2M(Model):
2607 name = CharField(max_length=255)
2608
2609 class Meta:
2610 app_label = "schema"
2611 apps = new_apps
2612
2613 self.local_models = [LocalAuthorWithM2M]
2614
2615 # Create the tables
2616 with connection.schema_editor() as editor:
2617 editor.create_model(LocalAuthorWithM2M)
2618 editor.create_model(TagM2MTest)
2619 # Create an M2M field
2620 new_field = M2MFieldClass("schema.TagM2MTest", related_name="authors")
2621 new_field.contribute_to_class(LocalAuthorWithM2M, "tags")
2622 # Ensure there's no m2m table there
2623 with self.assertRaises(DatabaseError):
2624 self.column_classes(new_field.remote_field.through)
2625 # Add the field
2626 with (
2627 CaptureQueriesContext(connection) as ctx,
2628 connection.schema_editor() as editor,
2629 ):
2630 editor.add_field(LocalAuthorWithM2M, new_field)
2631 # Table is not rebuilt.
2632 self.assertEqual(
2633 len(
2634 [
2635 query["sql"]
2636 for query in ctx.captured_queries
2637 if "CREATE TABLE" in query["sql"]
2638 ]
2639 ),
2640 1,
2641 )
2642 self.assertIs(
2643 any("DROP TABLE" in query["sql"] for query in ctx.captured_queries),
2644 False,
2645 )
2646 # Ensure there is now an m2m table there
2647 columns = self.column_classes(new_field.remote_field.through)
2648 self.assertEqual(
2649 columns["tagm2mtest_id"][0],
2650 connection.features.introspected_field_types["IntegerField"],
2651 )
2652
2653 # "Alter" the field. This should not rename the DB table to itself.
2654 with connection.schema_editor() as editor:
2655 editor.alter_field(LocalAuthorWithM2M, new_field, new_field, strict=True)
2656
2657 # Remove the M2M table again
2658 with connection.schema_editor() as editor:
2659 editor.remove_field(LocalAuthorWithM2M, new_field)
2660 # Ensure there's no m2m table there
2661 with self.assertRaises(DatabaseError):
2662 self.column_classes(new_field.remote_field.through)
2663
2664 # Make sure the model state is coherent with the table one now that
2665 # we've removed the tags field.
2666 opts = LocalAuthorWithM2M._meta
2667 opts.local_many_to_many.remove(new_field)
2668 del new_apps.all_models["schema"][
2669 new_field.remote_field.through._meta.model_name
2670 ]
2671 opts._expire_cache()
2672
2673 def test_m2m(self):
2674 self._test_m2m(ManyToManyField)
2675
2676 def test_m2m_custom(self):
2677 self._test_m2m(CustomManyToManyField)
2678
2679 def test_m2m_inherited(self):
2680 self._test_m2m(InheritedManyToManyField)
2681
2682 def _test_m2m_through_alter(self, M2MFieldClass):
2683 """
2684 Tests altering M2Ms with explicit through models (should no-op)
2685 """
2686
2687 class LocalAuthorTag(Model):
2688 author = ForeignKey("schema.LocalAuthorWithM2MThrough", CASCADE)
2689 tag = ForeignKey("schema.TagM2MTest", CASCADE)
2690
2691 class Meta:
2692 app_label = "schema"
2693 apps = new_apps
2694
2695 class LocalAuthorWithM2MThrough(Model):
2696 name = CharField(max_length=255)
2697 tags = M2MFieldClass(
2698 "schema.TagM2MTest", related_name="authors", through=LocalAuthorTag
2699 )
2700
2701 class Meta:
2702 app_label = "schema"
2703 apps = new_apps
2704
2705 self.local_models = [LocalAuthorTag, LocalAuthorWithM2MThrough]
2706
2707 # Create the tables
2708 with connection.schema_editor() as editor:
2709 editor.create_model(LocalAuthorTag)
2710 editor.create_model(LocalAuthorWithM2MThrough)
2711 editor.create_model(TagM2MTest)
2712 # Ensure the m2m table is there
2713 self.assertEqual(len(self.column_classes(LocalAuthorTag)), 3)
2714 # "Alter" the field's blankness. This should not actually do anything.
2715 old_field = LocalAuthorWithM2MThrough._meta.get_field("tags")
2716 new_field = M2MFieldClass(
2717 "schema.TagM2MTest", related_name="authors", through=LocalAuthorTag
2718 )
2719 new_field.contribute_to_class(LocalAuthorWithM2MThrough, "tags")
2720 with connection.schema_editor() as editor:
2721 editor.alter_field(
2722 LocalAuthorWithM2MThrough, old_field, new_field, strict=True
2723 )
2724 # Ensure the m2m table is still there
2725 self.assertEqual(len(self.column_classes(LocalAuthorTag)), 3)
2726
2727 def test_m2m_through_alter(self):
2728 self._test_m2m_through_alter(ManyToManyField)
2729
2730 def test_m2m_through_alter_custom(self):
2731 self._test_m2m_through_alter(CustomManyToManyField)
2732
2733 def test_m2m_through_alter_inherited(self):
2734 self._test_m2m_through_alter(InheritedManyToManyField)
2735
2736 def _test_m2m_repoint(self, M2MFieldClass):
2737 """
2738 Tests repointing M2M fields
2739 """
2740
2741 class LocalBookWithM2M(Model):
2742 author = ForeignKey(Author, CASCADE)
2743 title = CharField(max_length=100, db_index=True)
2744 pub_date = DateTimeField()
2745 tags = M2MFieldClass("TagM2MTest", related_name="books")
2746
2747 class Meta:
2748 app_label = "schema"
2749 apps = new_apps
2750
2751 self.local_models = [LocalBookWithM2M]
2752 # Create the tables
2753 with connection.schema_editor() as editor:
2754 editor.create_model(Author)
2755 editor.create_model(LocalBookWithM2M)
2756 editor.create_model(TagM2MTest)
2757 editor.create_model(UniqueTest)
2758 # Ensure the M2M exists and points to TagM2MTest
2759 if connection.features.supports_foreign_keys:
2760 self.assertForeignKeyExists(
2761 LocalBookWithM2M._meta.get_field("tags").remote_field.through,
2762 "tagm2mtest_id",
2763 "schema_tagm2mtest",
2764 )
2765 # Repoint the M2M
2766 old_field = LocalBookWithM2M._meta.get_field("tags")
2767 new_field = M2MFieldClass(UniqueTest)
2768 new_field.contribute_to_class(LocalBookWithM2M, "uniques")
2769 with connection.schema_editor() as editor:
2770 editor.alter_field(LocalBookWithM2M, old_field, new_field, strict=True)
2771 # Ensure old M2M is gone
2772 with self.assertRaises(DatabaseError):
2773 self.column_classes(
2774 LocalBookWithM2M._meta.get_field("tags").remote_field.through
2775 )
2776
2777 # This model looks like the new model and is used for teardown.
2778 opts = LocalBookWithM2M._meta
2779 opts.local_many_to_many.remove(old_field)
2780 # Ensure the new M2M exists and points to UniqueTest
2781 if connection.features.supports_foreign_keys:
2782 self.assertForeignKeyExists(
2783 new_field.remote_field.through, "uniquetest_id", "schema_uniquetest"
2784 )
2785
2786 def test_m2m_repoint(self):
2787 self._test_m2m_repoint(ManyToManyField)
2788
2789 def test_m2m_repoint_custom(self):
2790 self._test_m2m_repoint(CustomManyToManyField)
2791
2792 def test_m2m_repoint_inherited(self):
2793 self._test_m2m_repoint(InheritedManyToManyField)
2794
2795 @isolate_apps("schema")
2796 def test_m2m_rename_field_in_target_model(self):
2797 class LocalTagM2MTest(Model):
2798 title = CharField(max_length=255)
2799
2800 class Meta:
2801 app_label = "schema"
2802
2803 class LocalM2M(Model):
2804 tags = ManyToManyField(LocalTagM2MTest)
2805
2806 class Meta:
2807 app_label = "schema"
2808
2809 # Create the tables.
2810 with connection.schema_editor() as editor:
2811 editor.create_model(LocalM2M)
2812 editor.create_model(LocalTagM2MTest)
2813 self.isolated_local_models = [LocalM2M, LocalTagM2MTest]
2814 # Ensure the m2m table is there.
2815 self.assertEqual(len(self.column_classes(LocalM2M)), 1)
2816 # Alter a field in LocalTagM2MTest.
2817 old_field = LocalTagM2MTest._meta.get_field("title")
2818 new_field = CharField(max_length=254)
2819 new_field.contribute_to_class(LocalTagM2MTest, "title1")
2820 # @isolate_apps() and inner models are needed to have the model
2821 # relations populated, otherwise this doesn't act as a regression test.
2822 self.assertEqual(len(new_field.model._meta.related_objects), 1)
2823 with connection.schema_editor() as editor:
2824 editor.alter_field(LocalTagM2MTest, old_field, new_field, strict=True)
2825 # Ensure the m2m table is still there.
2826 self.assertEqual(len(self.column_classes(LocalM2M)), 1)
2827
2828 @skipUnlessDBFeature(
2829 "supports_column_check_constraints", "can_introspect_check_constraints"
2830 )
2831 def test_check_constraints(self):
2832 """
2833 Tests creating/deleting CHECK constraints
2834 """
2835 # Create the tables
2836 with connection.schema_editor() as editor:
2837 editor.create_model(Author)
2838 # Ensure the constraint exists
2839 constraints = self.get_constraints(Author._meta.db_table)
2840 if not any(
2841 details["columns"] == ["height"] and details["check"]
2842 for details in constraints.values()
2843 ):
2844 self.fail("No check constraint for height found")
2845 # Alter the column to remove it
2846 old_field = Author._meta.get_field("height")
2847 new_field = IntegerField(null=True, blank=True)
2848 new_field.set_attributes_from_name("height")
2849 with connection.schema_editor() as editor:
2850 editor.alter_field(Author, old_field, new_field, strict=True)
2851 constraints = self.get_constraints(Author._meta.db_table)
2852 for details in constraints.values():
2853 if details["columns"] == ["height"] and details["check"]:
2854 self.fail("Check constraint for height found")
2855 # Alter the column to re-add it
2856 new_field2 = Author._meta.get_field("height")
2857 with connection.schema_editor() as editor:
2858 editor.alter_field(Author, new_field, new_field2, strict=True)
2859 constraints = self.get_constraints(Author._meta.db_table)
2860 if not any(
2861 details["columns"] == ["height"] and details["check"]
2862 for details in constraints.values()
2863 ):
2864 self.fail("No check constraint for height found")
2865
2866 @skipUnlessDBFeature(
2867 "supports_column_check_constraints", "can_introspect_check_constraints"
2868 )
2869 @isolate_apps("schema")
2870 def test_check_constraint_timedelta_param(self):
2871 class DurationModel(Model):
2872 duration = DurationField()
2873
2874 class Meta:
2875 app_label = "schema"
2876
2877 with connection.schema_editor() as editor:
2878 editor.create_model(DurationModel)
2879 self.isolated_local_models = [DurationModel]
2880 constraint_name = "duration_gte_5_minutes"
2881 constraint = CheckConstraint(
2882 condition=Q(duration__gt=datetime.timedelta(minutes=5)),
2883 name=constraint_name,
2884 )
2885 DurationModel._meta.constraints = [constraint]
2886 with connection.schema_editor() as editor:
2887 editor.add_constraint(DurationModel, constraint)
2888 constraints = self.get_constraints(DurationModel._meta.db_table)
2889 self.assertIn(constraint_name, constraints)
2890 with self.assertRaises(IntegrityError), atomic():
2891 DurationModel.objects.create(duration=datetime.timedelta(minutes=4))
2892 DurationModel.objects.create(duration=datetime.timedelta(minutes=10))
2893
2894 @skipUnlessDBFeature(
2895 "supports_column_check_constraints",
2896 "can_introspect_check_constraints",
2897 "supports_json_field",
2898 )
2899 @isolate_apps("schema")
2900 def test_check_constraint_exact_jsonfield(self):
2901 class JSONConstraintModel(Model):
2902 data = JSONField()
2903
2904 class Meta:
2905 app_label = "schema"
2906
2907 with connection.schema_editor() as editor:
2908 editor.create_model(JSONConstraintModel)
2909 self.isolated_local_models = [JSONConstraintModel]
2910 constraint_name = "check_only_stable_version"
2911 constraint = CheckConstraint(
2912 condition=Q(data__version="stable"),
2913 name=constraint_name,
2914 )
2915 JSONConstraintModel._meta.constraints = [constraint]
2916 with connection.schema_editor() as editor:
2917 editor.add_constraint(JSONConstraintModel, constraint)
2918 constraints = self.get_constraints(JSONConstraintModel._meta.db_table)
2919 self.assertIn(constraint_name, constraints)
2920 with self.assertRaises(IntegrityError), atomic():
2921 JSONConstraintModel.objects.create(
2922 data={"release": "5.0.2dev", "version": "dev"}
2923 )
2924 JSONConstraintModel.objects.create(
2925 data={"release": "5.0.3", "version": "stable"}
2926 )
2927
2928 @skipUnlessDBFeature(
2929 "supports_column_check_constraints", "can_introspect_check_constraints"
2930 )
2931 def test_remove_field_check_does_not_remove_meta_constraints(self):
2932 with connection.schema_editor() as editor:
2933 editor.create_model(Author)
2934 # Add the custom check constraint
2935 constraint = CheckConstraint(
2936 condition=Q(height__gte=0), name="author_height_gte_0_check"
2937 )
2938 custom_constraint_name = constraint.name
2939 Author._meta.constraints = [constraint]
2940 with connection.schema_editor() as editor:
2941 editor.add_constraint(Author, constraint)
2942 # Ensure the constraints exist
2943 constraints = self.get_constraints(Author._meta.db_table)
2944 self.assertIn(custom_constraint_name, constraints)
2945 other_constraints = [
2946 name
2947 for name, details in constraints.items()
2948 if details["columns"] == ["height"]
2949 and details["check"]
2950 and name != custom_constraint_name
2951 ]
2952 self.assertEqual(len(other_constraints), 1)
2953 # Alter the column to remove field check
2954 old_field = Author._meta.get_field("height")
2955 new_field = IntegerField(null=True, blank=True)
2956 new_field.set_attributes_from_name("height")
2957 with connection.schema_editor() as editor:
2958 editor.alter_field(Author, old_field, new_field, strict=True)
2959 constraints = self.get_constraints(Author._meta.db_table)
2960 self.assertIn(custom_constraint_name, constraints)
2961 other_constraints = [
2962 name
2963 for name, details in constraints.items()
2964 if details["columns"] == ["height"]
2965 and details["check"]
2966 and name != custom_constraint_name
2967 ]
2968 self.assertEqual(len(other_constraints), 0)
2969 # Alter the column to re-add field check
2970 new_field2 = Author._meta.get_field("height")
2971 with connection.schema_editor() as editor:
2972 editor.alter_field(Author, new_field, new_field2, strict=True)
2973 constraints = self.get_constraints(Author._meta.db_table)
2974 self.assertIn(custom_constraint_name, constraints)
2975 other_constraints = [
2976 name
2977 for name, details in constraints.items()
2978 if details["columns"] == ["height"]
2979 and details["check"]
2980 and name != custom_constraint_name
2981 ]
2982 self.assertEqual(len(other_constraints), 1)
2983 # Drop the check constraint
2984 with connection.schema_editor() as editor:
2985 Author._meta.constraints = []
2986 editor.remove_constraint(Author, constraint)
2987
2988 def test_unique(self):
2989 """
2990 Tests removing and adding unique constraints to a single column.
2991 """
2992 # Create the table
2993 with connection.schema_editor() as editor:
2994 editor.create_model(Tag)
2995 # Ensure the field is unique to begin with
2996 Tag.objects.create(title="foo", slug="foo")
2997 with self.assertRaises(IntegrityError):
2998 Tag.objects.create(title="bar", slug="foo")
2999 Tag.objects.all().delete()
3000 # Alter the slug field to be non-unique
3001 old_field = Tag._meta.get_field("slug")
3002 new_field = SlugField(unique=False)
3003 new_field.set_attributes_from_name("slug")
3004 with connection.schema_editor() as editor:
3005 editor.alter_field(Tag, old_field, new_field, strict=True)
3006 # Ensure the field is no longer unique
3007 Tag.objects.create(title="foo", slug="foo")
3008 Tag.objects.create(title="bar", slug="foo")
3009 Tag.objects.all().delete()
3010 # Alter the slug field to be unique
3011 new_field2 = SlugField(unique=True)
3012 new_field2.set_attributes_from_name("slug")
3013 with connection.schema_editor() as editor:
3014 editor.alter_field(Tag, new_field, new_field2, strict=True)
3015 # Ensure the field is unique again
3016 Tag.objects.create(title="foo", slug="foo")
3017 with self.assertRaises(IntegrityError):
3018 Tag.objects.create(title="bar", slug="foo")
3019 Tag.objects.all().delete()
3020 # Rename the field
3021 new_field3 = SlugField(unique=True)
3022 new_field3.set_attributes_from_name("slug2")
3023 with connection.schema_editor() as editor:
3024 editor.alter_field(Tag, new_field2, new_field3, strict=True)
3025 # Ensure the field is still unique
3026 TagUniqueRename.objects.create(title="foo", slug2="foo")
3027 with self.assertRaises(IntegrityError):
3028 TagUniqueRename.objects.create(title="bar", slug2="foo")
3029 Tag.objects.all().delete()
3030
3031 def test_unique_name_quoting(self):
3032 old_table_name = TagUniqueRename._meta.db_table
3033 try:
3034 with connection.schema_editor() as editor:
3035 editor.create_model(TagUniqueRename)
3036 editor.alter_db_table(TagUniqueRename, old_table_name, "unique-table")
3037 TagUniqueRename._meta.db_table = "unique-table"
3038 # This fails if the unique index name isn't quoted.
3039 editor.alter_unique_together(TagUniqueRename, [], (("title", "slug2"),))
3040 finally:
3041 with connection.schema_editor() as editor:
3042 editor.delete_model(TagUniqueRename)
3043 TagUniqueRename._meta.db_table = old_table_name
3044
3045 @isolate_apps("schema")
3046 @skipUnlessDBFeature("supports_foreign_keys")
3047 def test_unique_no_unnecessary_fk_drops(self):
3048 """
3049 If AlterField isn't selective about dropping foreign key constraints
3050 when modifying a field with a unique constraint, the AlterField
3051 incorrectly drops and recreates the Book.author foreign key even though
3052 it doesn't restrict the field being changed (#29193).
3053 """
3054
3055 class Author(Model):
3056 name = CharField(max_length=254, unique=True)
3057
3058 class Meta:
3059 app_label = "schema"
3060
3061 class Book(Model):
3062 author = ForeignKey(Author, CASCADE)
3063
3064 class Meta:
3065 app_label = "schema"
3066
3067 with connection.schema_editor() as editor:
3068 editor.create_model(Author)
3069 editor.create_model(Book)
3070 new_field = CharField(max_length=255, unique=True)
3071 new_field.model = Author
3072 new_field.set_attributes_from_name("name")
3073 with self.assertLogs("django.db.backends.schema", "DEBUG") as cm:
3074 with connection.schema_editor() as editor:
3075 editor.alter_field(Author, Author._meta.get_field("name"), new_field)
3076 # One SQL statement is executed to alter the field.
3077 self.assertEqual(len(cm.records), 1)
3078
3079 @isolate_apps("schema")
3080 def test_unique_and_reverse_m2m(self):
3081 """
3082 AlterField can modify a unique field when there's a reverse M2M
3083 relation on the model.
3084 """
3085
3086 class Tag(Model):
3087 title = CharField(max_length=255)
3088 slug = SlugField(unique=True)
3089
3090 class Meta:
3091 app_label = "schema"
3092
3093 class Book(Model):
3094 tags = ManyToManyField(Tag, related_name="books")
3095
3096 class Meta:
3097 app_label = "schema"
3098
3099 self.isolated_local_models = [Book._meta.get_field("tags").remote_field.through]
3100 with connection.schema_editor() as editor:
3101 editor.create_model(Tag)
3102 editor.create_model(Book)
3103 new_field = SlugField(max_length=75, unique=True)
3104 new_field.model = Tag
3105 new_field.set_attributes_from_name("slug")
3106 with self.assertLogs("django.db.backends.schema", "DEBUG") as cm:
3107 with connection.schema_editor() as editor:
3108 editor.alter_field(Tag, Tag._meta.get_field("slug"), new_field)
3109 # One SQL statement is executed to alter the field.
3110 self.assertEqual(len(cm.records), 1)
3111 # Ensure that the field is still unique.
3112 Tag.objects.create(title="foo", slug="foo")
3113 with self.assertRaises(IntegrityError):
3114 Tag.objects.create(title="bar", slug="foo")
3115
3116 def test_remove_ignored_unique_constraint_not_create_fk_index(self):
3117 with connection.schema_editor() as editor:
3118 editor.create_model(Author)
3119 editor.create_model(Book)
3120 constraint = UniqueConstraint(
3121 "author",
3122 condition=Q(title__in=["tHGttG", "tRatEotU"]),
3123 name="book_author_condition_uniq",
3124 )
3125 # Add unique constraint.
3126 with connection.schema_editor() as editor:
3127 editor.add_constraint(Book, constraint)
3128 old_constraints = self.get_constraints_for_column(
3129 Book,
3130 Book._meta.get_field("author").column,
3131 )
3132 # Remove unique constraint.
3133 with connection.schema_editor() as editor:
3134 editor.remove_constraint(Book, constraint)
3135 new_constraints = self.get_constraints_for_column(
3136 Book,
3137 Book._meta.get_field("author").column,
3138 )
3139 # Redundant foreign key index is not added.
3140 self.assertEqual(
3141 (
3142 len(old_constraints) - 1
3143 if connection.features.supports_partial_indexes
3144 else len(old_constraints)
3145 ),
3146 len(new_constraints),
3147 )
3148
3149 @skipUnlessDBFeature("allows_multiple_constraints_on_same_fields")
3150 def test_remove_field_unique_does_not_remove_meta_constraints(self):
3151 with connection.schema_editor() as editor:
3152 editor.create_model(AuthorWithUniqueName)
3153 self.local_models = [AuthorWithUniqueName]
3154 # Add the custom unique constraint
3155 constraint = UniqueConstraint(fields=["name"], name="author_name_uniq")
3156 custom_constraint_name = constraint.name
3157 AuthorWithUniqueName._meta.constraints = [constraint]
3158 with connection.schema_editor() as editor:
3159 editor.add_constraint(AuthorWithUniqueName, constraint)
3160 # Ensure the constraints exist
3161 constraints = self.get_constraints(AuthorWithUniqueName._meta.db_table)
3162 self.assertIn(custom_constraint_name, constraints)
3163 other_constraints = [
3164 name
3165 for name, details in constraints.items()
3166 if details["columns"] == ["name"]
3167 and details["unique"]
3168 and name != custom_constraint_name
3169 ]
3170 self.assertEqual(len(other_constraints), 1)
3171 # Alter the column to remove field uniqueness
3172 old_field = AuthorWithUniqueName._meta.get_field("name")
3173 new_field = CharField(max_length=255)
3174 new_field.set_attributes_from_name("name")
3175 with connection.schema_editor() as editor:
3176 editor.alter_field(AuthorWithUniqueName, old_field, new_field, strict=True)
3177 constraints = self.get_constraints(AuthorWithUniqueName._meta.db_table)
3178 self.assertIn(custom_constraint_name, constraints)
3179 other_constraints = [
3180 name
3181 for name, details in constraints.items()
3182 if details["columns"] == ["name"]
3183 and details["unique"]
3184 and name != custom_constraint_name
3185 ]
3186 self.assertEqual(len(other_constraints), 0)
3187 # Alter the column to re-add field uniqueness
3188 new_field2 = AuthorWithUniqueName._meta.get_field("name")
3189 with connection.schema_editor() as editor:
3190 editor.alter_field(AuthorWithUniqueName, new_field, new_field2, strict=True)
3191 constraints = self.get_constraints(AuthorWithUniqueName._meta.db_table)
3192 self.assertIn(custom_constraint_name, constraints)
3193 other_constraints = [
3194 name
3195 for name, details in constraints.items()
3196 if details["columns"] == ["name"]
3197 and details["unique"]
3198 and name != custom_constraint_name
3199 ]
3200 self.assertEqual(len(other_constraints), 1)
3201 # Drop the unique constraint
3202 with connection.schema_editor() as editor:
3203 AuthorWithUniqueName._meta.constraints = []
3204 editor.remove_constraint(AuthorWithUniqueName, constraint)
3205
3206 def test_unique_together(self):
3207 """
3208 Tests removing and adding unique_together constraints on a model.
3209 """
3210 # Create the table
3211 with connection.schema_editor() as editor:
3212 editor.create_model(UniqueTest)
3213 # Ensure the fields are unique to begin with
3214 UniqueTest.objects.create(year=2012, slug="foo")
3215 UniqueTest.objects.create(year=2011, slug="foo")
3216 UniqueTest.objects.create(year=2011, slug="bar")
3217 with self.assertRaises(IntegrityError):
3218 UniqueTest.objects.create(year=2012, slug="foo")
3219 UniqueTest.objects.all().delete()
3220 # Alter the model to its non-unique-together companion
3221 with connection.schema_editor() as editor:
3222 editor.alter_unique_together(
3223 UniqueTest, UniqueTest._meta.unique_together, []
3224 )
3225 # Ensure the fields are no longer unique
3226 UniqueTest.objects.create(year=2012, slug="foo")
3227 UniqueTest.objects.create(year=2012, slug="foo")
3228 UniqueTest.objects.all().delete()
3229 # Alter it back
3230 new_field2 = SlugField(unique=True)
3231 new_field2.set_attributes_from_name("slug")
3232 with connection.schema_editor() as editor:
3233 editor.alter_unique_together(
3234 UniqueTest, [], UniqueTest._meta.unique_together
3235 )
3236 # Ensure the fields are unique again
3237 UniqueTest.objects.create(year=2012, slug="foo")
3238 with self.assertRaises(IntegrityError):
3239 UniqueTest.objects.create(year=2012, slug="foo")
3240 UniqueTest.objects.all().delete()
3241
3242 def test_unique_together_with_fk(self):
3243 """
3244 Tests removing and adding unique_together constraints that include
3245 a foreign key.
3246 """
3247 # Create the table
3248 with connection.schema_editor() as editor:
3249 editor.create_model(Author)
3250 editor.create_model(Book)
3251 # Ensure the fields are unique to begin with
3252 self.assertEqual(Book._meta.unique_together, ())
3253 # Add the unique_together constraint
3254 with connection.schema_editor() as editor:
3255 editor.alter_unique_together(Book, [], [["author", "title"]])
3256 # Alter it back
3257 with connection.schema_editor() as editor:
3258 editor.alter_unique_together(Book, [["author", "title"]], [])
3259
3260 def test_unique_together_with_fk_with_existing_index(self):
3261 """
3262 Tests removing and adding unique_together constraints that include
3263 a foreign key, where the foreign key is added after the model is
3264 created.
3265 """
3266 # Create the tables
3267 with connection.schema_editor() as editor:
3268 editor.create_model(Author)
3269 editor.create_model(BookWithoutAuthor)
3270 new_field = ForeignKey(Author, CASCADE)
3271 new_field.set_attributes_from_name("author")
3272 editor.add_field(BookWithoutAuthor, new_field)
3273 # Ensure the fields aren't unique to begin with
3274 self.assertEqual(Book._meta.unique_together, ())
3275 # Add the unique_together constraint
3276 with connection.schema_editor() as editor:
3277 editor.alter_unique_together(Book, [], [["author", "title"]])
3278 # Alter it back
3279 with connection.schema_editor() as editor:
3280 editor.alter_unique_together(Book, [["author", "title"]], [])
3281
3282 def _test_composed_index_with_fk(self, index):
3283 with connection.schema_editor() as editor:
3284 editor.create_model(Author)
3285 editor.create_model(Book)
3286 table = Book._meta.db_table
3287 self.assertEqual(Book._meta.indexes, [])
3288 Book._meta.indexes = [index]
3289 with connection.schema_editor() as editor:
3290 editor.add_index(Book, index)
3291 self.assertIn(index.name, self.get_constraints(table))
3292 Book._meta.indexes = []
3293 with connection.schema_editor() as editor:
3294 editor.remove_index(Book, index)
3295 self.assertNotIn(index.name, self.get_constraints(table))
3296
3297 def test_composed_index_with_fk(self):
3298 index = Index(fields=["author", "title"], name="book_author_title_idx")
3299 self._test_composed_index_with_fk(index)
3300
3301 def test_composed_desc_index_with_fk(self):
3302 index = Index(fields=["-author", "title"], name="book_author_title_idx")
3303 self._test_composed_index_with_fk(index)
3304
3305 @skipUnlessDBFeature("supports_expression_indexes")
3306 def test_composed_func_index_with_fk(self):
3307 index = Index(F("author"), F("title"), name="book_author_title_idx")
3308 self._test_composed_index_with_fk(index)
3309
3310 @skipUnlessDBFeature("supports_expression_indexes")
3311 def test_composed_desc_func_index_with_fk(self):
3312 index = Index(F("author").desc(), F("title"), name="book_author_title_idx")
3313 self._test_composed_index_with_fk(index)
3314
3315 @skipUnlessDBFeature("supports_expression_indexes")
3316 def test_composed_func_transform_index_with_fk(self):
3317 index = Index(F("title__lower"), name="book_title_lower_idx")
3318 with register_lookup(CharField, Lower):
3319 self._test_composed_index_with_fk(index)
3320
3321 def _test_composed_constraint_with_fk(self, constraint):
3322 with connection.schema_editor() as editor:
3323 editor.create_model(Author)
3324 editor.create_model(Book)
3325 table = Book._meta.db_table
3326 self.assertEqual(Book._meta.constraints, [])
3327 Book._meta.constraints = [constraint]
3328 with connection.schema_editor() as editor:
3329 editor.add_constraint(Book, constraint)
3330 self.assertIn(constraint.name, self.get_constraints(table))
3331 Book._meta.constraints = []
3332 with connection.schema_editor() as editor:
3333 editor.remove_constraint(Book, constraint)
3334 self.assertNotIn(constraint.name, self.get_constraints(table))
3335
3336 def test_composed_constraint_with_fk(self):
3337 constraint = UniqueConstraint(
3338 fields=["author", "title"],
3339 name="book_author_title_uniq",
3340 )
3341 self._test_composed_constraint_with_fk(constraint)
3342
3343 @skipUnlessDBFeature(
3344 "supports_column_check_constraints", "can_introspect_check_constraints"
3345 )
3346 def test_composed_check_constraint_with_fk(self):
3347 constraint = CheckConstraint(
3348 condition=Q(author__gt=0), name="book_author_check"
3349 )
3350 self._test_composed_constraint_with_fk(constraint)
3351
3352 @skipUnlessDBFeature("allows_multiple_constraints_on_same_fields")
3353 def test_remove_unique_together_does_not_remove_meta_constraints(self):
3354 with connection.schema_editor() as editor:
3355 editor.create_model(AuthorWithUniqueNameAndBirthday)
3356 self.local_models = [AuthorWithUniqueNameAndBirthday]
3357 # Add the custom unique constraint
3358 constraint = UniqueConstraint(
3359 fields=["name", "birthday"], name="author_name_birthday_uniq"
3360 )
3361 custom_constraint_name = constraint.name
3362 AuthorWithUniqueNameAndBirthday._meta.constraints = [constraint]
3363 with connection.schema_editor() as editor:
3364 editor.add_constraint(AuthorWithUniqueNameAndBirthday, constraint)
3365 # Ensure the constraints exist
3366 constraints = self.get_constraints(
3367 AuthorWithUniqueNameAndBirthday._meta.db_table
3368 )
3369 self.assertIn(custom_constraint_name, constraints)
3370 other_constraints = [
3371 name
3372 for name, details in constraints.items()
3373 if details["columns"] == ["name", "birthday"]
3374 and details["unique"]
3375 and name != custom_constraint_name
3376 ]
3377 self.assertEqual(len(other_constraints), 1)
3378 # Remove unique together
3379 unique_together = AuthorWithUniqueNameAndBirthday._meta.unique_together
3380 with connection.schema_editor() as editor:
3381 editor.alter_unique_together(
3382 AuthorWithUniqueNameAndBirthday, unique_together, []
3383 )
3384 constraints = self.get_constraints(
3385 AuthorWithUniqueNameAndBirthday._meta.db_table
3386 )
3387 self.assertIn(custom_constraint_name, constraints)
3388 other_constraints = [
3389 name
3390 for name, details in constraints.items()
3391 if details["columns"] == ["name", "birthday"]
3392 and details["unique"]
3393 and name != custom_constraint_name
3394 ]
3395 self.assertEqual(len(other_constraints), 0)
3396 # Re-add unique together
3397 with connection.schema_editor() as editor:
3398 editor.alter_unique_together(
3399 AuthorWithUniqueNameAndBirthday, [], unique_together
3400 )
3401 constraints = self.get_constraints(
3402 AuthorWithUniqueNameAndBirthday._meta.db_table
3403 )
3404 self.assertIn(custom_constraint_name, constraints)
3405 other_constraints = [
3406 name
3407 for name, details in constraints.items()
3408 if details["columns"] == ["name", "birthday"]
3409 and details["unique"]
3410 and name != custom_constraint_name
3411 ]
3412 self.assertEqual(len(other_constraints), 1)
3413 # Drop the unique constraint
3414 with connection.schema_editor() as editor:
3415 AuthorWithUniqueNameAndBirthday._meta.constraints = []
3416 editor.remove_constraint(AuthorWithUniqueNameAndBirthday, constraint)
3417
3418 def test_unique_constraint(self):
3419 with connection.schema_editor() as editor:
3420 editor.create_model(Author)
3421 constraint = UniqueConstraint(fields=["name"], name="name_uq")
3422 # Add constraint.
3423 with connection.schema_editor() as editor:
3424 editor.add_constraint(Author, constraint)
3425 sql = constraint.create_sql(Author, editor)
3426 table = Author._meta.db_table
3427 self.assertIs(sql.references_table(table), True)
3428 self.assertIs(sql.references_column(table, "name"), True)
3429 # Remove constraint.
3430 with connection.schema_editor() as editor:
3431 editor.remove_constraint(Author, constraint)
3432 self.assertNotIn(constraint.name, self.get_constraints(table))
3433
3434 @skipUnlessDBFeature("supports_expression_indexes")
3435 def test_func_unique_constraint(self):
3436 with connection.schema_editor() as editor:
3437 editor.create_model(Author)
3438 constraint = UniqueConstraint(Upper("name").desc(), name="func_upper_uq")
3439 # Add constraint.
3440 with connection.schema_editor() as editor:
3441 editor.add_constraint(Author, constraint)
3442 sql = constraint.create_sql(Author, editor)
3443 table = Author._meta.db_table
3444 constraints = self.get_constraints(table)
3445 if connection.features.supports_index_column_ordering:
3446 self.assertIndexOrder(table, constraint.name, ["DESC"])
3447 self.assertIn(constraint.name, constraints)
3448 self.assertIs(constraints[constraint.name]["unique"], True)
3449 # SQL contains a database function.
3450 self.assertIs(sql.references_column(table, "name"), True)
3451 self.assertIn("UPPER(%s)" % editor.quote_name("name"), str(sql))
3452 # Remove constraint.
3453 with connection.schema_editor() as editor:
3454 editor.remove_constraint(Author, constraint)
3455 self.assertNotIn(constraint.name, self.get_constraints(table))
3456
3457 @skipUnlessDBFeature("supports_expression_indexes")
3458 def test_composite_func_unique_constraint(self):
3459 with connection.schema_editor() as editor:
3460 editor.create_model(Author)
3461 editor.create_model(BookWithSlug)
3462 constraint = UniqueConstraint(
3463 Upper("title"),
3464 Lower("slug"),
3465 name="func_upper_lower_unq",
3466 )
3467 # Add constraint.
3468 with connection.schema_editor() as editor:
3469 editor.add_constraint(BookWithSlug, constraint)
3470 sql = constraint.create_sql(BookWithSlug, editor)
3471 table = BookWithSlug._meta.db_table
3472 constraints = self.get_constraints(table)
3473 self.assertIn(constraint.name, constraints)
3474 self.assertIs(constraints[constraint.name]["unique"], True)
3475 # SQL contains database functions.
3476 self.assertIs(sql.references_column(table, "title"), True)
3477 self.assertIs(sql.references_column(table, "slug"), True)
3478 sql = str(sql)
3479 self.assertIn("UPPER(%s)" % editor.quote_name("title"), sql)
3480 self.assertIn("LOWER(%s)" % editor.quote_name("slug"), sql)
3481 self.assertLess(sql.index("UPPER"), sql.index("LOWER"))
3482 # Remove constraint.
3483 with connection.schema_editor() as editor:
3484 editor.remove_constraint(BookWithSlug, constraint)
3485 self.assertNotIn(constraint.name, self.get_constraints(table))
3486
3487 @skipUnlessDBFeature("supports_expression_indexes")
3488 def test_unique_constraint_field_and_expression(self):
3489 with connection.schema_editor() as editor:
3490 editor.create_model(Author)
3491 constraint = UniqueConstraint(
3492 F("height").desc(),
3493 "uuid",
3494 Lower("name").asc(),
3495 name="func_f_lower_field_unq",
3496 )
3497 # Add constraint.
3498 with connection.schema_editor() as editor:
3499 editor.add_constraint(Author, constraint)
3500 sql = constraint.create_sql(Author, editor)
3501 table = Author._meta.db_table
3502 if connection.features.supports_index_column_ordering:
3503 self.assertIndexOrder(table, constraint.name, ["DESC", "ASC", "ASC"])
3504 constraints = self.get_constraints(table)
3505 self.assertIs(constraints[constraint.name]["unique"], True)
3506 self.assertEqual(len(constraints[constraint.name]["columns"]), 3)
3507 self.assertEqual(constraints[constraint.name]["columns"][1], "uuid")
3508 # SQL contains database functions and columns.
3509 self.assertIs(sql.references_column(table, "height"), True)
3510 self.assertIs(sql.references_column(table, "name"), True)
3511 self.assertIs(sql.references_column(table, "uuid"), True)
3512 self.assertIn("LOWER(%s)" % editor.quote_name("name"), str(sql))
3513 # Remove constraint.
3514 with connection.schema_editor() as editor:
3515 editor.remove_constraint(Author, constraint)
3516 self.assertNotIn(constraint.name, self.get_constraints(table))
3517
3518 @skipUnlessDBFeature("supports_expression_indexes", "supports_partial_indexes")
3519 def test_func_unique_constraint_partial(self):
3520 with connection.schema_editor() as editor:
3521 editor.create_model(Author)
3522 constraint = UniqueConstraint(
3523 Upper("name"),
3524 name="func_upper_cond_weight_uq",
3525 condition=Q(weight__isnull=False),
3526 )
3527 # Add constraint.
3528 with connection.schema_editor() as editor:
3529 editor.add_constraint(Author, constraint)
3530 sql = constraint.create_sql(Author, editor)
3531 table = Author._meta.db_table
3532 constraints = self.get_constraints(table)
3533 self.assertIn(constraint.name, constraints)
3534 self.assertIs(constraints[constraint.name]["unique"], True)
3535 self.assertIs(sql.references_column(table, "name"), True)
3536 self.assertIn("UPPER(%s)" % editor.quote_name("name"), str(sql))
3537 self.assertIn(
3538 "WHERE %s IS NOT NULL" % editor.quote_name("weight"),
3539 str(sql),
3540 )
3541 # Remove constraint.
3542 with connection.schema_editor() as editor:
3543 editor.remove_constraint(Author, constraint)
3544 self.assertNotIn(constraint.name, self.get_constraints(table))
3545
3546 @skipUnlessDBFeature("supports_expression_indexes", "supports_covering_indexes")
3547 def test_func_unique_constraint_covering(self):
3548 with connection.schema_editor() as editor:
3549 editor.create_model(Author)
3550 constraint = UniqueConstraint(
3551 Upper("name"),
3552 name="func_upper_covering_uq",
3553 include=["weight", "height"],
3554 )
3555 # Add constraint.
3556 with connection.schema_editor() as editor:
3557 editor.add_constraint(Author, constraint)
3558 sql = constraint.create_sql(Author, editor)
3559 table = Author._meta.db_table
3560 constraints = self.get_constraints(table)
3561 self.assertIn(constraint.name, constraints)
3562 self.assertIs(constraints[constraint.name]["unique"], True)
3563 self.assertEqual(
3564 constraints[constraint.name]["columns"],
3565 [None, "weight", "height"],
3566 )
3567 self.assertIs(sql.references_column(table, "name"), True)
3568 self.assertIs(sql.references_column(table, "weight"), True)
3569 self.assertIs(sql.references_column(table, "height"), True)
3570 self.assertIn("UPPER(%s)" % editor.quote_name("name"), str(sql))
3571 self.assertIn(
3572 "INCLUDE (%s, %s)"
3573 % (
3574 editor.quote_name("weight"),
3575 editor.quote_name("height"),
3576 ),
3577 str(sql),
3578 )
3579 # Remove constraint.
3580 with connection.schema_editor() as editor:
3581 editor.remove_constraint(Author, constraint)
3582 self.assertNotIn(constraint.name, self.get_constraints(table))
3583
3584 @skipUnlessDBFeature("supports_expression_indexes")
3585 def test_func_unique_constraint_lookups(self):
3586 with connection.schema_editor() as editor:
3587 editor.create_model(Author)
3588 with register_lookup(CharField, Lower), register_lookup(IntegerField, Abs):
3589 constraint = UniqueConstraint(
3590 F("name__lower"),
3591 F("weight__abs"),
3592 name="func_lower_abs_lookup_uq",
3593 )
3594 # Add constraint.
3595 with connection.schema_editor() as editor:
3596 editor.add_constraint(Author, constraint)
3597 sql = constraint.create_sql(Author, editor)
3598 table = Author._meta.db_table
3599 constraints = self.get_constraints(table)
3600 self.assertIn(constraint.name, constraints)
3601 self.assertIs(constraints[constraint.name]["unique"], True)
3602 # SQL contains columns.
3603 self.assertIs(sql.references_column(table, "name"), True)
3604 self.assertIs(sql.references_column(table, "weight"), True)
3605 # Remove constraint.
3606 with connection.schema_editor() as editor:
3607 editor.remove_constraint(Author, constraint)
3608 self.assertNotIn(constraint.name, self.get_constraints(table))
3609
3610 @skipUnlessDBFeature("supports_expression_indexes")
3611 def test_func_unique_constraint_collate(self):
3612 collation = connection.features.test_collations.get("non_default")
3613 if not collation:
3614 self.skipTest("This backend does not support case-insensitive collations.")
3615 with connection.schema_editor() as editor:
3616 editor.create_model(Author)
3617 editor.create_model(BookWithSlug)
3618 constraint = UniqueConstraint(
3619 Collate(F("title"), collation=collation).desc(),
3620 Collate("slug", collation=collation),
3621 name="func_collate_uq",
3622 )
3623 # Add constraint.
3624 with connection.schema_editor() as editor:
3625 editor.add_constraint(BookWithSlug, constraint)
3626 sql = constraint.create_sql(BookWithSlug, editor)
3627 table = BookWithSlug._meta.db_table
3628 constraints = self.get_constraints(table)
3629 self.assertIn(constraint.name, constraints)
3630 self.assertIs(constraints[constraint.name]["unique"], True)
3631 if connection.features.supports_index_column_ordering:
3632 self.assertIndexOrder(table, constraint.name, ["DESC", "ASC"])
3633 # SQL contains columns and a collation.
3634 self.assertIs(sql.references_column(table, "title"), True)
3635 self.assertIs(sql.references_column(table, "slug"), True)
3636 self.assertIn("COLLATE %s" % editor.quote_name(collation), str(sql))
3637 # Remove constraint.
3638 with connection.schema_editor() as editor:
3639 editor.remove_constraint(BookWithSlug, constraint)
3640 self.assertNotIn(constraint.name, self.get_constraints(table))
3641
3642 @skipIfDBFeature("supports_expression_indexes")
3643 def test_func_unique_constraint_unsupported(self):
3644 # UniqueConstraint is ignored on databases that don't support indexes on
3645 # expressions.
3646 with connection.schema_editor() as editor:
3647 editor.create_model(Author)
3648 constraint = UniqueConstraint(F("name"), name="func_name_uq")
3649 with connection.schema_editor() as editor, self.assertNumQueries(0):
3650 self.assertIsNone(editor.add_constraint(Author, constraint))
3651 self.assertIsNone(editor.remove_constraint(Author, constraint))
3652
3653 @skipUnlessDBFeature("supports_expression_indexes")
3654 def test_func_unique_constraint_nonexistent_field(self):
3655 constraint = UniqueConstraint(Lower("nonexistent"), name="func_nonexistent_uq")
3656 msg = (
3657 "Cannot resolve keyword 'nonexistent' into field. Choices are: "
3658 "height, id, name, uuid, weight"
3659 )
3660 with self.assertRaisesMessage(FieldError, msg):
3661 with connection.schema_editor() as editor:
3662 editor.add_constraint(Author, constraint)
3663
3664 @skipUnlessDBFeature("supports_expression_indexes")
3665 def test_func_unique_constraint_nondeterministic(self):
3666 with connection.schema_editor() as editor:
3667 editor.create_model(Author)
3668 constraint = UniqueConstraint(Random(), name="func_random_uq")
3669 with connection.schema_editor() as editor:
3670 with self.assertRaises(DatabaseError):
3671 editor.add_constraint(Author, constraint)
3672
3673 @skipUnlessDBFeature("supports_nulls_distinct_unique_constraints")
3674 def test_unique_constraint_index_nulls_distinct(self):
3675 with connection.schema_editor() as editor:
3676 editor.create_model(Author)
3677 nulls_distinct = UniqueConstraint(
3678 F("height"), name="distinct_height", nulls_distinct=True
3679 )
3680 nulls_not_distinct = UniqueConstraint(
3681 F("weight"), name="not_distinct_weight", nulls_distinct=False
3682 )
3683 with connection.schema_editor() as editor:
3684 editor.add_constraint(Author, nulls_distinct)
3685 editor.add_constraint(Author, nulls_not_distinct)
3686 Author.objects.create(name="", height=None, weight=None)
3687 Author.objects.create(name="", height=None, weight=1)
3688 with self.assertRaises(IntegrityError):
3689 Author.objects.create(name="", height=1, weight=None)
3690 with connection.schema_editor() as editor:
3691 editor.remove_constraint(Author, nulls_distinct)
3692 editor.remove_constraint(Author, nulls_not_distinct)
3693 constraints = self.get_constraints(Author._meta.db_table)
3694 self.assertNotIn(nulls_distinct.name, constraints)
3695 self.assertNotIn(nulls_not_distinct.name, constraints)
3696
3697 @skipUnlessDBFeature("supports_nulls_distinct_unique_constraints")
3698 def test_unique_constraint_nulls_distinct(self):
3699 with connection.schema_editor() as editor:
3700 editor.create_model(Author)
3701 constraint = UniqueConstraint(
3702 fields=["height", "weight"], name="constraint", nulls_distinct=False
3703 )
3704 with connection.schema_editor() as editor:
3705 editor.add_constraint(Author, constraint)
3706 Author.objects.create(name="", height=None, weight=None)
3707 Author.objects.create(name="", height=1, weight=None)
3708 Author.objects.create(name="", height=None, weight=1)
3709 with self.assertRaises(IntegrityError):
3710 Author.objects.create(name="", height=None, weight=None)
3711 with self.assertRaises(IntegrityError):
3712 Author.objects.create(name="", height=1, weight=None)
3713 with self.assertRaises(IntegrityError):
3714 Author.objects.create(name="", height=None, weight=1)
3715 with connection.schema_editor() as editor:
3716 editor.remove_constraint(Author, constraint)
3717 constraints = self.get_constraints(Author._meta.db_table)
3718 self.assertNotIn(constraint.name, constraints)
3719
3720 @skipUnlessDBFeature(
3721 "supports_nulls_distinct_unique_constraints",
3722 "supports_partial_indexes",
3723 )
3724 def test_unique_constraint_nulls_distinct_condition(self):
3725 with connection.schema_editor() as editor:
3726 editor.create_model(Author)
3727 constraint = UniqueConstraint(
3728 fields=["height", "weight"],
3729 name="un_height_weight_start_A",
3730 condition=Q(name__startswith="A"),
3731 nulls_distinct=False,
3732 )
3733 with connection.schema_editor() as editor:
3734 editor.add_constraint(Author, constraint)
3735 Author.objects.create(name="Adam", height=None, weight=None)
3736 Author.objects.create(name="Avocado", height=1, weight=None)
3737 Author.objects.create(name="Adrian", height=None, weight=1)
3738 with self.assertRaises(IntegrityError):
3739 Author.objects.create(name="Alex", height=None, weight=None)
3740 Author.objects.create(name="Bob", height=None, weight=None)
3741 with self.assertRaises(IntegrityError):
3742 Author.objects.create(name="Alex", height=1, weight=None)
3743 Author.objects.create(name="Bill", height=None, weight=None)
3744 with self.assertRaises(IntegrityError):
3745 Author.objects.create(name="Alex", height=None, weight=1)
3746 Author.objects.create(name="Celine", height=None, weight=1)
3747 with connection.schema_editor() as editor:
3748 editor.remove_constraint(Author, constraint)
3749 constraints = self.get_constraints(Author._meta.db_table)
3750 self.assertNotIn(constraint.name, constraints)
3751
3752 @skipIfDBFeature("supports_nulls_distinct_unique_constraints")
3753 def test_unique_constraint_nulls_distinct_unsupported(self):
3754 # UniqueConstraint is ignored on databases that don't support
3755 # NULLS [NOT] DISTINCT.
3756 with connection.schema_editor() as editor:
3757 editor.create_model(Author)
3758 constraint = UniqueConstraint(
3759 F("name"), name="func_name_uq", nulls_distinct=True
3760 )
3761 with connection.schema_editor() as editor, self.assertNumQueries(0):
3762 self.assertIsNone(editor.add_constraint(Author, constraint))
3763 self.assertIsNone(editor.remove_constraint(Author, constraint))
3764
3765 def test_index_together(self):
3766 """
3767 Tests removing and adding index_together constraints on a model.
3768 """
3769 # Create the table
3770 with connection.schema_editor() as editor:
3771 editor.create_model(Tag)
3772 # Ensure there's no index on the year/slug columns first
3773 self.assertIs(
3774 any(
3775 c["index"]
3776 for c in self.get_constraints("schema_tag").values()
3777 if c["columns"] == ["slug", "title"]
3778 ),
3779 False,
3780 )
3781 # Alter the model to add an index
3782 with connection.schema_editor() as editor:
3783 editor.alter_index_together(Tag, [], [("slug", "title")])
3784 # Ensure there is now an index
3785 self.assertIs(
3786 any(
3787 c["index"]
3788 for c in self.get_constraints("schema_tag").values()
3789 if c["columns"] == ["slug", "title"]
3790 ),
3791 True,
3792 )
3793 # Alter it back
3794 new_field2 = SlugField(unique=True)
3795 new_field2.set_attributes_from_name("slug")
3796 with connection.schema_editor() as editor:
3797 editor.alter_index_together(Tag, [("slug", "title")], [])
3798 # Ensure there's no index
3799 self.assertIs(
3800 any(
3801 c["index"]
3802 for c in self.get_constraints("schema_tag").values()
3803 if c["columns"] == ["slug", "title"]
3804 ),
3805 False,
3806 )
3807
3808 @isolate_apps("schema")
3809 def test_db_table(self):
3810 """
3811 Tests renaming of the table
3812 """
3813
3814 class Author(Model):
3815 name = CharField(max_length=255)
3816
3817 class Meta:
3818 app_label = "schema"
3819
3820 class Book(Model):
3821 author = ForeignKey(Author, CASCADE)
3822
3823 class Meta:
3824 app_label = "schema"
3825
3826 # Create the table and one referring it.
3827 with connection.schema_editor() as editor:
3828 editor.create_model(Author)
3829 editor.create_model(Book)
3830 # Ensure the table is there to begin with
3831 columns = self.column_classes(Author)
3832 self.assertEqual(
3833 columns["name"][0],
3834 connection.features.introspected_field_types["CharField"],
3835 )
3836 # Alter the table
3837 with connection.schema_editor() as editor:
3838 editor.alter_db_table(Author, "schema_author", "schema_otherauthor")
3839 Author._meta.db_table = "schema_otherauthor"
3840 columns = self.column_classes(Author)
3841 self.assertEqual(
3842 columns["name"][0],
3843 connection.features.introspected_field_types["CharField"],
3844 )
3845 # Ensure the foreign key reference was updated
3846 self.assertForeignKeyExists(Book, "author_id", "schema_otherauthor")
3847 # Alter the table again
3848 with connection.schema_editor() as editor:
3849 editor.alter_db_table(Author, "schema_otherauthor", "schema_author")
3850 # Ensure the table is still there
3851 Author._meta.db_table = "schema_author"
3852 columns = self.column_classes(Author)
3853 self.assertEqual(
3854 columns["name"][0],
3855 connection.features.introspected_field_types["CharField"],
3856 )
3857
3858 def test_add_remove_index(self):
3859 """
3860 Tests index addition and removal
3861 """
3862 # Create the table
3863 with connection.schema_editor() as editor:
3864 editor.create_model(Author)
3865 # Ensure the table is there and has no index
3866 self.assertNotIn("title", self.get_indexes(Author._meta.db_table))
3867 # Add the index
3868 index = Index(fields=["name"], name="author_title_idx")
3869 with connection.schema_editor() as editor:
3870 editor.add_index(Author, index)
3871 self.assertIn("name", self.get_indexes(Author._meta.db_table))
3872 # Drop the index
3873 with connection.schema_editor() as editor:
3874 editor.remove_index(Author, index)
3875 self.assertNotIn("name", self.get_indexes(Author._meta.db_table))
3876
3877 def test_remove_db_index_doesnt_remove_custom_indexes(self):
3878 """
3879 Changing db_index to False doesn't remove indexes from Meta.indexes.
3880 """
3881 with connection.schema_editor() as editor:
3882 editor.create_model(AuthorWithIndexedName)
3883 self.local_models = [AuthorWithIndexedName]
3884 # Ensure the table has its index
3885 self.assertIn("name", self.get_indexes(AuthorWithIndexedName._meta.db_table))
3886
3887 # Add the custom index
3888 index = Index(fields=["-name"], name="author_name_idx")
3889 author_index_name = index.name
3890 with connection.schema_editor() as editor:
3891 db_index_name = editor._create_index_name(
3892 table_name=AuthorWithIndexedName._meta.db_table,
3893 column_names=("name",),
3894 )
3895 try:
3896 AuthorWithIndexedName._meta.indexes = [index]
3897 with connection.schema_editor() as editor:
3898 editor.add_index(AuthorWithIndexedName, index)
3899 old_constraints = self.get_constraints(AuthorWithIndexedName._meta.db_table)
3900 self.assertIn(author_index_name, old_constraints)
3901 self.assertIn(db_index_name, old_constraints)
3902 # Change name field to db_index=False
3903 old_field = AuthorWithIndexedName._meta.get_field("name")
3904 new_field = CharField(max_length=255)
3905 new_field.set_attributes_from_name("name")
3906 with connection.schema_editor() as editor:
3907 editor.alter_field(
3908 AuthorWithIndexedName, old_field, new_field, strict=True
3909 )
3910 new_constraints = self.get_constraints(AuthorWithIndexedName._meta.db_table)
3911 self.assertNotIn(db_index_name, new_constraints)
3912 # The index from Meta.indexes is still in the database.
3913 self.assertIn(author_index_name, new_constraints)
3914 # Drop the index
3915 with connection.schema_editor() as editor:
3916 editor.remove_index(AuthorWithIndexedName, index)
3917 finally:
3918 AuthorWithIndexedName._meta.indexes = []
3919
3920 def test_order_index(self):
3921 """
3922 Indexes defined with ordering (ASC/DESC) defined on column
3923 """
3924 with connection.schema_editor() as editor:
3925 editor.create_model(Author)
3926 # The table doesn't have an index
3927 self.assertNotIn("title", self.get_indexes(Author._meta.db_table))
3928 index_name = "author_name_idx"
3929 # Add the index
3930 index = Index(fields=["name", "-weight"], name=index_name)
3931 with connection.schema_editor() as editor:
3932 editor.add_index(Author, index)
3933 if connection.features.supports_index_column_ordering:
3934 self.assertIndexOrder(Author._meta.db_table, index_name, ["ASC", "DESC"])
3935 # Drop the index
3936 with connection.schema_editor() as editor:
3937 editor.remove_index(Author, index)
3938
3939 def test_indexes(self):
3940 """
3941 Tests creation/altering of indexes
3942 """
3943 # Create the table
3944 with connection.schema_editor() as editor:
3945 editor.create_model(Author)
3946 editor.create_model(Book)
3947 # Ensure the table is there and has the right index
3948 self.assertIn(
3949 "title",
3950 self.get_indexes(Book._meta.db_table),
3951 )
3952 # Alter to remove the index
3953 old_field = Book._meta.get_field("title")
3954 new_field = CharField(max_length=100, db_index=False)
3955 new_field.set_attributes_from_name("title")
3956 with connection.schema_editor() as editor:
3957 editor.alter_field(Book, old_field, new_field, strict=True)
3958 # Ensure the table is there and has no index
3959 self.assertNotIn(
3960 "title",
3961 self.get_indexes(Book._meta.db_table),
3962 )
3963 # Alter to re-add the index
3964 new_field2 = Book._meta.get_field("title")
3965 with connection.schema_editor() as editor:
3966 editor.alter_field(Book, new_field, new_field2, strict=True)
3967 # Ensure the table is there and has the index again
3968 self.assertIn(
3969 "title",
3970 self.get_indexes(Book._meta.db_table),
3971 )
3972 # Add a unique column, verify that creates an implicit index
3973 new_field3 = BookWithSlug._meta.get_field("slug")
3974 with connection.schema_editor() as editor:
3975 editor.add_field(Book, new_field3)
3976 self.assertIn(
3977 "slug",
3978 self.get_uniques(Book._meta.db_table),
3979 )
3980 # Remove the unique, check the index goes with it
3981 new_field4 = CharField(max_length=20, unique=False)
3982 new_field4.set_attributes_from_name("slug")
3983 with connection.schema_editor() as editor:
3984 editor.alter_field(BookWithSlug, new_field3, new_field4, strict=True)
3985 self.assertNotIn(
3986 "slug",
3987 self.get_uniques(Book._meta.db_table),
3988 )
3989
3990 def test_text_field_with_db_index(self):
3991 with connection.schema_editor() as editor:
3992 editor.create_model(AuthorTextFieldWithIndex)
3993 # The text_field index is present if the database supports it.
3994 assertion = (
3995 self.assertIn
3996 if connection.features.supports_index_on_text_field
3997 else self.assertNotIn
3998 )
3999 assertion(
4000 "text_field", self.get_indexes(AuthorTextFieldWithIndex._meta.db_table)
4001 )
4002
4003 def _index_expressions_wrappers(self):
4004 index_expression = IndexExpression()
4005 index_expression.set_wrapper_classes(connection)
4006 return ", ".join(
4007 [
4008 wrapper_cls.__qualname__
4009 for wrapper_cls in index_expression.wrapper_classes
4010 ]
4011 )
4012
4013 @skipUnlessDBFeature("supports_expression_indexes")
4014 def test_func_index_multiple_wrapper_references(self):
4015 index = Index(OrderBy(F("name").desc(), descending=True), name="name")
4016 msg = (
4017 "Multiple references to %s can't be used in an indexed expression."
4018 % self._index_expressions_wrappers()
4019 )
4020 with connection.schema_editor() as editor:
4021 with self.assertRaisesMessage(ValueError, msg):
4022 editor.add_index(Author, index)
4023
4024 @skipUnlessDBFeature("supports_expression_indexes")
4025 def test_func_index_invalid_topmost_expressions(self):
4026 index = Index(Upper(F("name").desc()), name="name")
4027 msg = (
4028 "%s must be topmost expressions in an indexed expression."
4029 % self._index_expressions_wrappers()
4030 )
4031 with connection.schema_editor() as editor:
4032 with self.assertRaisesMessage(ValueError, msg):
4033 editor.add_index(Author, index)
4034
4035 @skipUnlessDBFeature("supports_expression_indexes")
4036 def test_func_index(self):
4037 with connection.schema_editor() as editor:
4038 editor.create_model(Author)
4039 index = Index(Lower("name").desc(), name="func_lower_idx")
4040 # Add index.
4041 with connection.schema_editor() as editor:
4042 editor.add_index(Author, index)
4043 sql = index.create_sql(Author, editor)
4044 table = Author._meta.db_table
4045 if connection.features.supports_index_column_ordering:
4046 self.assertIndexOrder(table, index.name, ["DESC"])
4047 # SQL contains a database function.
4048 self.assertIs(sql.references_column(table, "name"), True)
4049 self.assertIn("LOWER(%s)" % editor.quote_name("name"), str(sql))
4050 # Remove index.
4051 with connection.schema_editor() as editor:
4052 editor.remove_index(Author, index)
4053 self.assertNotIn(index.name, self.get_constraints(table))
4054
4055 @skipUnlessDBFeature("supports_expression_indexes")
4056 def test_func_index_f(self):
4057 with connection.schema_editor() as editor:
4058 editor.create_model(Tag)
4059 index = Index("slug", F("title").desc(), name="func_f_idx")
4060 # Add index.
4061 with connection.schema_editor() as editor:
4062 editor.add_index(Tag, index)
4063 sql = index.create_sql(Tag, editor)
4064 table = Tag._meta.db_table
4065 self.assertIn(index.name, self.get_constraints(table))
4066 if connection.features.supports_index_column_ordering:
4067 self.assertIndexOrder(Tag._meta.db_table, index.name, ["ASC", "DESC"])
4068 # SQL contains columns.
4069 self.assertIs(sql.references_column(table, "slug"), True)
4070 self.assertIs(sql.references_column(table, "title"), True)
4071 # Remove index.
4072 with connection.schema_editor() as editor:
4073 editor.remove_index(Tag, index)
4074 self.assertNotIn(index.name, self.get_constraints(table))
4075
4076 @skipUnlessDBFeature("supports_expression_indexes")
4077 def test_func_index_lookups(self):
4078 with connection.schema_editor() as editor:
4079 editor.create_model(Author)
4080 with register_lookup(CharField, Lower), register_lookup(IntegerField, Abs):
4081 index = Index(
4082 F("name__lower"),
4083 F("weight__abs"),
4084 name="func_lower_abs_lookup_idx",
4085 )
4086 # Add index.
4087 with connection.schema_editor() as editor:
4088 editor.add_index(Author, index)
4089 sql = index.create_sql(Author, editor)
4090 table = Author._meta.db_table
4091 self.assertIn(index.name, self.get_constraints(table))
4092 # SQL contains columns.
4093 self.assertIs(sql.references_column(table, "name"), True)
4094 self.assertIs(sql.references_column(table, "weight"), True)
4095 # Remove index.
4096 with connection.schema_editor() as editor:
4097 editor.remove_index(Author, index)
4098 self.assertNotIn(index.name, self.get_constraints(table))
4099
4100 @skipUnlessDBFeature("supports_expression_indexes")
4101 def test_composite_func_index(self):
4102 with connection.schema_editor() as editor:
4103 editor.create_model(Author)
4104 index = Index(Lower("name"), Upper("name"), name="func_lower_upper_idx")
4105 # Add index.
4106 with connection.schema_editor() as editor:
4107 editor.add_index(Author, index)
4108 sql = index.create_sql(Author, editor)
4109 table = Author._meta.db_table
4110 self.assertIn(index.name, self.get_constraints(table))
4111 # SQL contains database functions.
4112 self.assertIs(sql.references_column(table, "name"), True)
4113 sql = str(sql)
4114 self.assertIn("LOWER(%s)" % editor.quote_name("name"), sql)
4115 self.assertIn("UPPER(%s)" % editor.quote_name("name"), sql)
4116 self.assertLess(sql.index("LOWER"), sql.index("UPPER"))
4117 # Remove index.
4118 with connection.schema_editor() as editor:
4119 editor.remove_index(Author, index)
4120 self.assertNotIn(index.name, self.get_constraints(table))
4121
4122 @skipUnlessDBFeature("supports_expression_indexes")
4123 def test_composite_func_index_field_and_expression(self):
4124 with connection.schema_editor() as editor:
4125 editor.create_model(Author)
4126 editor.create_model(Book)
4127 index = Index(
4128 F("author").desc(),
4129 Lower("title").asc(),
4130 "pub_date",
4131 name="func_f_lower_field_idx",
4132 )
4133 # Add index.
4134 with connection.schema_editor() as editor:
4135 editor.add_index(Book, index)
4136 sql = index.create_sql(Book, editor)
4137 table = Book._meta.db_table
4138 constraints = self.get_constraints(table)
4139 if connection.features.supports_index_column_ordering:
4140 self.assertIndexOrder(table, index.name, ["DESC", "ASC", "ASC"])
4141 self.assertEqual(len(constraints[index.name]["columns"]), 3)
4142 self.assertEqual(constraints[index.name]["columns"][2], "pub_date")
4143 # SQL contains database functions and columns.
4144 self.assertIs(sql.references_column(table, "author_id"), True)
4145 self.assertIs(sql.references_column(table, "title"), True)
4146 self.assertIs(sql.references_column(table, "pub_date"), True)
4147 self.assertIn("LOWER(%s)" % editor.quote_name("title"), str(sql))
4148 # Remove index.
4149 with connection.schema_editor() as editor:
4150 editor.remove_index(Book, index)
4151 self.assertNotIn(index.name, self.get_constraints(table))
4152
4153 @skipUnlessDBFeature("supports_expression_indexes")
4154 @isolate_apps("schema")
4155 def test_func_index_f_decimalfield(self):
4156 class Node(Model):
4157 value = DecimalField(max_digits=5, decimal_places=2)
4158
4159 class Meta:
4160 app_label = "schema"
4161
4162 with connection.schema_editor() as editor:
4163 editor.create_model(Node)
4164 index = Index(F("value"), name="func_f_decimalfield_idx")
4165 # Add index.
4166 with connection.schema_editor() as editor:
4167 editor.add_index(Node, index)
4168 sql = index.create_sql(Node, editor)
4169 table = Node._meta.db_table
4170 self.assertIn(index.name, self.get_constraints(table))
4171 self.assertIs(sql.references_column(table, "value"), True)
4172 # SQL doesn't contain casting.
4173 self.assertNotIn("CAST", str(sql))
4174 # Remove index.
4175 with connection.schema_editor() as editor:
4176 editor.remove_index(Node, index)
4177 self.assertNotIn(index.name, self.get_constraints(table))
4178
4179 @skipUnlessDBFeature("supports_expression_indexes")
4180 def test_func_index_cast(self):
4181 with connection.schema_editor() as editor:
4182 editor.create_model(Author)
4183 index = Index(Cast("weight", FloatField()), name="func_cast_idx")
4184 # Add index.
4185 with connection.schema_editor() as editor:
4186 editor.add_index(Author, index)
4187 sql = index.create_sql(Author, editor)
4188 table = Author._meta.db_table
4189 self.assertIn(index.name, self.get_constraints(table))
4190 self.assertIs(sql.references_column(table, "weight"), True)
4191 # Remove index.
4192 with connection.schema_editor() as editor:
4193 editor.remove_index(Author, index)
4194 self.assertNotIn(index.name, self.get_constraints(table))
4195
4196 @skipUnlessDBFeature("supports_expression_indexes")
4197 def test_func_index_collate(self):
4198 collation = connection.features.test_collations.get("non_default")
4199 if not collation:
4200 self.skipTest("This backend does not support case-insensitive collations.")
4201 with connection.schema_editor() as editor:
4202 editor.create_model(Author)
4203 editor.create_model(BookWithSlug)
4204 index = Index(
4205 Collate(F("title"), collation=collation).desc(),
4206 Collate("slug", collation=collation),
4207 name="func_collate_idx",
4208 )
4209 # Add index.
4210 with connection.schema_editor() as editor:
4211 editor.add_index(BookWithSlug, index)
4212 sql = index.create_sql(BookWithSlug, editor)
4213 table = Book._meta.db_table
4214 self.assertIn(index.name, self.get_constraints(table))
4215 if connection.features.supports_index_column_ordering:
4216 self.assertIndexOrder(table, index.name, ["DESC", "ASC"])
4217 # SQL contains columns and a collation.
4218 self.assertIs(sql.references_column(table, "title"), True)
4219 self.assertIs(sql.references_column(table, "slug"), True)
4220 self.assertIn("COLLATE %s" % editor.quote_name(collation), str(sql))
4221 # Remove index.
4222 with connection.schema_editor() as editor:
4223 editor.remove_index(Book, index)
4224 self.assertNotIn(index.name, self.get_constraints(table))
4225
4226 @skipUnlessDBFeature("supports_expression_indexes")
4227 @skipIfDBFeature("collate_as_index_expression")
4228 def test_func_index_collate_f_ordered(self):
4229 collation = connection.features.test_collations.get("non_default")
4230 if not collation:
4231 self.skipTest("This backend does not support case-insensitive collations.")
4232 with connection.schema_editor() as editor:
4233 editor.create_model(Author)
4234 index = Index(
4235 Collate(F("name").desc(), collation=collation),
4236 name="func_collate_f_desc_idx",
4237 )
4238 # Add index.
4239 with connection.schema_editor() as editor:
4240 editor.add_index(Author, index)
4241 sql = index.create_sql(Author, editor)
4242 table = Author._meta.db_table
4243 self.assertIn(index.name, self.get_constraints(table))
4244 if connection.features.supports_index_column_ordering:
4245 self.assertIndexOrder(table, index.name, ["DESC"])
4246 # SQL contains columns and a collation.
4247 self.assertIs(sql.references_column(table, "name"), True)
4248 self.assertIn("COLLATE %s" % editor.quote_name(collation), str(sql))
4249 # Remove index.
4250 with connection.schema_editor() as editor:
4251 editor.remove_index(Author, index)
4252 self.assertNotIn(index.name, self.get_constraints(table))
4253
4254 @skipUnlessDBFeature("supports_expression_indexes")
4255 def test_func_index_calc(self):
4256 with connection.schema_editor() as editor:
4257 editor.create_model(Author)
4258 index = Index(F("height") / (F("weight") + Value(5)), name="func_calc_idx")
4259 # Add index.
4260 with connection.schema_editor() as editor:
4261 editor.add_index(Author, index)
4262 sql = index.create_sql(Author, editor)
4263 table = Author._meta.db_table
4264 self.assertIn(index.name, self.get_constraints(table))
4265 # SQL contains columns and expressions.
4266 self.assertIs(sql.references_column(table, "height"), True)
4267 self.assertIs(sql.references_column(table, "weight"), True)
4268 sql = str(sql)
4269 self.assertIs(
4270 sql.index(editor.quote_name("height"))
4271 < sql.index("/")
4272 < sql.index(editor.quote_name("weight"))
4273 < sql.index("+")
4274 < sql.index("5"),
4275 True,
4276 )
4277 # Remove index.
4278 with connection.schema_editor() as editor:
4279 editor.remove_index(Author, index)
4280 self.assertNotIn(index.name, self.get_constraints(table))
4281
4282 @skipUnlessDBFeature("supports_expression_indexes", "supports_json_field")
4283 @isolate_apps("schema")
4284 def test_func_index_json_key_transform(self):
4285 class JSONModel(Model):
4286 field = JSONField()
4287
4288 class Meta:
4289 app_label = "schema"
4290
4291 with connection.schema_editor() as editor:
4292 editor.create_model(JSONModel)
4293 self.isolated_local_models = [JSONModel]
4294 index = Index("field__some_key", name="func_json_key_idx")
4295 with connection.schema_editor() as editor:
4296 editor.add_index(JSONModel, index)
4297 sql = index.create_sql(JSONModel, editor)
4298 table = JSONModel._meta.db_table
4299 self.assertIn(index.name, self.get_constraints(table))
4300 self.assertIs(sql.references_column(table, "field"), True)
4301 with connection.schema_editor() as editor:
4302 editor.remove_index(JSONModel, index)
4303 self.assertNotIn(index.name, self.get_constraints(table))
4304
4305 @skipUnlessDBFeature("supports_expression_indexes", "supports_json_field")
4306 @isolate_apps("schema")
4307 def test_func_index_json_key_transform_cast(self):
4308 class JSONModel(Model):
4309 field = JSONField()
4310
4311 class Meta:
4312 app_label = "schema"
4313
4314 with connection.schema_editor() as editor:
4315 editor.create_model(JSONModel)
4316 self.isolated_local_models = [JSONModel]
4317 index = Index(
4318 Cast(KeyTextTransform("some_key", "field"), IntegerField()),
4319 name="func_json_key_cast_idx",
4320 )
4321 with connection.schema_editor() as editor:
4322 editor.add_index(JSONModel, index)
4323 sql = index.create_sql(JSONModel, editor)
4324 table = JSONModel._meta.db_table
4325 self.assertIn(index.name, self.get_constraints(table))
4326 self.assertIs(sql.references_column(table, "field"), True)
4327 with connection.schema_editor() as editor:
4328 editor.remove_index(JSONModel, index)
4329 self.assertNotIn(index.name, self.get_constraints(table))
4330
4331 @skipIfDBFeature("supports_expression_indexes")
4332 def test_func_index_unsupported(self):
4333 # Index is ignored on databases that don't support indexes on
4334 # expressions.
4335 with connection.schema_editor() as editor:
4336 editor.create_model(Author)
4337 index = Index(F("name"), name="random_idx")
4338 with connection.schema_editor() as editor, self.assertNumQueries(0):
4339 self.assertIsNone(editor.add_index(Author, index))
4340 self.assertIsNone(editor.remove_index(Author, index))
4341
4342 @skipUnlessDBFeature("supports_expression_indexes")
4343 def test_func_index_nonexistent_field(self):
4344 index = Index(Lower("nonexistent"), name="func_nonexistent_idx")
4345 msg = (
4346 "Cannot resolve keyword 'nonexistent' into field. Choices are: "
4347 "height, id, name, uuid, weight"
4348 )
4349 with self.assertRaisesMessage(FieldError, msg):
4350 with connection.schema_editor() as editor:
4351 editor.add_index(Author, index)
4352
4353 @skipUnlessDBFeature("supports_expression_indexes")
4354 def test_func_index_nondeterministic(self):
4355 with connection.schema_editor() as editor:
4356 editor.create_model(Author)
4357 index = Index(Random(), name="func_random_idx")
4358 with connection.schema_editor() as editor:
4359 with self.assertRaises(DatabaseError):
4360 editor.add_index(Author, index)
4361
4362 def test_primary_key(self):
4363 """
4364 Tests altering of the primary key
4365 """
4366 # Create the table
4367 with connection.schema_editor() as editor:
4368 editor.create_model(Tag)
4369 # Ensure the table is there and has the right PK
4370 self.assertEqual(self.get_primary_key(Tag._meta.db_table), "id")
4371 # Alter to change the PK
4372 id_field = Tag._meta.get_field("id")
4373 old_field = Tag._meta.get_field("slug")
4374 new_field = SlugField(primary_key=True)
4375 new_field.set_attributes_from_name("slug")
4376 new_field.model = Tag
4377 with connection.schema_editor() as editor:
4378 editor.remove_field(Tag, id_field)
4379 editor.alter_field(Tag, old_field, new_field)
4380 # Ensure the PK changed
4381 self.assertNotIn(
4382 "id",
4383 self.get_indexes(Tag._meta.db_table),
4384 )
4385 self.assertEqual(self.get_primary_key(Tag._meta.db_table), "slug")
4386
4387 def test_alter_primary_key_the_same_name(self):
4388 with connection.schema_editor() as editor:
4389 editor.create_model(Thing)
4390
4391 old_field = Thing._meta.get_field("when")
4392 new_field = CharField(max_length=2, primary_key=True)
4393 new_field.set_attributes_from_name("when")
4394 new_field.model = Thing
4395 with connection.schema_editor() as editor:
4396 editor.alter_field(Thing, old_field, new_field, strict=True)
4397 self.assertEqual(self.get_primary_key(Thing._meta.db_table), "when")
4398 with connection.schema_editor() as editor:
4399 editor.alter_field(Thing, new_field, old_field, strict=True)
4400 self.assertEqual(self.get_primary_key(Thing._meta.db_table), "when")
4401
4402 def test_context_manager_exit(self):
4403 """
4404 Ensures transaction is correctly closed when an error occurs
4405 inside a SchemaEditor context.
4406 """
4407
4408 class SomeError(Exception):
4409 pass
4410
4411 try:
4412 with connection.schema_editor():
4413 raise SomeError
4414 except SomeError:
4415 self.assertFalse(connection.in_atomic_block)
4416
4417 @skipIfDBFeature("can_rollback_ddl")
4418 def test_unsupported_transactional_ddl_disallowed(self):
4419 message = (
4420 "Executing DDL statements while in a transaction on databases "
4421 "that can't perform a rollback is prohibited."
4422 )
4423 with atomic(), connection.schema_editor() as editor:
4424 with self.assertRaisesMessage(TransactionManagementError, message):
4425 editor.execute(
4426 editor.sql_create_table % {"table": "foo", "definition": ""}
4427 )
4428
4429 @skipUnlessDBFeature("supports_foreign_keys", "indexes_foreign_keys")
4430 def test_foreign_key_index_long_names_regression(self):
4431 """
4432 Regression test for #21497.
4433 Only affects databases that supports foreign keys.
4434 """
4435 # Create the table
4436 with connection.schema_editor() as editor:
4437 editor.create_model(AuthorWithEvenLongerName)
4438 editor.create_model(BookWithLongName)
4439 # Find the properly shortened column name
4440 column_name = connection.ops.quote_name(
4441 "author_foreign_key_with_really_long_field_name_id"
4442 )
4443 column_name = column_name[1:-1].lower() # unquote, and, for Oracle, un-upcase
4444 # Ensure the table is there and has an index on the column
4445 self.assertIn(
4446 column_name,
4447 self.get_indexes(BookWithLongName._meta.db_table),
4448 )
4449
4450 @skipUnlessDBFeature("supports_foreign_keys")
4451 def test_add_foreign_key_long_names(self):
4452 """
4453 Regression test for #23009.
4454 Only affects databases that supports foreign keys.
4455 """
4456 # Create the initial tables
4457 with connection.schema_editor() as editor:
4458 editor.create_model(AuthorWithEvenLongerName)
4459 editor.create_model(BookWithLongName)
4460 # Add a second FK, this would fail due to long ref name before the fix
4461 new_field = ForeignKey(
4462 AuthorWithEvenLongerName, CASCADE, related_name="something"
4463 )
4464 new_field.set_attributes_from_name(
4465 "author_other_really_long_named_i_mean_so_long_fk"
4466 )
4467 with connection.schema_editor() as editor:
4468 editor.add_field(BookWithLongName, new_field)
4469
4470 @isolate_apps("schema")
4471 @skipUnlessDBFeature("supports_foreign_keys")
4472 def test_add_foreign_key_quoted_db_table(self):
4473 class Author(Model):
4474 class Meta:
4475 db_table = '"table_author_double_quoted"'
4476 app_label = "schema"
4477
4478 class Book(Model):
4479 author = ForeignKey(Author, CASCADE)
4480
4481 class Meta:
4482 app_label = "schema"
4483
4484 with connection.schema_editor() as editor:
4485 editor.create_model(Author)
4486 editor.create_model(Book)
4487 self.isolated_local_models = [Author]
4488 if connection.vendor == "mysql":
4489 self.assertForeignKeyExists(
4490 Book, "author_id", '"table_author_double_quoted"'
4491 )
4492 else:
4493 self.assertForeignKeyExists(Book, "author_id", "table_author_double_quoted")
4494
4495 def test_add_foreign_object(self):
4496 with connection.schema_editor() as editor:
4497 editor.create_model(BookForeignObj)
4498 self.local_models = [BookForeignObj]
4499
4500 new_field = ForeignObject(
4501 Author, on_delete=CASCADE, from_fields=["author_id"], to_fields=["id"]
4502 )
4503 new_field.set_attributes_from_name("author")
4504 with connection.schema_editor() as editor:
4505 editor.add_field(BookForeignObj, new_field)
4506
4507 def test_creation_deletion_reserved_names(self):
4508 """
4509 Tries creating a model's table, and then deleting it when it has a
4510 SQL reserved name.
4511 """
4512 # Create the table
4513 with connection.schema_editor() as editor:
4514 try:
4515 editor.create_model(Thing)
4516 except OperationalError as e:
4517 self.fail(
4518 "Errors when applying initial migration for a model "
4519 "with a table named after an SQL reserved word: %s" % e
4520 )
4521 # The table is there
4522 list(Thing.objects.all())
4523 # Clean up that table
4524 with connection.schema_editor() as editor:
4525 editor.delete_model(Thing)
4526 # The table is gone
4527 with self.assertRaises(DatabaseError):
4528 list(Thing.objects.all())
4529
4530 def test_remove_constraints_capital_letters(self):
4531 """
4532 #23065 - Constraint names must be quoted if they contain capital letters.
4533 """
4534
4535 def get_field(*args, field_class=IntegerField, **kwargs):
4536 kwargs["db_column"] = "CamelCase"
4537 field = field_class(*args, **kwargs)
4538 field.set_attributes_from_name("CamelCase")
4539 return field
4540
4541 model = Author
4542 field = get_field()
4543 table = model._meta.db_table
4544 column = field.column
4545 identifier_converter = connection.introspection.identifier_converter
4546
4547 with connection.schema_editor() as editor:
4548 editor.create_model(model)
4549 editor.add_field(model, field)
4550
4551 constraint_name = "CamelCaseIndex"
4552 expected_constraint_name = identifier_converter(constraint_name)
4553 editor.execute(
4554 editor.sql_create_index
4555 % {
4556 "table": editor.quote_name(table),
4557 "name": editor.quote_name(constraint_name),
4558 "using": "",
4559 "columns": editor.quote_name(column),
4560 "extra": "",
4561 "condition": "",
4562 "include": "",
4563 }
4564 )
4565 self.assertIn(
4566 expected_constraint_name, self.get_constraints(model._meta.db_table)
4567 )
4568 editor.alter_field(model, get_field(db_index=True), field, strict=True)
4569 self.assertNotIn(
4570 expected_constraint_name, self.get_constraints(model._meta.db_table)
4571 )
4572
4573 constraint_name = "CamelCaseUniqConstraint"
4574 expected_constraint_name = identifier_converter(constraint_name)
4575 editor.execute(editor._create_unique_sql(model, [field], constraint_name))
4576 self.assertIn(
4577 expected_constraint_name, self.get_constraints(model._meta.db_table)
4578 )
4579 editor.alter_field(model, get_field(unique=True), field, strict=True)
4580 self.assertNotIn(
4581 expected_constraint_name, self.get_constraints(model._meta.db_table)
4582 )
4583
4584 if editor.sql_create_fk and connection.features.can_introspect_foreign_keys:
4585 constraint_name = "CamelCaseFKConstraint"
4586 expected_constraint_name = identifier_converter(constraint_name)
4587 editor.execute(
4588 editor.sql_create_fk
4589 % {
4590 "table": editor.quote_name(table),
4591 "name": editor.quote_name(constraint_name),
4592 "column": editor.quote_name(column),
4593 "to_table": editor.quote_name(table),
4594 "to_column": editor.quote_name(model._meta.auto_field.column),
4595 "deferrable": connection.ops.deferrable_sql(),
4596 }
4597 )
4598 self.assertIn(
4599 expected_constraint_name, self.get_constraints(model._meta.db_table)
4600 )
4601 editor.alter_field(
4602 model,
4603 get_field(Author, CASCADE, field_class=ForeignKey),
4604 field,
4605 strict=True,
4606 )
4607 self.assertNotIn(
4608 expected_constraint_name, self.get_constraints(model._meta.db_table)
4609 )
4610
4611 def test_add_field_use_effective_default(self):
4612 """
4613 #23987 - effective_default() should be used as the field default when
4614 adding a new field.
4615 """
4616 # Create the table
4617 with connection.schema_editor() as editor:
4618 editor.create_model(Author)
4619 # Ensure there's no surname field
4620 columns = self.column_classes(Author)
4621 self.assertNotIn("surname", columns)
4622 # Create a row
4623 Author.objects.create(name="Anonymous1")
4624 # Add new CharField to ensure default will be used from effective_default
4625 new_field = CharField(max_length=15, blank=True)
4626 new_field.set_attributes_from_name("surname")
4627 with connection.schema_editor() as editor:
4628 editor.add_field(Author, new_field)
4629 # Ensure field was added with the right default
4630 with connection.cursor() as cursor:
4631 cursor.execute("SELECT surname FROM schema_author;")
4632 item = cursor.fetchall()[0]
4633 self.assertEqual(
4634 item[0],
4635 None if connection.features.interprets_empty_strings_as_nulls else "",
4636 )
4637
4638 def test_add_field_default_dropped(self):
4639 # Create the table
4640 with connection.schema_editor() as editor:
4641 editor.create_model(Author)
4642 # Ensure there's no surname field
4643 columns = self.column_classes(Author)
4644 self.assertNotIn("surname", columns)
4645 # Create a row
4646 Author.objects.create(name="Anonymous1")
4647 # Add new CharField with a default
4648 new_field = CharField(max_length=15, blank=True, default="surname default")
4649 new_field.set_attributes_from_name("surname")
4650 with connection.schema_editor() as editor:
4651 editor.add_field(Author, new_field)
4652 # Ensure field was added with the right default
4653 with connection.cursor() as cursor:
4654 cursor.execute("SELECT surname FROM schema_author;")
4655 item = cursor.fetchall()[0]
4656 self.assertEqual(item[0], "surname default")
4657 # And that the default is no longer set in the database.
4658 field = next(
4659 f
4660 for f in connection.introspection.get_table_description(
4661 cursor, "schema_author"
4662 )
4663 if f.name == "surname"
4664 )
4665 if connection.features.can_introspect_default:
4666 self.assertIsNone(field.default)
4667
4668 def test_add_field_default_nullable(self):
4669 with connection.schema_editor() as editor:
4670 editor.create_model(Author)
4671 # Add new nullable CharField with a default.
4672 new_field = CharField(max_length=15, blank=True, null=True, default="surname")
4673 new_field.set_attributes_from_name("surname")
4674 with connection.schema_editor() as editor:
4675 editor.add_field(Author, new_field)
4676 Author.objects.create(name="Anonymous1")
4677 with connection.cursor() as cursor:
4678 cursor.execute("SELECT surname FROM schema_author;")
4679 item = cursor.fetchall()[0]
4680 self.assertIsNone(item[0])
4681 field = next(
4682 f
4683 for f in connection.introspection.get_table_description(
4684 cursor,
4685 "schema_author",
4686 )
4687 if f.name == "surname"
4688 )
4689 # Field is still nullable.
4690 self.assertTrue(field.null_ok)
4691 # The database default is no longer set.
4692 if connection.features.can_introspect_default:
4693 self.assertIn(field.default, ["NULL", None])
4694
4695 def test_add_textfield_default_nullable(self):
4696 with connection.schema_editor() as editor:
4697 editor.create_model(Author)
4698 # Add new nullable TextField with a default.
4699 new_field = TextField(blank=True, null=True, default="text")
4700 new_field.set_attributes_from_name("description")
4701 with connection.schema_editor() as editor:
4702 editor.add_field(Author, new_field)
4703 Author.objects.create(name="Anonymous1")
4704 with connection.cursor() as cursor:
4705 cursor.execute("SELECT description FROM schema_author;")
4706 item = cursor.fetchall()[0]
4707 self.assertIsNone(item[0])
4708 field = next(
4709 f
4710 for f in connection.introspection.get_table_description(
4711 cursor,
4712 "schema_author",
4713 )
4714 if f.name == "description"
4715 )
4716 # Field is still nullable.
4717 self.assertTrue(field.null_ok)
4718 # The database default is no longer set.
4719 if connection.features.can_introspect_default:
4720 self.assertIn(field.default, ["NULL", None])
4721
4722 def test_alter_field_default_dropped(self):
4723 # Create the table
4724 with connection.schema_editor() as editor:
4725 editor.create_model(Author)
4726 # Create a row
4727 Author.objects.create(name="Anonymous1")
4728 self.assertIsNone(Author.objects.get().height)
4729 old_field = Author._meta.get_field("height")
4730 # The default from the new field is used in updating existing rows.
4731 new_field = IntegerField(blank=True, default=42)
4732 new_field.set_attributes_from_name("height")
4733 with connection.schema_editor() as editor:
4734 editor.alter_field(Author, old_field, new_field, strict=True)
4735 self.assertEqual(Author.objects.get().height, 42)
4736 # The database default should be removed.
4737 with connection.cursor() as cursor:
4738 field = next(
4739 f
4740 for f in connection.introspection.get_table_description(
4741 cursor, "schema_author"
4742 )
4743 if f.name == "height"
4744 )
4745 if connection.features.can_introspect_default:
4746 self.assertIsNone(field.default)
4747
4748 def test_alter_field_default_doesnt_perform_queries(self):
4749 """
4750 No queries are performed if a field default changes and the field's
4751 not changing from null to non-null.
4752 """
4753 with connection.schema_editor() as editor:
4754 editor.create_model(AuthorWithDefaultHeight)
4755 old_field = AuthorWithDefaultHeight._meta.get_field("height")
4756 new_default = old_field.default * 2
4757 new_field = PositiveIntegerField(null=True, blank=True, default=new_default)
4758 new_field.set_attributes_from_name("height")
4759 with connection.schema_editor() as editor, self.assertNumQueries(0):
4760 editor.alter_field(
4761 AuthorWithDefaultHeight, old_field, new_field, strict=True
4762 )
4763
4764 @skipUnlessDBFeature("supports_foreign_keys")
4765 def test_alter_field_fk_attributes_noop(self):
4766 """
4767 No queries are performed when changing field attributes that don't
4768 affect the schema.
4769 """
4770 with connection.schema_editor() as editor:
4771 editor.create_model(Author)
4772 editor.create_model(Book)
4773 old_field = Book._meta.get_field("author")
4774 new_field = ForeignKey(
4775 Author,
4776 blank=True,
4777 editable=False,
4778 error_messages={"invalid": "error message"},
4779 help_text="help text",
4780 limit_choices_to={"limit": "choice"},
4781 on_delete=PROTECT,
4782 related_name="related_name",
4783 related_query_name="related_query_name",
4784 validators=[lambda x: x],
4785 verbose_name="verbose name",
4786 )
4787 new_field.set_attributes_from_name("author")
4788 with connection.schema_editor() as editor, self.assertNumQueries(0):
4789 editor.alter_field(Book, old_field, new_field, strict=True)
4790 with connection.schema_editor() as editor, self.assertNumQueries(0):
4791 editor.alter_field(Book, new_field, old_field, strict=True)
4792
4793 def test_alter_field_choices_noop(self):
4794 with connection.schema_editor() as editor:
4795 editor.create_model(Author)
4796 old_field = Author._meta.get_field("name")
4797 new_field = CharField(
4798 choices=(("Jane", "Jane"), ("Joe", "Joe")),
4799 max_length=255,
4800 )
4801 new_field.set_attributes_from_name("name")
4802 with connection.schema_editor() as editor, self.assertNumQueries(0):
4803 editor.alter_field(Author, old_field, new_field, strict=True)
4804 with connection.schema_editor() as editor, self.assertNumQueries(0):
4805 editor.alter_field(Author, new_field, old_field, strict=True)
4806
4807 def test_add_textfield_unhashable_default(self):
4808 # Create the table
4809 with connection.schema_editor() as editor:
4810 editor.create_model(Author)
4811 # Create a row
4812 Author.objects.create(name="Anonymous1")
4813 # Create a field that has an unhashable default
4814 new_field = TextField(default={})
4815 new_field.set_attributes_from_name("info")
4816 with connection.schema_editor() as editor:
4817 editor.add_field(Author, new_field)
4818
4819 @unittest.skipUnless(connection.vendor == "postgresql", "PostgreSQL specific")
4820 def test_add_indexed_charfield(self):
4821 field = CharField(max_length=255, db_index=True)
4822 field.set_attributes_from_name("nom_de_plume")
4823 with connection.schema_editor() as editor:
4824 editor.create_model(Author)
4825 editor.add_field(Author, field)
4826 # Should create two indexes; one for like operator.
4827 self.assertEqual(
4828 self.get_constraints_for_column(Author, "nom_de_plume"),
4829 [
4830 "schema_author_nom_de_plume_7570a851",
4831 "schema_author_nom_de_plume_7570a851_like",
4832 ],
4833 )
4834
4835 @unittest.skipUnless(connection.vendor == "postgresql", "PostgreSQL specific")
4836 def test_add_unique_charfield(self):
4837 field = CharField(max_length=255, unique=True)
4838 field.set_attributes_from_name("nom_de_plume")
4839 with connection.schema_editor() as editor:
4840 editor.create_model(Author)
4841 editor.add_field(Author, field)
4842 # Should create two indexes; one for like operator.
4843 self.assertEqual(
4844 self.get_constraints_for_column(Author, "nom_de_plume"),
4845 [
4846 "schema_author_nom_de_plume_7570a851_like",
4847 "schema_author_nom_de_plume_key",
4848 ],
4849 )
4850
4851 @skipUnlessDBFeature("supports_comments")
4852 def test_add_db_comment_charfield(self):
4853 comment = "Custom comment"
4854 field = CharField(max_length=255, db_comment=comment)
4855 field.set_attributes_from_name("name_with_comment")
4856 with connection.schema_editor() as editor:
4857 editor.create_model(Author)
4858 editor.add_field(Author, field)
4859 self.assertEqual(
4860 self.get_column_comment(Author._meta.db_table, "name_with_comment"),
4861 comment,
4862 )
4863
4864 @skipUnlessDBFeature("supports_comments")
4865 def test_add_db_comment_and_default_charfield(self):
4866 comment = "Custom comment with default"
4867 field = CharField(max_length=255, default="Joe Doe", db_comment=comment)
4868 field.set_attributes_from_name("name_with_comment_default")
4869 with connection.schema_editor() as editor:
4870 editor.create_model(Author)
4871 Author.objects.create(name="Before adding a new field")
4872 editor.add_field(Author, field)
4873
4874 self.assertEqual(
4875 self.get_column_comment(Author._meta.db_table, "name_with_comment_default"),
4876 comment,
4877 )
4878 with connection.cursor() as cursor:
4879 cursor.execute(
4880 f"SELECT name_with_comment_default FROM {Author._meta.db_table};"
4881 )
4882 for row in cursor.fetchall():
4883 self.assertEqual(row[0], "Joe Doe")
4884
4885 @skipUnlessDBFeature("supports_comments")
4886 def test_alter_db_comment(self):
4887 with connection.schema_editor() as editor:
4888 editor.create_model(Author)
4889 # Add comment.
4890 old_field = Author._meta.get_field("name")
4891 new_field = CharField(max_length=255, db_comment="Custom comment")
4892 new_field.set_attributes_from_name("name")
4893 with connection.schema_editor() as editor:
4894 editor.alter_field(Author, old_field, new_field, strict=True)
4895 self.assertEqual(
4896 self.get_column_comment(Author._meta.db_table, "name"),
4897 "Custom comment",
4898 )
4899 # Alter comment.
4900 old_field = new_field
4901 new_field = CharField(max_length=255, db_comment="New custom comment")
4902 new_field.set_attributes_from_name("name")
4903 with connection.schema_editor() as editor:
4904 editor.alter_field(Author, old_field, new_field, strict=True)
4905 self.assertEqual(
4906 self.get_column_comment(Author._meta.db_table, "name"),
4907 "New custom comment",
4908 )
4909 # Remove comment.
4910 old_field = new_field
4911 new_field = CharField(max_length=255)
4912 new_field.set_attributes_from_name("name")
4913 with connection.schema_editor() as editor:
4914 editor.alter_field(Author, old_field, new_field, strict=True)
4915 self.assertIn(
4916 self.get_column_comment(Author._meta.db_table, "name"),
4917 [None, ""],
4918 )
4919
4920 @skipUnlessDBFeature("supports_comments", "supports_foreign_keys")
4921 def test_alter_db_comment_foreign_key(self):
4922 with connection.schema_editor() as editor:
4923 editor.create_model(Author)
4924 editor.create_model(Book)
4925
4926 comment = "FK custom comment"
4927 old_field = Book._meta.get_field("author")
4928 new_field = ForeignKey(Author, CASCADE, db_comment=comment)
4929 new_field.set_attributes_from_name("author")
4930 with connection.schema_editor() as editor:
4931 editor.alter_field(Book, old_field, new_field, strict=True)
4932 self.assertEqual(
4933 self.get_column_comment(Book._meta.db_table, "author_id"),
4934 comment,
4935 )
4936
4937 @skipUnlessDBFeature("supports_comments")
4938 def test_alter_field_type_preserve_comment(self):
4939 with connection.schema_editor() as editor:
4940 editor.create_model(Author)
4941
4942 comment = "This is the name."
4943 old_field = Author._meta.get_field("name")
4944 new_field = CharField(max_length=255, db_comment=comment)
4945 new_field.set_attributes_from_name("name")
4946 new_field.model = Author
4947 with connection.schema_editor() as editor:
4948 editor.alter_field(Author, old_field, new_field, strict=True)
4949 self.assertEqual(
4950 self.get_column_comment(Author._meta.db_table, "name"),
4951 comment,
4952 )
4953 # Changing a field type should preserve the comment.
4954 old_field = new_field
4955 new_field = CharField(max_length=511, db_comment=comment)
4956 new_field.set_attributes_from_name("name")
4957 new_field.model = Author
4958 with connection.schema_editor() as editor:
4959 editor.alter_field(Author, new_field, old_field, strict=True)
4960 # Comment is preserved.
4961 self.assertEqual(
4962 self.get_column_comment(Author._meta.db_table, "name"),
4963 comment,
4964 )
4965
4966 @isolate_apps("schema")
4967 @skipUnlessDBFeature("supports_comments")
4968 def test_db_comment_table(self):
4969 class ModelWithDbTableComment(Model):
4970 class Meta:
4971 app_label = "schema"
4972 db_table_comment = "Custom table comment"
4973
4974 with connection.schema_editor() as editor:
4975 editor.create_model(ModelWithDbTableComment)
4976 self.isolated_local_models = [ModelWithDbTableComment]
4977 self.assertEqual(
4978 self.get_table_comment(ModelWithDbTableComment._meta.db_table),
4979 "Custom table comment",
4980 )
4981 # Alter table comment.
4982 old_db_table_comment = ModelWithDbTableComment._meta.db_table_comment
4983 with connection.schema_editor() as editor:
4984 editor.alter_db_table_comment(
4985 ModelWithDbTableComment, old_db_table_comment, "New table comment"
4986 )
4987 self.assertEqual(
4988 self.get_table_comment(ModelWithDbTableComment._meta.db_table),
4989 "New table comment",
4990 )
4991 # Remove table comment.
4992 old_db_table_comment = ModelWithDbTableComment._meta.db_table_comment
4993 with connection.schema_editor() as editor:
4994 editor.alter_db_table_comment(
4995 ModelWithDbTableComment, old_db_table_comment, None
4996 )
4997 self.assertIn(
4998 self.get_table_comment(ModelWithDbTableComment._meta.db_table),
4999 [None, ""],
5000 )
5001
5002 @isolate_apps("schema")
5003 @skipIfDBFeature("supports_comments")
5004 def test_db_comment_table_unsupported(self):
5005 class ModelWithDbTableComment(Model):
5006 class Meta:
5007 app_label = "schema"
5008 db_table_comment = "Custom table comment"
5009
5010 # Table comments are ignored on databases that don't support them.
5011 with connection.schema_editor() as editor, self.assertNumQueries(1):
5012 editor.create_model(ModelWithDbTableComment)
5013 self.isolated_local_models = [ModelWithDbTableComment]
5014 with connection.schema_editor() as editor, self.assertNumQueries(0):
5015 editor.alter_db_table_comment(
5016 ModelWithDbTableComment, "Custom table comment", "New table comment"
5017 )
5018
5019 @isolate_apps("schema")
5020 @skipUnlessDBFeature("supports_comments", "supports_foreign_keys")
5021 def test_db_comments_from_abstract_model(self):
5022 class AbstractModelWithDbComments(Model):
5023 name = CharField(
5024 max_length=255, db_comment="Custom comment", null=True, blank=True
5025 )
5026
5027 class Meta:
5028 app_label = "schema"
5029 abstract = True
5030 db_table_comment = "Custom table comment"
5031
5032 class ModelWithDbComments(AbstractModelWithDbComments):
5033 pass
5034
5035 with connection.schema_editor() as editor:
5036 editor.create_model(ModelWithDbComments)
5037 self.isolated_local_models = [ModelWithDbComments]
5038
5039 self.assertEqual(
5040 self.get_column_comment(ModelWithDbComments._meta.db_table, "name"),
5041 "Custom comment",
5042 )
5043 self.assertEqual(
5044 self.get_table_comment(ModelWithDbComments._meta.db_table),
5045 "Custom table comment",
5046 )
5047
5048 @unittest.skipUnless(connection.vendor == "postgresql", "PostgreSQL specific")
5049 def test_alter_field_add_index_to_charfield(self):
5050 # Create the table and verify no initial indexes.
5051 with connection.schema_editor() as editor:
5052 editor.create_model(Author)
5053 self.assertEqual(self.get_constraints_for_column(Author, "name"), [])
5054 # Alter to add db_index=True and create 2 indexes.
5055 old_field = Author._meta.get_field("name")
5056 new_field = CharField(max_length=255, db_index=True)
5057 new_field.set_attributes_from_name("name")
5058 with connection.schema_editor() as editor:
5059 editor.alter_field(Author, old_field, new_field, strict=True)
5060 self.assertEqual(
5061 self.get_constraints_for_column(Author, "name"),
5062 ["schema_author_name_1fbc5617", "schema_author_name_1fbc5617_like"],
5063 )
5064 # Remove db_index=True to drop both indexes.
5065 with connection.schema_editor() as editor:
5066 editor.alter_field(Author, new_field, old_field, strict=True)
5067 self.assertEqual(self.get_constraints_for_column(Author, "name"), [])
5068
5069 @unittest.skipUnless(connection.vendor == "postgresql", "PostgreSQL specific")
5070 def test_alter_field_add_unique_to_charfield(self):
5071 # Create the table and verify no initial indexes.
5072 with connection.schema_editor() as editor:
5073 editor.create_model(Author)
5074 self.assertEqual(self.get_constraints_for_column(Author, "name"), [])
5075 # Alter to add unique=True and create 2 indexes.
5076 old_field = Author._meta.get_field("name")
5077 new_field = CharField(max_length=255, unique=True)
5078 new_field.set_attributes_from_name("name")
5079 with connection.schema_editor() as editor:
5080 editor.alter_field(Author, old_field, new_field, strict=True)
5081 self.assertEqual(
5082 self.get_constraints_for_column(Author, "name"),
5083 ["schema_author_name_1fbc5617_like", "schema_author_name_1fbc5617_uniq"],
5084 )
5085 # Remove unique=True to drop both indexes.
5086 with connection.schema_editor() as editor:
5087 editor.alter_field(Author, new_field, old_field, strict=True)
5088 self.assertEqual(self.get_constraints_for_column(Author, "name"), [])
5089
5090 @unittest.skipUnless(connection.vendor == "postgresql", "PostgreSQL specific")
5091 def test_alter_field_add_index_to_textfield(self):
5092 # Create the table and verify no initial indexes.
5093 with connection.schema_editor() as editor:
5094 editor.create_model(Note)
5095 self.assertEqual(self.get_constraints_for_column(Note, "info"), [])
5096 # Alter to add db_index=True and create 2 indexes.
5097 old_field = Note._meta.get_field("info")
5098 new_field = TextField(db_index=True)
5099 new_field.set_attributes_from_name("info")
5100 with connection.schema_editor() as editor:
5101 editor.alter_field(Note, old_field, new_field, strict=True)
5102 self.assertEqual(
5103 self.get_constraints_for_column(Note, "info"),
5104 ["schema_note_info_4b0ea695", "schema_note_info_4b0ea695_like"],
5105 )
5106 # Remove db_index=True to drop both indexes.
5107 with connection.schema_editor() as editor:
5108 editor.alter_field(Note, new_field, old_field, strict=True)
5109 self.assertEqual(self.get_constraints_for_column(Note, "info"), [])
5110
5111 @unittest.skipUnless(connection.vendor == "postgresql", "PostgreSQL specific")
5112 def test_alter_field_add_unique_to_charfield_with_db_index(self):
5113 # Create the table and verify initial indexes.
5114 with connection.schema_editor() as editor:
5115 editor.create_model(BookWithoutAuthor)
5116 self.assertEqual(
5117 self.get_constraints_for_column(BookWithoutAuthor, "title"),
5118 ["schema_book_title_2dfb2dff", "schema_book_title_2dfb2dff_like"],
5119 )
5120 # Alter to add unique=True (should replace the index)
5121 old_field = BookWithoutAuthor._meta.get_field("title")
5122 new_field = CharField(max_length=100, db_index=True, unique=True)
5123 new_field.set_attributes_from_name("title")
5124 with connection.schema_editor() as editor:
5125 editor.alter_field(BookWithoutAuthor, old_field, new_field, strict=True)
5126 self.assertEqual(
5127 self.get_constraints_for_column(BookWithoutAuthor, "title"),
5128 ["schema_book_title_2dfb2dff_like", "schema_book_title_2dfb2dff_uniq"],
5129 )
5130 # Alter to remove unique=True (should drop unique index)
5131 new_field2 = CharField(max_length=100, db_index=True)
5132 new_field2.set_attributes_from_name("title")
5133 with connection.schema_editor() as editor:
5134 editor.alter_field(BookWithoutAuthor, new_field, new_field2, strict=True)
5135 self.assertEqual(
5136 self.get_constraints_for_column(BookWithoutAuthor, "title"),
5137 ["schema_book_title_2dfb2dff", "schema_book_title_2dfb2dff_like"],
5138 )
5139
5140 @unittest.skipUnless(connection.vendor == "postgresql", "PostgreSQL specific")
5141 def test_alter_field_remove_unique_and_db_index_from_charfield(self):
5142 # Create the table and verify initial indexes.
5143 with connection.schema_editor() as editor:
5144 editor.create_model(BookWithoutAuthor)
5145 self.assertEqual(
5146 self.get_constraints_for_column(BookWithoutAuthor, "title"),
5147 ["schema_book_title_2dfb2dff", "schema_book_title_2dfb2dff_like"],
5148 )
5149 # Alter to add unique=True (should replace the index)
5150 old_field = BookWithoutAuthor._meta.get_field("title")
5151 new_field = CharField(max_length=100, db_index=True, unique=True)
5152 new_field.set_attributes_from_name("title")
5153 with connection.schema_editor() as editor:
5154 editor.alter_field(BookWithoutAuthor, old_field, new_field, strict=True)
5155 self.assertEqual(
5156 self.get_constraints_for_column(BookWithoutAuthor, "title"),
5157 ["schema_book_title_2dfb2dff_like", "schema_book_title_2dfb2dff_uniq"],
5158 )
5159 # Alter to remove both unique=True and db_index=True (should drop all indexes)
5160 new_field2 = CharField(max_length=100)
5161 new_field2.set_attributes_from_name("title")
5162 with connection.schema_editor() as editor:
5163 editor.alter_field(BookWithoutAuthor, new_field, new_field2, strict=True)
5164 self.assertEqual(
5165 self.get_constraints_for_column(BookWithoutAuthor, "title"), []
5166 )
5167
5168 @unittest.skipUnless(connection.vendor == "postgresql", "PostgreSQL specific")
5169 def test_alter_field_swap_unique_and_db_index_with_charfield(self):
5170 # Create the table and verify initial indexes.
5171 with connection.schema_editor() as editor:
5172 editor.create_model(BookWithoutAuthor)
5173 self.assertEqual(
5174 self.get_constraints_for_column(BookWithoutAuthor, "title"),
5175 ["schema_book_title_2dfb2dff", "schema_book_title_2dfb2dff_like"],
5176 )
5177 # Alter to set unique=True and remove db_index=True (should replace the index)
5178 old_field = BookWithoutAuthor._meta.get_field("title")
5179 new_field = CharField(max_length=100, unique=True)
5180 new_field.set_attributes_from_name("title")
5181 with connection.schema_editor() as editor:
5182 editor.alter_field(BookWithoutAuthor, old_field, new_field, strict=True)
5183 self.assertEqual(
5184 self.get_constraints_for_column(BookWithoutAuthor, "title"),
5185 ["schema_book_title_2dfb2dff_like", "schema_book_title_2dfb2dff_uniq"],
5186 )
5187 # Alter to set db_index=True and remove unique=True (should restore index)
5188 new_field2 = CharField(max_length=100, db_index=True)
5189 new_field2.set_attributes_from_name("title")
5190 with connection.schema_editor() as editor:
5191 editor.alter_field(BookWithoutAuthor, new_field, new_field2, strict=True)
5192 self.assertEqual(
5193 self.get_constraints_for_column(BookWithoutAuthor, "title"),
5194 ["schema_book_title_2dfb2dff", "schema_book_title_2dfb2dff_like"],
5195 )
5196
5197 @unittest.skipUnless(connection.vendor == "postgresql", "PostgreSQL specific")
5198 def test_alter_field_add_db_index_to_charfield_with_unique(self):
5199 # Create the table and verify initial indexes.
5200 with connection.schema_editor() as editor:
5201 editor.create_model(Tag)
5202 self.assertEqual(
5203 self.get_constraints_for_column(Tag, "slug"),
5204 ["schema_tag_slug_2c418ba3_like", "schema_tag_slug_key"],
5205 )
5206 # Alter to add db_index=True
5207 old_field = Tag._meta.get_field("slug")
5208 new_field = SlugField(db_index=True, unique=True)
5209 new_field.set_attributes_from_name("slug")
5210 with connection.schema_editor() as editor:
5211 editor.alter_field(Tag, old_field, new_field, strict=True)
5212 self.assertEqual(
5213 self.get_constraints_for_column(Tag, "slug"),
5214 ["schema_tag_slug_2c418ba3_like", "schema_tag_slug_key"],
5215 )
5216 # Alter to remove db_index=True
5217 new_field2 = SlugField(unique=True)
5218 new_field2.set_attributes_from_name("slug")
5219 with connection.schema_editor() as editor:
5220 editor.alter_field(Tag, new_field, new_field2, strict=True)
5221 self.assertEqual(
5222 self.get_constraints_for_column(Tag, "slug"),
5223 ["schema_tag_slug_2c418ba3_like", "schema_tag_slug_key"],
5224 )
5225
5226 @isolate_apps("schema")
5227 @unittest.skipUnless(connection.vendor == "postgresql", "PostgreSQL specific")
5228 def test_indexed_charfield_to_textfield(self):
5229 class SimpleModel(Model):
5230 field1 = CharField(max_length=10, db_index=True)
5231
5232 class Meta:
5233 app_label = "schema"
5234
5235 with connection.schema_editor() as editor:
5236 editor.create_model(SimpleModel)
5237 self.assertEqual(
5238 self.get_constraints_for_column(SimpleModel, "field1"),
5239 [
5240 "schema_simplemodel_field1_f07a3d6a",
5241 "schema_simplemodel_field1_f07a3d6a_like",
5242 ],
5243 )
5244 # Change to TextField.
5245 old_field1 = SimpleModel._meta.get_field("field1")
5246 new_field1 = TextField(db_index=True)
5247 new_field1.set_attributes_from_name("field1")
5248 with connection.schema_editor() as editor:
5249 editor.alter_field(SimpleModel, old_field1, new_field1, strict=True)
5250 self.assertEqual(
5251 self.get_constraints_for_column(SimpleModel, "field1"),
5252 [
5253 "schema_simplemodel_field1_f07a3d6a",
5254 "schema_simplemodel_field1_f07a3d6a_like",
5255 ],
5256 )
5257 # Change back to CharField.
5258 old_field1 = SimpleModel._meta.get_field("field1")
5259 new_field1 = CharField(max_length=10, db_index=True)
5260 new_field1.set_attributes_from_name("field1")
5261 with connection.schema_editor() as editor:
5262 editor.alter_field(SimpleModel, old_field1, new_field1, strict=True)
5263 self.assertEqual(
5264 self.get_constraints_for_column(SimpleModel, "field1"),
5265 [
5266 "schema_simplemodel_field1_f07a3d6a",
5267 "schema_simplemodel_field1_f07a3d6a_like",
5268 ],
5269 )
5270
5271 def test_alter_field_add_index_to_integerfield(self):
5272 # Create the table and verify no initial indexes.
5273 with connection.schema_editor() as editor:
5274 editor.create_model(Author)
5275 self.assertEqual(self.get_constraints_for_column(Author, "weight"), [])
5276
5277 # Alter to add db_index=True and create index.
5278 old_field = Author._meta.get_field("weight")
5279 new_field = IntegerField(null=True, db_index=True)
5280 new_field.set_attributes_from_name("weight")
5281 with connection.schema_editor() as editor:
5282 editor.alter_field(Author, old_field, new_field, strict=True)
5283 self.assertEqual(
5284 self.get_constraints_for_column(Author, "weight"),
5285 ["schema_author_weight_587740f9"],
5286 )
5287
5288 # Remove db_index=True to drop index.
5289 with connection.schema_editor() as editor:
5290 editor.alter_field(Author, new_field, old_field, strict=True)
5291 self.assertEqual(self.get_constraints_for_column(Author, "weight"), [])
5292
5293 def test_alter_pk_with_self_referential_field(self):
5294 """
5295 Changing the primary key field name of a model with a self-referential
5296 foreign key (#26384).
5297 """
5298 with connection.schema_editor() as editor:
5299 editor.create_model(Node)
5300 old_field = Node._meta.get_field("node_id")
5301 new_field = AutoField(primary_key=True)
5302 new_field.set_attributes_from_name("id")
5303 with connection.schema_editor() as editor:
5304 editor.alter_field(Node, old_field, new_field, strict=True)
5305 self.assertForeignKeyExists(Node, "parent_id", Node._meta.db_table)
5306
5307 @mock.patch("django.db.backends.base.schema.datetime")
5308 @mock.patch("django.db.backends.base.schema.timezone")
5309 def test_add_datefield_and_datetimefield_use_effective_default(
5310 self, mocked_datetime, mocked_tz
5311 ):
5312 """
5313 effective_default() should be used for DateField, DateTimeField, and
5314 TimeField if auto_now or auto_now_add is set (#25005).
5315 """
5316 now = datetime.datetime(month=1, day=1, year=2000, hour=1, minute=1)
5317 now_tz = datetime.datetime(
5318 month=1, day=1, year=2000, hour=1, minute=1, tzinfo=datetime.timezone.utc
5319 )
5320 mocked_datetime.now = mock.MagicMock(return_value=now)
5321 mocked_tz.now = mock.MagicMock(return_value=now_tz)
5322 # Create the table
5323 with connection.schema_editor() as editor:
5324 editor.create_model(Author)
5325 # Check auto_now/auto_now_add attributes are not defined
5326 columns = self.column_classes(Author)
5327 self.assertNotIn("dob_auto_now", columns)
5328 self.assertNotIn("dob_auto_now_add", columns)
5329 self.assertNotIn("dtob_auto_now", columns)
5330 self.assertNotIn("dtob_auto_now_add", columns)
5331 self.assertNotIn("tob_auto_now", columns)
5332 self.assertNotIn("tob_auto_now_add", columns)
5333 # Create a row
5334 Author.objects.create(name="Anonymous1")
5335 # Ensure fields were added with the correct defaults
5336 dob_auto_now = DateField(auto_now=True)
5337 dob_auto_now.set_attributes_from_name("dob_auto_now")
5338 self.check_added_field_default(
5339 editor,
5340 Author,
5341 dob_auto_now,
5342 "dob_auto_now",
5343 now.date(),
5344 cast_function=lambda x: x.date(),
5345 )
5346 dob_auto_now_add = DateField(auto_now_add=True)
5347 dob_auto_now_add.set_attributes_from_name("dob_auto_now_add")
5348 self.check_added_field_default(
5349 editor,
5350 Author,
5351 dob_auto_now_add,
5352 "dob_auto_now_add",
5353 now.date(),
5354 cast_function=lambda x: x.date(),
5355 )
5356 dtob_auto_now = DateTimeField(auto_now=True)
5357 dtob_auto_now.set_attributes_from_name("dtob_auto_now")
5358 self.check_added_field_default(
5359 editor,
5360 Author,
5361 dtob_auto_now,
5362 "dtob_auto_now",
5363 now,
5364 )
5365 dt_tm_of_birth_auto_now_add = DateTimeField(auto_now_add=True)
5366 dt_tm_of_birth_auto_now_add.set_attributes_from_name("dtob_auto_now_add")
5367 self.check_added_field_default(
5368 editor,
5369 Author,
5370 dt_tm_of_birth_auto_now_add,
5371 "dtob_auto_now_add",
5372 now,
5373 )
5374 tob_auto_now = TimeField(auto_now=True)
5375 tob_auto_now.set_attributes_from_name("tob_auto_now")
5376 self.check_added_field_default(
5377 editor,
5378 Author,
5379 tob_auto_now,
5380 "tob_auto_now",
5381 now.time(),
5382 cast_function=lambda x: x.time(),
5383 )
5384 tob_auto_now_add = TimeField(auto_now_add=True)
5385 tob_auto_now_add.set_attributes_from_name("tob_auto_now_add")
5386 self.check_added_field_default(
5387 editor,
5388 Author,
5389 tob_auto_now_add,
5390 "tob_auto_now_add",
5391 now.time(),
5392 cast_function=lambda x: x.time(),
5393 )
5394
5395 def test_namespaced_db_table_create_index_name(self):
5396 """
5397 Table names are stripped of their namespace/schema before being used to
5398 generate index names.
5399 """
5400 with connection.schema_editor() as editor:
5401 max_name_length = connection.ops.max_name_length() or 200
5402 namespace = "n" * max_name_length
5403 table_name = "t" * max_name_length
5404 namespaced_table_name = '"%s"."%s"' % (namespace, table_name)
5405 self.assertEqual(
5406 editor._create_index_name(table_name, []),
5407 editor._create_index_name(namespaced_table_name, []),
5408 )
5409
5410 @unittest.skipUnless(
5411 connection.vendor == "oracle", "Oracle specific db_table syntax"
5412 )
5413 def test_creation_with_db_table_double_quotes(self):
5414 oracle_user = connection.creation._test_database_user()
5415
5416 class Student(Model):
5417 name = CharField(max_length=30)
5418
5419 class Meta:
5420 app_label = "schema"
5421 apps = new_apps
5422 db_table = '"%s"."DJANGO_STUDENT_TABLE"' % oracle_user
5423
5424 class Document(Model):
5425 name = CharField(max_length=30)
5426 students = ManyToManyField(Student)
5427
5428 class Meta:
5429 app_label = "schema"
5430 apps = new_apps
5431 db_table = '"%s"."DJANGO_DOCUMENT_TABLE"' % oracle_user
5432
5433 self.isolated_local_models = [Student, Document]
5434
5435 with connection.schema_editor() as editor:
5436 editor.create_model(Student)
5437 editor.create_model(Document)
5438
5439 doc = Document.objects.create(name="Test Name")
5440 student = Student.objects.create(name="Some man")
5441 doc.students.add(student)
5442
5443 @isolate_apps("schema")
5444 @unittest.skipUnless(
5445 connection.vendor == "postgresql", "PostgreSQL specific db_table syntax."
5446 )
5447 def test_namespaced_db_table_foreign_key_reference(self):
5448 with connection.cursor() as cursor:
5449 cursor.execute("CREATE SCHEMA django_schema_tests")
5450
5451 def delete_schema():
5452 with connection.cursor() as cursor:
5453 cursor.execute("DROP SCHEMA django_schema_tests CASCADE")
5454
5455 self.addCleanup(delete_schema)
5456
5457 class Author(Model):
5458 class Meta:
5459 app_label = "schema"
5460
5461 class Book(Model):
5462 class Meta:
5463 app_label = "schema"
5464 db_table = '"django_schema_tests"."schema_book"'
5465
5466 author = ForeignKey(Author, CASCADE)
5467 author.set_attributes_from_name("author")
5468
5469 with connection.schema_editor() as editor:
5470 editor.create_model(Author)
5471 editor.create_model(Book)
5472 editor.add_field(Book, author)
5473
5474 def test_rename_table_renames_deferred_sql_references(self):
5475 with connection.schema_editor() as editor:
5476 editor.create_model(Author)
5477 editor.create_model(Book)
5478 editor.alter_db_table(Author, "schema_author", "schema_renamed_author")
5479 editor.alter_db_table(Author, "schema_book", "schema_renamed_book")
5480 try:
5481 self.assertGreater(len(editor.deferred_sql), 0)
5482 for statement in editor.deferred_sql:
5483 self.assertIs(statement.references_table("schema_author"), False)
5484 self.assertIs(statement.references_table("schema_book"), False)
5485 finally:
5486 editor.alter_db_table(Author, "schema_renamed_author", "schema_author")
5487 editor.alter_db_table(Author, "schema_renamed_book", "schema_book")
5488
5489 def test_rename_column_renames_deferred_sql_references(self):
5490 with connection.schema_editor() as editor:
5491 editor.create_model(Author)
5492 editor.create_model(Book)
5493 old_title = Book._meta.get_field("title")
5494 new_title = CharField(max_length=100, db_index=True)
5495 new_title.set_attributes_from_name("renamed_title")
5496 editor.alter_field(Book, old_title, new_title)
5497 old_author = Book._meta.get_field("author")
5498 new_author = ForeignKey(Author, CASCADE)
5499 new_author.set_attributes_from_name("renamed_author")
5500 editor.alter_field(Book, old_author, new_author)
5501 self.assertGreater(len(editor.deferred_sql), 0)
5502 for statement in editor.deferred_sql:
5503 self.assertIs(statement.references_column("book", "title"), False)
5504 self.assertIs(statement.references_column("book", "author_id"), False)
5505
5506 @isolate_apps("schema")
5507 def test_referenced_field_without_constraint_rename_inside_atomic_block(self):
5508 """
5509 Foreign keys without database level constraint don't prevent the field
5510 they reference from being renamed in an atomic block.
5511 """
5512
5513 class Foo(Model):
5514 field = CharField(max_length=255, unique=True)
5515
5516 class Meta:
5517 app_label = "schema"
5518
5519 class Bar(Model):
5520 foo = ForeignKey(Foo, CASCADE, to_field="field", db_constraint=False)
5521
5522 class Meta:
5523 app_label = "schema"
5524
5525 self.isolated_local_models = [Foo, Bar]
5526 with connection.schema_editor() as editor:
5527 editor.create_model(Foo)
5528 editor.create_model(Bar)
5529
5530 new_field = CharField(max_length=255, unique=True)
5531 new_field.set_attributes_from_name("renamed")
5532 with connection.schema_editor(atomic=True) as editor:
5533 editor.alter_field(Foo, Foo._meta.get_field("field"), new_field)
5534
5535 @isolate_apps("schema")
5536 def test_referenced_table_without_constraint_rename_inside_atomic_block(self):
5537 """
5538 Foreign keys without database level constraint don't prevent the table
5539 they reference from being renamed in an atomic block.
5540 """
5541
5542 class Foo(Model):
5543 field = CharField(max_length=255, unique=True)
5544
5545 class Meta:
5546 app_label = "schema"
5547
5548 class Bar(Model):
5549 foo = ForeignKey(Foo, CASCADE, to_field="field", db_constraint=False)
5550
5551 class Meta:
5552 app_label = "schema"
5553
5554 self.isolated_local_models = [Foo, Bar]
5555 with connection.schema_editor() as editor:
5556 editor.create_model(Foo)
5557 editor.create_model(Bar)
5558
5559 new_field = CharField(max_length=255, unique=True)
5560 new_field.set_attributes_from_name("renamed")
5561 with connection.schema_editor(atomic=True) as editor:
5562 editor.alter_db_table(Foo, Foo._meta.db_table, "renamed_table")
5563 Foo._meta.db_table = "renamed_table"
5564
5565 @isolate_apps("schema")
5566 @skipUnlessDBFeature("supports_collation_on_charfield")
5567 def test_db_collation_charfield(self):
5568 collation = connection.features.test_collations.get("non_default")
5569 if not collation:
5570 self.skipTest("Language collations are not supported.")
5571
5572 class Foo(Model):
5573 field = CharField(max_length=255, db_collation=collation)
5574
5575 class Meta:
5576 app_label = "schema"
5577
5578 self.isolated_local_models = [Foo]
5579 with connection.schema_editor() as editor:
5580 editor.create_model(Foo)
5581
5582 self.assertEqual(
5583 self.get_column_collation(Foo._meta.db_table, "field"),
5584 collation,
5585 )
5586
5587 @isolate_apps("schema")
5588 @skipUnlessDBFeature("supports_collation_on_textfield")
5589 def test_db_collation_textfield(self):
5590 collation = connection.features.test_collations.get("non_default")
5591 if not collation:
5592 self.skipTest("Language collations are not supported.")
5593
5594 class Foo(Model):
5595 field = TextField(db_collation=collation)
5596
5597 class Meta:
5598 app_label = "schema"
5599
5600 self.isolated_local_models = [Foo]
5601 with connection.schema_editor() as editor:
5602 editor.create_model(Foo)
5603
5604 self.assertEqual(
5605 self.get_column_collation(Foo._meta.db_table, "field"),
5606 collation,
5607 )
5608
5609 @skipUnlessDBFeature("supports_collation_on_charfield")
5610 def test_add_field_db_collation(self):
5611 collation = connection.features.test_collations.get("non_default")
5612 if not collation:
5613 self.skipTest("Language collations are not supported.")
5614
5615 with connection.schema_editor() as editor:
5616 editor.create_model(Author)
5617
5618 new_field = CharField(max_length=255, db_collation=collation)
5619 new_field.set_attributes_from_name("alias")
5620 with connection.schema_editor() as editor:
5621 editor.add_field(Author, new_field)
5622 columns = self.column_classes(Author)
5623 self.assertEqual(
5624 columns["alias"][0],
5625 connection.features.introspected_field_types["CharField"],
5626 )
5627 self.assertEqual(columns["alias"][1][8], collation)
5628
5629 @skipUnlessDBFeature("supports_collation_on_charfield")
5630 def test_alter_field_db_collation(self):
5631 collation = connection.features.test_collations.get("non_default")
5632 if not collation:
5633 self.skipTest("Language collations are not supported.")
5634
5635 with connection.schema_editor() as editor:
5636 editor.create_model(Author)
5637
5638 old_field = Author._meta.get_field("name")
5639 new_field = CharField(max_length=255, db_collation=collation)
5640 new_field.set_attributes_from_name("name")
5641 new_field.model = Author
5642 with connection.schema_editor() as editor:
5643 editor.alter_field(Author, old_field, new_field, strict=True)
5644 self.assertEqual(
5645 self.get_column_collation(Author._meta.db_table, "name"),
5646 collation,
5647 )
5648 with connection.schema_editor() as editor:
5649 editor.alter_field(Author, new_field, old_field, strict=True)
5650 self.assertIsNone(self.get_column_collation(Author._meta.db_table, "name"))
5651
5652 @skipUnlessDBFeature("supports_collation_on_charfield")
5653 def test_alter_field_type_preserve_db_collation(self):
5654 collation = connection.features.test_collations.get("non_default")
5655 if not collation:
5656 self.skipTest("Language collations are not supported.")
5657
5658 with connection.schema_editor() as editor:
5659 editor.create_model(Author)
5660
5661 old_field = Author._meta.get_field("name")
5662 new_field = CharField(max_length=255, db_collation=collation)
5663 new_field.set_attributes_from_name("name")
5664 new_field.model = Author
5665 with connection.schema_editor() as editor:
5666 editor.alter_field(Author, old_field, new_field, strict=True)
5667 self.assertEqual(
5668 self.get_column_collation(Author._meta.db_table, "name"),
5669 collation,
5670 )
5671 # Changing a field type should preserve the collation.
5672 old_field = new_field
5673 new_field = CharField(max_length=511, db_collation=collation)
5674 new_field.set_attributes_from_name("name")
5675 new_field.model = Author
5676 with connection.schema_editor() as editor:
5677 editor.alter_field(Author, new_field, old_field, strict=True)
5678 # Collation is preserved.
5679 self.assertEqual(
5680 self.get_column_collation(Author._meta.db_table, "name"),
5681 collation,
5682 )
5683
5684 @skipUnlessDBFeature("supports_collation_on_charfield")
5685 def test_alter_primary_key_db_collation(self):
5686 collation = connection.features.test_collations.get("non_default")
5687 if not collation:
5688 self.skipTest("Language collations are not supported.")
5689
5690 with connection.schema_editor() as editor:
5691 editor.create_model(Thing)
5692
5693 old_field = Thing._meta.get_field("when")
5694 new_field = CharField(max_length=1, db_collation=collation, primary_key=True)
5695 new_field.set_attributes_from_name("when")
5696 new_field.model = Thing
5697 with connection.schema_editor() as editor:
5698 editor.alter_field(Thing, old_field, new_field, strict=True)
5699 self.assertEqual(self.get_primary_key(Thing._meta.db_table), "when")
5700 self.assertEqual(
5701 self.get_column_collation(Thing._meta.db_table, "when"),
5702 collation,
5703 )
5704 with connection.schema_editor() as editor:
5705 editor.alter_field(Thing, new_field, old_field, strict=True)
5706 self.assertEqual(self.get_primary_key(Thing._meta.db_table), "when")
5707 self.assertIsNone(self.get_column_collation(Thing._meta.db_table, "when"))
5708
5709 @skipUnlessDBFeature(
5710 "supports_collation_on_charfield", "supports_collation_on_textfield"
5711 )
5712 def test_alter_field_type_and_db_collation(self):
5713 collation = connection.features.test_collations.get("non_default")
5714 if not collation:
5715 self.skipTest("Language collations are not supported.")
5716
5717 with connection.schema_editor() as editor:
5718 editor.create_model(Note)
5719
5720 old_field = Note._meta.get_field("info")
5721 new_field = CharField(max_length=255, db_collation=collation)
5722 new_field.set_attributes_from_name("info")
5723 new_field.model = Note
5724 with connection.schema_editor() as editor:
5725 editor.alter_field(Note, old_field, new_field, strict=True)
5726 columns = self.column_classes(Note)
5727 self.assertEqual(
5728 columns["info"][0],
5729 connection.features.introspected_field_types["CharField"],
5730 )
5731 self.assertEqual(columns["info"][1][8], collation)
5732 with connection.schema_editor() as editor:
5733 editor.alter_field(Note, new_field, old_field, strict=True)
5734 columns = self.column_classes(Note)
5735 self.assertEqual(columns["info"][0], "TextField")
5736 self.assertIsNone(columns["info"][1][8])
5737
5738 @skipUnlessDBFeature(
5739 "supports_collation_on_charfield",
5740 "supports_non_deterministic_collations",
5741 )
5742 def test_ci_cs_db_collation(self):
5743 cs_collation = connection.features.test_collations.get("cs")
5744 ci_collation = connection.features.test_collations.get("ci")
5745 try:
5746 if connection.vendor == "mysql":
5747 cs_collation = "latin1_general_cs"
5748 elif connection.vendor == "postgresql":
5749 cs_collation = "en-x-icu"
5750 with connection.cursor() as cursor:
5751 cursor.execute(
5752 "CREATE COLLATION IF NOT EXISTS case_insensitive "
5753 "(provider = icu, locale = 'und-u-ks-level2', "
5754 "deterministic = false)"
5755 )
5756 ci_collation = "case_insensitive"
5757 # Create the table.
5758 with connection.schema_editor() as editor:
5759 editor.create_model(Author)
5760 # Case-insensitive collation.
5761 old_field = Author._meta.get_field("name")
5762 new_field_ci = CharField(max_length=255, db_collation=ci_collation)
5763 new_field_ci.set_attributes_from_name("name")
5764 new_field_ci.model = Author
5765 with connection.schema_editor() as editor:
5766 editor.alter_field(Author, old_field, new_field_ci, strict=True)
5767 Author.objects.create(name="ANDREW")
5768 self.assertIs(Author.objects.filter(name="Andrew").exists(), True)
5769 # Case-sensitive collation.
5770 new_field_cs = CharField(max_length=255, db_collation=cs_collation)
5771 new_field_cs.set_attributes_from_name("name")
5772 new_field_cs.model = Author
5773 with connection.schema_editor() as editor:
5774 editor.alter_field(Author, new_field_ci, new_field_cs, strict=True)
5775 self.assertIs(Author.objects.filter(name="Andrew").exists(), False)
5776 finally:
5777 if connection.vendor == "postgresql":
5778 with connection.cursor() as cursor:
5779 cursor.execute("DROP COLLATION IF EXISTS case_insensitive")