2012-06-19 00:32:03 +08:00
|
|
|
import datetime
|
2015-01-29 22:14:55 +08:00
|
|
|
import itertools
|
2013-09-03 21:55:21 +08:00
|
|
|
import unittest
|
2015-02-18 06:50:45 +08:00
|
|
|
from copy import copy
|
2017-01-20 01:16:04 +08:00
|
|
|
from unittest import mock
|
2013-09-03 21:55:21 +08:00
|
|
|
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.db import (
|
|
|
|
DatabaseError, IntegrityError, OperationalError, connection,
|
|
|
|
)
|
2015-01-29 22:14:55 +08:00
|
|
|
from django.db.models import Model
|
2016-06-23 12:46:11 +08:00
|
|
|
from django.db.models.deletion import CASCADE, PROTECT
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.db.models.fields import (
|
2017-06-01 17:49:57 +08:00
|
|
|
AutoField, BigAutoField, BigIntegerField, BinaryField, BooleanField,
|
|
|
|
CharField, DateField, DateTimeField, IntegerField, PositiveIntegerField,
|
|
|
|
SlugField, TextField, TimeField,
|
2015-01-28 20:35:27 +08:00
|
|
|
)
|
|
|
|
from django.db.models.fields.related import (
|
2015-07-16 02:38:10 +08:00
|
|
|
ForeignKey, ForeignObject, ManyToManyField, OneToOneField,
|
2015-01-28 20:35:27 +08:00
|
|
|
)
|
2016-06-26 00:32:56 +08:00
|
|
|
from django.db.models.indexes import Index
|
2016-12-24 11:34:52 +08:00
|
|
|
from django.db.transaction import TransactionManagementError, atomic
|
2015-08-08 23:27:06 +08:00
|
|
|
from django.test import (
|
2017-01-20 01:16:04 +08:00
|
|
|
TransactionTestCase, skipIfDBFeature, skipUnlessDBFeature,
|
2015-08-08 23:27:06 +08:00
|
|
|
)
|
2016-09-08 12:57:04 +08:00
|
|
|
from django.test.utils import CaptureQueriesContext
|
2016-10-08 09:06:49 +08:00
|
|
|
from django.utils import timezone
|
2015-01-29 22:14:55 +08:00
|
|
|
|
2015-05-24 18:16:21 +08:00
|
|
|
from .fields import (
|
|
|
|
CustomManyToManyField, InheritedManyToManyField, MediumBlobField,
|
|
|
|
)
|
2015-01-29 22:14:55 +08:00
|
|
|
from .models import (
|
2017-05-24 13:25:59 +08:00
|
|
|
Author, AuthorCharFieldWithIndex, AuthorTextFieldWithIndex,
|
|
|
|
AuthorWithDefaultHeight, AuthorWithEvenLongerName, AuthorWithIndexedName,
|
|
|
|
Book, BookForeignObj, BookWeak, BookWithLongName, BookWithO2O,
|
|
|
|
BookWithoutAuthor, BookWithSlug, IntegerPK, Node, Note, NoteRename, Tag,
|
|
|
|
TagIndexed, TagM2MTest, TagUniqueRename, Thing, UniqueTest, new_apps,
|
2015-01-29 22:14:55 +08:00
|
|
|
)
|
2012-06-19 00:32:03 +08:00
|
|
|
|
|
|
|
|
2013-04-19 00:16:39 +08:00
|
|
|
class SchemaTests(TransactionTestCase):
|
2012-06-19 00:32:03 +08:00
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
Tests for the schema-alteration code.
|
2012-06-19 00:32:03 +08:00
|
|
|
|
|
|
|
Be aware that these tests are more liable than most to false results,
|
|
|
|
as sometimes the code to check if a test has worked is almost as complex
|
|
|
|
as the code it is testing.
|
|
|
|
"""
|
2013-09-03 21:55:21 +08:00
|
|
|
|
2013-06-19 22:36:22 +08:00
|
|
|
available_apps = []
|
2012-06-19 00:32:03 +08:00
|
|
|
|
2013-11-23 06:31:50 +08:00
|
|
|
models = [
|
2017-05-24 13:25:59 +08:00
|
|
|
Author, AuthorCharFieldWithIndex, AuthorTextFieldWithIndex,
|
|
|
|
AuthorWithDefaultHeight, AuthorWithEvenLongerName, Book, BookWeak,
|
|
|
|
BookWithLongName, BookWithO2O, BookWithSlug, IntegerPK, Node, Note,
|
|
|
|
Tag, TagIndexed, TagM2MTest, TagUniqueRename, Thing, UniqueTest,
|
2013-11-23 06:31:50 +08:00
|
|
|
]
|
2012-06-19 00:32:03 +08:00
|
|
|
|
2012-06-19 20:25:22 +08:00
|
|
|
# Utility functions
|
|
|
|
|
2015-01-29 22:14:55 +08:00
|
|
|
def setUp(self):
|
|
|
|
# local_models should contain test dependent model classes that will be
|
|
|
|
# automatically removed from the app cache on test tear down.
|
|
|
|
self.local_models = []
|
|
|
|
|
2012-06-19 00:32:03 +08:00
|
|
|
def tearDown(self):
|
|
|
|
# Delete any tables made for our models
|
2012-09-18 02:57:23 +08:00
|
|
|
self.delete_tables()
|
2015-01-29 22:14:55 +08:00
|
|
|
new_apps.clear_cache()
|
|
|
|
for model in new_apps.get_models():
|
|
|
|
model._meta._expire_cache()
|
|
|
|
if 'schema' in new_apps.all_models:
|
|
|
|
for model in self.local_models:
|
2015-11-14 06:13:27 +08:00
|
|
|
for many_to_many in model._meta.many_to_many:
|
|
|
|
through = many_to_many.remote_field.through
|
|
|
|
if through and through._meta.auto_created:
|
|
|
|
del new_apps.all_models['schema'][through._meta.model_name]
|
2015-01-29 22:14:55 +08:00
|
|
|
del new_apps.all_models['schema'][model._meta.model_name]
|
2012-09-18 02:57:23 +08:00
|
|
|
|
|
|
|
def delete_tables(self):
|
|
|
|
"Deletes all model tables for our models for a clean test environment"
|
2015-06-03 07:17:17 +08:00
|
|
|
converter = connection.introspection.table_name_converter
|
2016-12-24 11:34:52 +08:00
|
|
|
with connection.schema_editor() as editor:
|
2014-01-09 23:05:15 +08:00
|
|
|
connection.disable_constraint_checking()
|
2015-10-16 03:36:28 +08:00
|
|
|
table_names = connection.introspection.table_names()
|
2015-01-29 22:14:55 +08:00
|
|
|
for model in itertools.chain(SchemaTests.models, self.local_models):
|
2015-10-16 03:36:28 +08:00
|
|
|
tbl = converter(model._meta.db_table)
|
|
|
|
if tbl in table_names:
|
2016-12-24 11:34:52 +08:00
|
|
|
editor.delete_model(model)
|
2015-10-16 03:36:28 +08:00
|
|
|
table_names.remove(tbl)
|
|
|
|
connection.enable_constraint_checking()
|
2012-06-19 00:32:03 +08:00
|
|
|
|
2012-06-19 20:25:22 +08:00
|
|
|
def column_classes(self, model):
|
2014-01-09 23:05:15 +08:00
|
|
|
with connection.cursor() as cursor:
|
2014-12-07 05:00:09 +08:00
|
|
|
columns = {
|
|
|
|
d[0]: (connection.introspection.get_field_type(d[1], d), d)
|
2014-01-09 23:05:15 +08:00
|
|
|
for d in connection.introspection.get_table_description(
|
|
|
|
cursor,
|
|
|
|
model._meta.db_table,
|
|
|
|
)
|
2014-12-07 05:00:09 +08:00
|
|
|
}
|
2012-09-08 00:51:11 +08:00
|
|
|
# SQLite has a different format for field_type
|
|
|
|
for name, (type, desc) in columns.items():
|
|
|
|
if isinstance(type, tuple):
|
|
|
|
columns[name] = (type[0], desc)
|
|
|
|
# SQLite also doesn't error properly
|
|
|
|
if not columns:
|
|
|
|
raise DatabaseError("Table does not exist (empty pragma)")
|
|
|
|
return columns
|
2012-06-19 20:25:22 +08:00
|
|
|
|
2016-08-20 18:14:02 +08:00
|
|
|
def get_primary_key(self, table):
|
|
|
|
with connection.cursor() as cursor:
|
|
|
|
return connection.introspection.get_primary_key_column(cursor, table)
|
|
|
|
|
2014-01-09 23:05:15 +08:00
|
|
|
def get_indexes(self, table):
|
|
|
|
"""
|
|
|
|
Get the indexes on the table using a new cursor.
|
|
|
|
"""
|
|
|
|
with connection.cursor() as cursor:
|
2016-08-20 18:14:02 +08:00
|
|
|
return [
|
|
|
|
c['columns'][0]
|
|
|
|
for c in connection.introspection.get_constraints(cursor, table).values()
|
|
|
|
if c['index'] and len(c['columns']) == 1
|
|
|
|
]
|
2014-01-09 23:05:15 +08:00
|
|
|
|
|
|
|
def get_constraints(self, table):
|
|
|
|
"""
|
|
|
|
Get the constraints on a table using a new cursor.
|
|
|
|
"""
|
|
|
|
with connection.cursor() as cursor:
|
|
|
|
return connection.introspection.get_constraints(cursor, table)
|
|
|
|
|
2016-01-08 08:42:58 +08:00
|
|
|
def get_constraints_for_column(self, model, column_name):
|
|
|
|
constraints = self.get_constraints(model._meta.db_table)
|
|
|
|
constraints_for_column = []
|
|
|
|
for name, details in constraints.items():
|
|
|
|
if details['columns'] == [column_name]:
|
|
|
|
constraints_for_column.append(name)
|
|
|
|
return sorted(constraints_for_column)
|
|
|
|
|
2016-04-02 21:49:32 +08:00
|
|
|
def check_added_field_default(self, schema_editor, model, field, field_name, expected_default,
|
|
|
|
cast_function=None):
|
|
|
|
with connection.cursor() as cursor:
|
|
|
|
schema_editor.add_field(model, field)
|
|
|
|
cursor.execute("SELECT {} FROM {};".format(field_name, model._meta.db_table))
|
|
|
|
database_default = cursor.fetchall()[0][0]
|
|
|
|
if cast_function and not type(database_default) == type(expected_default):
|
|
|
|
database_default = cast_function(database_default)
|
|
|
|
self.assertEqual(database_default, expected_default)
|
2012-06-19 20:25:22 +08:00
|
|
|
|
2016-06-23 12:46:11 +08:00
|
|
|
def get_constraints_count(self, table, column, fk_to):
|
|
|
|
"""
|
|
|
|
Return a dict with keys 'fks', 'uniques, and 'indexes' indicating the
|
|
|
|
number of foreign keys, unique constraints, and indexes on
|
|
|
|
`table`.`column`. The `fk_to` argument is a 2-tuple specifying the
|
|
|
|
expected foreign key relationship's (table, column).
|
|
|
|
"""
|
|
|
|
with connection.cursor() as cursor:
|
|
|
|
constraints = connection.introspection.get_constraints(cursor, table)
|
|
|
|
counts = {'fks': 0, 'uniques': 0, 'indexes': 0}
|
|
|
|
for c in constraints.values():
|
|
|
|
if c['columns'] == [column]:
|
|
|
|
if c['foreign_key'] == fk_to:
|
|
|
|
counts['fks'] += 1
|
|
|
|
if c['unique']:
|
|
|
|
counts['uniques'] += 1
|
|
|
|
elif c['index']:
|
|
|
|
counts['indexes'] += 1
|
|
|
|
return counts
|
|
|
|
|
2016-07-22 20:52:44 +08:00
|
|
|
def assertIndexOrder(self, table, index, order):
|
|
|
|
constraints = self.get_constraints(table)
|
|
|
|
self.assertIn(index, constraints)
|
|
|
|
index_orders = constraints[index]['orders']
|
|
|
|
self.assertTrue(all([(val == expected) for val, expected in zip(index_orders, order)]))
|
|
|
|
|
2017-05-24 22:43:56 +08:00
|
|
|
def assertForeignKeyExists(self, model, column, expected_fk_table):
|
|
|
|
"""
|
|
|
|
Fail if the FK constraint on `model.Meta.db_table`.`column` to
|
|
|
|
`expected_fk_table`.id doesn't exist.
|
|
|
|
"""
|
|
|
|
constraints = self.get_constraints(model._meta.db_table)
|
|
|
|
constraint_fk = None
|
|
|
|
for name, details in constraints.items():
|
|
|
|
if details['columns'] == [column] and details['foreign_key']:
|
|
|
|
constraint_fk = details['foreign_key']
|
|
|
|
break
|
|
|
|
self.assertEqual(constraint_fk, (expected_fk_table, 'id'))
|
|
|
|
|
|
|
|
def assertForeignKeyNotExists(self, model, column, expected_fk_table):
|
|
|
|
with self.assertRaises(AssertionError):
|
|
|
|
self.assertForeignKeyExists(model, column, expected_fk_table)
|
|
|
|
|
2016-04-02 21:49:32 +08:00
|
|
|
# Tests
|
2012-06-19 00:32:03 +08:00
|
|
|
def test_creation_deletion(self):
|
|
|
|
"""
|
|
|
|
Tries creating a model's table, and then deleting it.
|
|
|
|
"""
|
|
|
|
# Create the table
|
2013-05-18 17:48:46 +08:00
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.create_model(Author)
|
2016-10-27 15:53:39 +08:00
|
|
|
# The table is there
|
2012-09-18 17:59:03 +08:00
|
|
|
list(Author.objects.all())
|
2012-06-19 00:32:03 +08:00
|
|
|
# Clean up that table
|
2013-05-18 17:48:46 +08:00
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.delete_model(Author)
|
2016-10-27 15:53:39 +08:00
|
|
|
# The table is gone
|
2016-01-17 19:26:39 +08:00
|
|
|
with self.assertRaises(DatabaseError):
|
|
|
|
list(Author.objects.all())
|
2012-06-19 00:32:03 +08:00
|
|
|
|
2015-08-08 23:27:06 +08:00
|
|
|
@skipUnlessDBFeature('supports_foreign_keys')
|
2012-09-08 01:31:05 +08:00
|
|
|
def test_fk(self):
|
2016-10-27 15:53:39 +08:00
|
|
|
"Creating tables out of FK order, then repointing, works"
|
2012-06-19 00:32:03 +08:00
|
|
|
# Create the table
|
2013-05-18 17:48:46 +08:00
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.create_model(Book)
|
|
|
|
editor.create_model(Author)
|
|
|
|
editor.create_model(Tag)
|
2016-10-27 15:53:39 +08:00
|
|
|
# Initial tables are there
|
2012-09-18 17:59:03 +08:00
|
|
|
list(Author.objects.all())
|
|
|
|
list(Book.objects.all())
|
2012-06-19 00:32:03 +08:00
|
|
|
# Make sure the FK constraint is present
|
|
|
|
with self.assertRaises(IntegrityError):
|
|
|
|
Book.objects.create(
|
2013-11-04 02:17:58 +08:00
|
|
|
author_id=1,
|
|
|
|
title="Much Ado About Foreign Keys",
|
|
|
|
pub_date=datetime.datetime.now(),
|
2012-06-19 00:32:03 +08:00
|
|
|
)
|
2012-09-08 01:31:05 +08:00
|
|
|
# Repoint the FK constraint
|
2015-01-29 22:14:55 +08:00
|
|
|
old_field = Book._meta.get_field("author")
|
2015-07-22 22:43:21 +08:00
|
|
|
new_field = ForeignKey(Tag, CASCADE)
|
2012-09-08 01:31:05 +08:00
|
|
|
new_field.set_attributes_from_name("author")
|
2013-05-18 17:48:46 +08:00
|
|
|
with connection.schema_editor() as editor:
|
2015-01-29 22:14:55 +08:00
|
|
|
editor.alter_field(Book, old_field, new_field, strict=True)
|
2017-05-24 22:43:56 +08:00
|
|
|
self.assertForeignKeyExists(Book, 'author_id', 'schema_tag')
|
2012-06-19 20:25:22 +08:00
|
|
|
|
2017-05-24 13:25:59 +08:00
|
|
|
@skipUnlessDBFeature('supports_foreign_keys')
|
|
|
|
def test_char_field_with_db_index_to_fk(self):
|
|
|
|
# Create the table
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.create_model(Author)
|
|
|
|
editor.create_model(AuthorCharFieldWithIndex)
|
|
|
|
# Change CharField to FK
|
|
|
|
old_field = AuthorCharFieldWithIndex._meta.get_field('char_field')
|
|
|
|
new_field = ForeignKey(Author, CASCADE, blank=True)
|
|
|
|
new_field.set_attributes_from_name('char_field')
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.alter_field(AuthorCharFieldWithIndex, old_field, new_field, strict=True)
|
2017-05-24 22:43:56 +08:00
|
|
|
self.assertForeignKeyExists(AuthorCharFieldWithIndex, 'char_field_id', 'schema_author')
|
2017-05-24 13:25:59 +08:00
|
|
|
|
|
|
|
@skipUnlessDBFeature('supports_foreign_keys')
|
|
|
|
@skipUnlessDBFeature('supports_index_on_text_field')
|
|
|
|
def test_text_field_with_db_index_to_fk(self):
|
|
|
|
# Create the table
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.create_model(Author)
|
|
|
|
editor.create_model(AuthorTextFieldWithIndex)
|
|
|
|
# Change TextField to FK
|
|
|
|
old_field = AuthorTextFieldWithIndex._meta.get_field('text_field')
|
|
|
|
new_field = ForeignKey(Author, CASCADE, blank=True)
|
|
|
|
new_field.set_attributes_from_name('text_field')
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.alter_field(AuthorTextFieldWithIndex, old_field, new_field, strict=True)
|
2017-05-24 22:43:56 +08:00
|
|
|
self.assertForeignKeyExists(AuthorTextFieldWithIndex, 'text_field_id', 'schema_author')
|
2017-05-24 13:25:59 +08:00
|
|
|
|
2015-08-08 23:52:39 +08:00
|
|
|
@skipUnlessDBFeature('supports_foreign_keys')
|
|
|
|
def test_fk_to_proxy(self):
|
2016-10-27 15:53:39 +08:00
|
|
|
"Creating a FK to a proxy model creates database constraints."
|
2015-08-08 23:52:39 +08:00
|
|
|
class AuthorProxy(Author):
|
|
|
|
class Meta:
|
|
|
|
app_label = 'schema'
|
|
|
|
apps = new_apps
|
|
|
|
proxy = True
|
|
|
|
|
|
|
|
class AuthorRef(Model):
|
|
|
|
author = ForeignKey(AuthorProxy, on_delete=CASCADE)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
app_label = 'schema'
|
|
|
|
apps = new_apps
|
|
|
|
|
|
|
|
self.local_models = [AuthorProxy, AuthorRef]
|
|
|
|
|
|
|
|
# Create the table
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.create_model(Author)
|
|
|
|
editor.create_model(AuthorRef)
|
2017-05-24 22:43:56 +08:00
|
|
|
self.assertForeignKeyExists(AuthorRef, 'author_id', 'schema_author')
|
2015-08-08 23:52:39 +08:00
|
|
|
|
2015-08-08 23:27:06 +08:00
|
|
|
@skipUnlessDBFeature('supports_foreign_keys')
|
2014-08-09 15:50:00 +08:00
|
|
|
def test_fk_db_constraint(self):
|
2016-10-27 15:53:39 +08:00
|
|
|
"The db_constraint parameter is respected"
|
2014-08-09 15:50:00 +08:00
|
|
|
# Create the table
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.create_model(Tag)
|
|
|
|
editor.create_model(Author)
|
|
|
|
editor.create_model(BookWeak)
|
2016-10-27 15:53:39 +08:00
|
|
|
# Initial tables are there
|
2014-08-09 15:50:00 +08:00
|
|
|
list(Author.objects.all())
|
|
|
|
list(Tag.objects.all())
|
|
|
|
list(BookWeak.objects.all())
|
2017-05-24 22:43:56 +08:00
|
|
|
self.assertForeignKeyNotExists(BookWeak, 'author_id', 'schema_author')
|
2014-08-09 15:50:00 +08:00
|
|
|
# Make a db_constraint=False FK
|
2015-07-22 22:43:21 +08:00
|
|
|
new_field = ForeignKey(Tag, CASCADE, db_constraint=False)
|
2014-08-09 15:50:00 +08:00
|
|
|
new_field.set_attributes_from_name("tag")
|
|
|
|
with connection.schema_editor() as editor:
|
2015-01-29 22:14:55 +08:00
|
|
|
editor.add_field(Author, new_field)
|
2017-05-24 22:43:56 +08:00
|
|
|
self.assertForeignKeyNotExists(Author, 'tag_id', 'schema_tag')
|
2014-08-09 15:50:00 +08:00
|
|
|
# Alter to one with a constraint
|
2015-07-22 22:43:21 +08:00
|
|
|
new_field2 = ForeignKey(Tag, CASCADE)
|
2015-01-29 22:14:55 +08:00
|
|
|
new_field2.set_attributes_from_name("tag")
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.alter_field(Author, new_field, new_field2, strict=True)
|
2017-05-24 22:43:56 +08:00
|
|
|
self.assertForeignKeyExists(Author, 'tag_id', 'schema_tag')
|
2014-08-09 15:50:00 +08:00
|
|
|
# Alter to one without a constraint again
|
2015-07-22 22:43:21 +08:00
|
|
|
new_field2 = ForeignKey(Tag, CASCADE)
|
2015-01-29 22:14:55 +08:00
|
|
|
new_field2.set_attributes_from_name("tag")
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.alter_field(Author, new_field2, new_field, strict=True)
|
2017-05-24 22:43:56 +08:00
|
|
|
self.assertForeignKeyNotExists(Author, 'tag_id', 'schema_tag')
|
2014-08-09 15:50:00 +08:00
|
|
|
|
2015-01-29 22:14:55 +08:00
|
|
|
def _test_m2m_db_constraint(self, M2MFieldClass):
|
|
|
|
class LocalAuthorWithM2M(Model):
|
|
|
|
name = CharField(max_length=255)
|
|
|
|
|
|
|
|
class Meta:
|
2015-02-06 07:40:36 +08:00
|
|
|
app_label = 'schema'
|
2015-01-29 22:14:55 +08:00
|
|
|
apps = new_apps
|
|
|
|
|
|
|
|
self.local_models = [LocalAuthorWithM2M]
|
|
|
|
|
2014-08-09 15:50:00 +08:00
|
|
|
# Create the table
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.create_model(Tag)
|
2015-01-29 22:14:55 +08:00
|
|
|
editor.create_model(LocalAuthorWithM2M)
|
2016-10-27 15:53:39 +08:00
|
|
|
# Initial tables are there
|
2015-01-29 22:14:55 +08:00
|
|
|
list(LocalAuthorWithM2M.objects.all())
|
2014-08-09 15:50:00 +08:00
|
|
|
list(Tag.objects.all())
|
|
|
|
# Make a db_constraint=False FK
|
2015-01-29 22:14:55 +08:00
|
|
|
new_field = M2MFieldClass(Tag, related_name="authors", db_constraint=False)
|
|
|
|
new_field.contribute_to_class(LocalAuthorWithM2M, "tags")
|
2014-08-09 15:50:00 +08:00
|
|
|
# Add the field
|
|
|
|
with connection.schema_editor() as editor:
|
2015-01-29 22:14:55 +08:00
|
|
|
editor.add_field(LocalAuthorWithM2M, new_field)
|
2017-05-24 22:43:56 +08:00
|
|
|
self.assertForeignKeyNotExists(new_field.remote_field.through, 'tag_id', 'schema_tag')
|
2014-08-09 15:50:00 +08:00
|
|
|
|
2015-08-08 23:27:06 +08:00
|
|
|
@skipUnlessDBFeature('supports_foreign_keys')
|
2015-01-29 22:14:55 +08:00
|
|
|
def test_m2m_db_constraint(self):
|
|
|
|
self._test_m2m_db_constraint(ManyToManyField)
|
|
|
|
|
2015-08-08 23:27:06 +08:00
|
|
|
@skipUnlessDBFeature('supports_foreign_keys')
|
2015-01-29 22:14:55 +08:00
|
|
|
def test_m2m_db_constraint_custom(self):
|
|
|
|
self._test_m2m_db_constraint(CustomManyToManyField)
|
|
|
|
|
2015-08-08 23:27:06 +08:00
|
|
|
@skipUnlessDBFeature('supports_foreign_keys')
|
2015-01-29 22:14:55 +08:00
|
|
|
def test_m2m_db_constraint_inherited(self):
|
|
|
|
self._test_m2m_db_constraint(InheritedManyToManyField)
|
|
|
|
|
2013-05-30 00:47:10 +08:00
|
|
|
def test_add_field(self):
|
2012-06-19 20:25:22 +08:00
|
|
|
"""
|
|
|
|
Tests adding fields to models
|
|
|
|
"""
|
|
|
|
# Create the table
|
2013-05-18 17:48:46 +08:00
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.create_model(Author)
|
2012-06-19 20:25:22 +08:00
|
|
|
# Ensure there's no age field
|
|
|
|
columns = self.column_classes(Author)
|
|
|
|
self.assertNotIn("age", columns)
|
2014-01-20 01:10:24 +08:00
|
|
|
# Add the new field
|
2012-06-19 20:25:22 +08:00
|
|
|
new_field = IntegerField(null=True)
|
|
|
|
new_field.set_attributes_from_name("age")
|
2016-09-08 12:57:04 +08:00
|
|
|
with CaptureQueriesContext(connection) as ctx, connection.schema_editor() as editor:
|
2015-01-29 22:14:55 +08:00
|
|
|
editor.add_field(Author, new_field)
|
2016-09-08 12:57:04 +08:00
|
|
|
drop_default_sql = editor.sql_alter_column_no_default % {
|
|
|
|
'column': editor.quote_name(new_field.name),
|
|
|
|
}
|
|
|
|
self.assertFalse(any(drop_default_sql in query['sql'] for query in ctx.captured_queries))
|
2012-06-19 20:25:22 +08:00
|
|
|
# Ensure the field is right afterwards
|
|
|
|
columns = self.column_classes(Author)
|
|
|
|
self.assertEqual(columns['age'][0], "IntegerField")
|
|
|
|
self.assertEqual(columns['age'][1][6], True)
|
|
|
|
|
2014-01-20 01:10:24 +08:00
|
|
|
def test_add_field_temp_default(self):
|
|
|
|
"""
|
|
|
|
Tests adding fields to models with a temporary default
|
|
|
|
"""
|
|
|
|
# Create the table
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.create_model(Author)
|
|
|
|
# Ensure there's no age field
|
|
|
|
columns = self.column_classes(Author)
|
|
|
|
self.assertNotIn("age", columns)
|
|
|
|
# Add some rows of data
|
|
|
|
Author.objects.create(name="Andrew", height=30)
|
|
|
|
Author.objects.create(name="Andrea")
|
|
|
|
# Add a not-null field
|
|
|
|
new_field = CharField(max_length=30, default="Godwin")
|
|
|
|
new_field.set_attributes_from_name("surname")
|
|
|
|
with connection.schema_editor() as editor:
|
2015-01-29 22:14:55 +08:00
|
|
|
editor.add_field(Author, new_field)
|
2014-01-20 01:10:24 +08:00
|
|
|
# Ensure the field is right afterwards
|
|
|
|
columns = self.column_classes(Author)
|
|
|
|
self.assertEqual(columns['surname'][0], "CharField")
|
2014-01-20 19:01:14 +08:00
|
|
|
self.assertEqual(columns['surname'][1][6],
|
|
|
|
connection.features.interprets_empty_strings_as_nulls)
|
2014-01-20 01:10:24 +08:00
|
|
|
|
2014-01-25 08:09:56 +08:00
|
|
|
def test_add_field_temp_default_boolean(self):
|
|
|
|
"""
|
|
|
|
Tests adding fields to models with a temporary default where
|
|
|
|
the default is False. (#21783)
|
|
|
|
"""
|
|
|
|
# Create the table
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.create_model(Author)
|
|
|
|
# Ensure there's no age field
|
|
|
|
columns = self.column_classes(Author)
|
|
|
|
self.assertNotIn("age", columns)
|
|
|
|
# Add some rows of data
|
|
|
|
Author.objects.create(name="Andrew", height=30)
|
|
|
|
Author.objects.create(name="Andrea")
|
|
|
|
# Add a not-null field
|
|
|
|
new_field = BooleanField(default=False)
|
|
|
|
new_field.set_attributes_from_name("awesome")
|
|
|
|
with connection.schema_editor() as editor:
|
2015-01-29 22:14:55 +08:00
|
|
|
editor.add_field(Author, new_field)
|
2014-01-25 08:09:56 +08:00
|
|
|
# Ensure the field is right afterwards
|
|
|
|
columns = self.column_classes(Author)
|
2014-01-25 23:59:38 +08:00
|
|
|
# BooleanField are stored as TINYINT(1) on MySQL.
|
2014-09-26 14:43:50 +08:00
|
|
|
field_type = columns['awesome'][0]
|
2015-09-12 07:33:12 +08:00
|
|
|
self.assertEqual(
|
|
|
|
field_type,
|
2017-01-28 21:19:47 +08:00
|
|
|
connection.features.introspected_boolean_field_type(new_field)
|
2015-09-12 07:33:12 +08:00
|
|
|
)
|
2014-01-25 08:09:56 +08:00
|
|
|
|
2014-05-08 04:46:23 +08:00
|
|
|
def test_add_field_default_transform(self):
|
|
|
|
"""
|
|
|
|
Tests adding fields to models with a default that is not directly
|
|
|
|
valid in the database (#22581)
|
|
|
|
"""
|
2014-05-09 03:49:54 +08:00
|
|
|
|
2014-05-08 04:46:23 +08:00
|
|
|
class TestTransformField(IntegerField):
|
2014-05-09 03:49:54 +08:00
|
|
|
|
2014-05-08 04:46:23 +08:00
|
|
|
# Weird field that saves the count of items in its value
|
|
|
|
def get_default(self):
|
|
|
|
return self.default
|
2014-05-09 03:49:54 +08:00
|
|
|
|
2014-05-08 04:46:23 +08:00
|
|
|
def get_prep_value(self, value):
|
|
|
|
if value is None:
|
|
|
|
return 0
|
|
|
|
return len(value)
|
2014-05-09 03:49:54 +08:00
|
|
|
|
2014-05-08 04:46:23 +08:00
|
|
|
# Create the table
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.create_model(Author)
|
|
|
|
# Add some rows of data
|
|
|
|
Author.objects.create(name="Andrew", height=30)
|
|
|
|
Author.objects.create(name="Andrea")
|
|
|
|
# Add the field with a default it needs to cast (to string in this case)
|
2014-05-09 03:49:54 +08:00
|
|
|
new_field = TestTransformField(default={1: 2})
|
2014-05-08 04:46:23 +08:00
|
|
|
new_field.set_attributes_from_name("thing")
|
|
|
|
with connection.schema_editor() as editor:
|
2015-01-29 22:14:55 +08:00
|
|
|
editor.add_field(Author, new_field)
|
2014-05-08 04:46:23 +08:00
|
|
|
# Ensure the field is there
|
|
|
|
columns = self.column_classes(Author)
|
|
|
|
field_type, field_info = columns['thing']
|
|
|
|
self.assertEqual(field_type, 'IntegerField')
|
|
|
|
# Make sure the values were transformed correctly
|
|
|
|
self.assertEqual(Author.objects.extra(where=["thing = 1"]).count(), 2)
|
|
|
|
|
2014-06-17 09:43:45 +08:00
|
|
|
def test_add_field_binary(self):
|
|
|
|
"""
|
|
|
|
Tests binary fields get a sane default (#22851)
|
|
|
|
"""
|
|
|
|
# Create the table
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.create_model(Author)
|
|
|
|
# Add the new field
|
|
|
|
new_field = BinaryField(blank=True)
|
|
|
|
new_field.set_attributes_from_name("bits")
|
|
|
|
with connection.schema_editor() as editor:
|
2015-01-29 22:14:55 +08:00
|
|
|
editor.add_field(Author, new_field)
|
2014-06-17 09:43:45 +08:00
|
|
|
# Ensure the field is right afterwards
|
|
|
|
columns = self.column_classes(Author)
|
2014-06-17 15:36:27 +08:00
|
|
|
# MySQL annoyingly uses the same backend, so it'll come back as one of
|
|
|
|
# these two types.
|
|
|
|
self.assertIn(columns['bits'][0], ("BinaryField", "TextField"))
|
2014-06-17 09:43:45 +08:00
|
|
|
|
2015-05-24 18:16:21 +08:00
|
|
|
@unittest.skipUnless(connection.vendor == 'mysql', "MySQL specific")
|
|
|
|
def test_add_binaryfield_mediumblob(self):
|
|
|
|
"""
|
|
|
|
Test adding a custom-sized binary field on MySQL (#24846).
|
|
|
|
"""
|
|
|
|
# Create the table
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.create_model(Author)
|
|
|
|
# Add the new field with default
|
|
|
|
new_field = MediumBlobField(blank=True, default=b'123')
|
|
|
|
new_field.set_attributes_from_name('bits')
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.add_field(Author, new_field)
|
|
|
|
columns = self.column_classes(Author)
|
|
|
|
# Introspection treats BLOBs as TextFields
|
|
|
|
self.assertEqual(columns['bits'][0], "TextField")
|
|
|
|
|
2012-06-19 20:25:22 +08:00
|
|
|
def test_alter(self):
|
|
|
|
"""
|
|
|
|
Tests simple altering of fields
|
|
|
|
"""
|
|
|
|
# Create the table
|
2013-05-18 17:48:46 +08:00
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.create_model(Author)
|
2012-06-19 20:25:22 +08:00
|
|
|
# Ensure the field is right to begin with
|
|
|
|
columns = self.column_classes(Author)
|
|
|
|
self.assertEqual(columns['name'][0], "CharField")
|
2013-08-14 03:54:57 +08:00
|
|
|
self.assertEqual(bool(columns['name'][1][6]), bool(connection.features.interprets_empty_strings_as_nulls))
|
2012-06-19 20:25:22 +08:00
|
|
|
# Alter the name field to a TextField
|
2015-01-29 22:14:55 +08:00
|
|
|
old_field = Author._meta.get_field("name")
|
2012-06-19 20:25:22 +08:00
|
|
|
new_field = TextField(null=True)
|
|
|
|
new_field.set_attributes_from_name("name")
|
2013-05-18 17:48:46 +08:00
|
|
|
with connection.schema_editor() as editor:
|
2015-01-29 22:14:55 +08:00
|
|
|
editor.alter_field(Author, old_field, new_field, strict=True)
|
2012-06-19 20:25:22 +08:00
|
|
|
# Ensure the field is right afterwards
|
|
|
|
columns = self.column_classes(Author)
|
|
|
|
self.assertEqual(columns['name'][0], "TextField")
|
|
|
|
self.assertEqual(columns['name'][1][6], True)
|
2012-09-24 19:53:37 +08:00
|
|
|
# Change nullability again
|
|
|
|
new_field2 = TextField(null=False)
|
|
|
|
new_field2.set_attributes_from_name("name")
|
2013-05-18 17:48:46 +08:00
|
|
|
with connection.schema_editor() as editor:
|
2015-01-29 22:14:55 +08:00
|
|
|
editor.alter_field(Author, new_field, new_field2, strict=True)
|
2012-09-24 19:53:37 +08:00
|
|
|
# Ensure the field is right afterwards
|
|
|
|
columns = self.column_classes(Author)
|
|
|
|
self.assertEqual(columns['name'][0], "TextField")
|
2015-02-18 06:50:45 +08:00
|
|
|
self.assertEqual(bool(columns['name'][1][6]), bool(connection.features.interprets_empty_strings_as_nulls))
|
2012-08-02 22:08:39 +08:00
|
|
|
|
2017-06-01 17:49:57 +08:00
|
|
|
def test_alter_auto_field_to_integer_field(self):
|
|
|
|
# Create the table
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.create_model(Author)
|
|
|
|
# Change AutoField to IntegerField
|
|
|
|
old_field = Author._meta.get_field('id')
|
|
|
|
new_field = IntegerField(primary_key=True)
|
|
|
|
new_field.set_attributes_from_name('id')
|
|
|
|
new_field.model = Author
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.alter_field(Author, old_field, new_field, strict=True)
|
|
|
|
|
|
|
|
def test_alter_auto_field_to_char_field(self):
|
|
|
|
# Create the table
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.create_model(Author)
|
|
|
|
# Change AutoField to CharField
|
|
|
|
old_field = Author._meta.get_field('id')
|
|
|
|
new_field = CharField(primary_key=True, max_length=50)
|
|
|
|
new_field.set_attributes_from_name('id')
|
|
|
|
new_field.model = Author
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.alter_field(Author, old_field, new_field, strict=True)
|
|
|
|
|
2014-12-04 22:11:30 +08:00
|
|
|
def test_alter_text_field(self):
|
|
|
|
# Regression for "BLOB/TEXT column 'info' can't have a default value")
|
|
|
|
# on MySQL.
|
2015-01-29 22:14:55 +08:00
|
|
|
# Create the table
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.create_model(Note)
|
|
|
|
old_field = Note._meta.get_field("info")
|
2014-12-04 22:11:30 +08:00
|
|
|
new_field = TextField(blank=True)
|
|
|
|
new_field.set_attributes_from_name("info")
|
|
|
|
with connection.schema_editor() as editor:
|
2015-01-29 22:14:55 +08:00
|
|
|
editor.alter_field(Note, old_field, new_field, strict=True)
|
2015-06-19 09:47:21 +08:00
|
|
|
|
2016-12-18 04:06:47 +08:00
|
|
|
@skipUnlessDBFeature('can_defer_constraint_checks', 'can_rollback_ddl')
|
|
|
|
def test_alter_fk_checks_deferred_constraints(self):
|
|
|
|
"""
|
|
|
|
#25492 - Altering a foreign key's structure and data in the same
|
|
|
|
transaction.
|
|
|
|
"""
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.create_model(Node)
|
|
|
|
old_field = Node._meta.get_field('parent')
|
|
|
|
new_field = ForeignKey(Node, CASCADE)
|
|
|
|
new_field.set_attributes_from_name('parent')
|
|
|
|
parent = Node.objects.create()
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
# Update the parent FK to create a deferred constraint check.
|
|
|
|
Node.objects.update(parent=parent)
|
|
|
|
editor.alter_field(Node, old_field, new_field, strict=True)
|
|
|
|
|
2015-06-23 11:53:49 +08:00
|
|
|
def test_alter_text_field_to_date_field(self):
|
|
|
|
"""
|
|
|
|
#25002 - Test conversion of text field to date field.
|
|
|
|
"""
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.create_model(Note)
|
|
|
|
Note.objects.create(info='1988-05-05')
|
|
|
|
old_field = Note._meta.get_field('info')
|
|
|
|
new_field = DateField(blank=True)
|
|
|
|
new_field.set_attributes_from_name('info')
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.alter_field(Note, old_field, new_field, strict=True)
|
|
|
|
# Make sure the field isn't nullable
|
|
|
|
columns = self.column_classes(Note)
|
|
|
|
self.assertFalse(columns['info'][1][6])
|
|
|
|
|
|
|
|
def test_alter_text_field_to_datetime_field(self):
|
|
|
|
"""
|
|
|
|
#25002 - Test conversion of text field to datetime field.
|
|
|
|
"""
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.create_model(Note)
|
|
|
|
Note.objects.create(info='1988-05-05 3:16:17.4567')
|
|
|
|
old_field = Note._meta.get_field('info')
|
|
|
|
new_field = DateTimeField(blank=True)
|
|
|
|
new_field.set_attributes_from_name('info')
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.alter_field(Note, old_field, new_field, strict=True)
|
|
|
|
# Make sure the field isn't nullable
|
|
|
|
columns = self.column_classes(Note)
|
|
|
|
self.assertFalse(columns['info'][1][6])
|
|
|
|
|
2015-06-19 09:47:21 +08:00
|
|
|
def test_alter_text_field_to_time_field(self):
|
|
|
|
"""
|
|
|
|
#25002 - Test conversion of text field to time field.
|
|
|
|
"""
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.create_model(Note)
|
2015-06-23 11:53:49 +08:00
|
|
|
Note.objects.create(info='3:16:17.4567')
|
2015-06-19 09:47:21 +08:00
|
|
|
old_field = Note._meta.get_field('info')
|
|
|
|
new_field = TimeField(blank=True)
|
|
|
|
new_field.set_attributes_from_name('info')
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.alter_field(Note, old_field, new_field, strict=True)
|
2015-06-23 11:53:49 +08:00
|
|
|
# Make sure the field isn't nullable
|
|
|
|
columns = self.column_classes(Note)
|
|
|
|
self.assertFalse(columns['info'][1][6])
|
2014-12-04 22:11:30 +08:00
|
|
|
|
2015-04-18 23:52:30 +08:00
|
|
|
@skipIfDBFeature('interprets_empty_strings_as_nulls')
|
|
|
|
def test_alter_textual_field_keep_null_status(self):
|
2015-04-11 22:10:31 +08:00
|
|
|
"""
|
|
|
|
Changing a field type shouldn't affect the not null status.
|
|
|
|
"""
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.create_model(Note)
|
|
|
|
with self.assertRaises(IntegrityError):
|
|
|
|
Note.objects.create(info=None)
|
|
|
|
old_field = Note._meta.get_field("info")
|
|
|
|
new_field = CharField(max_length=50)
|
|
|
|
new_field.set_attributes_from_name("info")
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.alter_field(Note, old_field, new_field, strict=True)
|
|
|
|
with self.assertRaises(IntegrityError):
|
|
|
|
Note.objects.create(info=None)
|
|
|
|
|
2015-04-18 23:52:30 +08:00
|
|
|
def test_alter_numeric_field_keep_null_status(self):
|
|
|
|
"""
|
|
|
|
Changing a field type shouldn't affect the not null status.
|
|
|
|
"""
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.create_model(UniqueTest)
|
|
|
|
with self.assertRaises(IntegrityError):
|
|
|
|
UniqueTest.objects.create(year=None, slug='aaa')
|
|
|
|
old_field = UniqueTest._meta.get_field("year")
|
|
|
|
new_field = BigIntegerField()
|
|
|
|
new_field.set_attributes_from_name("year")
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.alter_field(UniqueTest, old_field, new_field, strict=True)
|
|
|
|
with self.assertRaises(IntegrityError):
|
|
|
|
UniqueTest.objects.create(year=None, slug='bbb')
|
|
|
|
|
2014-10-07 07:53:21 +08:00
|
|
|
def test_alter_null_to_not_null(self):
|
|
|
|
"""
|
|
|
|
#23609 - Tests handling of default values when altering from NULL to NOT NULL.
|
|
|
|
"""
|
|
|
|
# Create the table
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.create_model(Author)
|
|
|
|
# Ensure the field is right to begin with
|
|
|
|
columns = self.column_classes(Author)
|
|
|
|
self.assertTrue(columns['height'][1][6])
|
|
|
|
# Create some test data
|
|
|
|
Author.objects.create(name='Not null author', height=12)
|
|
|
|
Author.objects.create(name='Null author')
|
|
|
|
# Verify null value
|
|
|
|
self.assertEqual(Author.objects.get(name='Not null author').height, 12)
|
|
|
|
self.assertIsNone(Author.objects.get(name='Null author').height)
|
|
|
|
# Alter the height field to NOT NULL with default
|
2015-01-29 22:14:55 +08:00
|
|
|
old_field = Author._meta.get_field("height")
|
2014-10-07 07:53:21 +08:00
|
|
|
new_field = PositiveIntegerField(default=42)
|
|
|
|
new_field.set_attributes_from_name("height")
|
|
|
|
with connection.schema_editor() as editor:
|
2016-07-09 00:03:33 +08:00
|
|
|
editor.alter_field(Author, old_field, new_field, strict=True)
|
2014-10-07 07:53:21 +08:00
|
|
|
# Ensure the field is right afterwards
|
|
|
|
columns = self.column_classes(Author)
|
|
|
|
self.assertFalse(columns['height'][1][6])
|
|
|
|
# Verify default value
|
|
|
|
self.assertEqual(Author.objects.get(name='Not null author').height, 12)
|
|
|
|
self.assertEqual(Author.objects.get(name='Null author').height, 42)
|
|
|
|
|
2015-02-18 06:50:45 +08:00
|
|
|
def test_alter_charfield_to_null(self):
|
|
|
|
"""
|
|
|
|
#24307 - Should skip an alter statement on databases with
|
|
|
|
interprets_empty_strings_as_null when changing a CharField to null.
|
|
|
|
"""
|
|
|
|
# Create the table
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.create_model(Author)
|
|
|
|
# Change the CharField to null
|
|
|
|
old_field = Author._meta.get_field('name')
|
|
|
|
new_field = copy(old_field)
|
|
|
|
new_field.null = True
|
|
|
|
with connection.schema_editor() as editor:
|
2016-07-09 00:03:33 +08:00
|
|
|
editor.alter_field(Author, old_field, new_field, strict=True)
|
2015-02-18 06:50:45 +08:00
|
|
|
|
|
|
|
def test_alter_textfield_to_null(self):
|
|
|
|
"""
|
|
|
|
#24307 - Should skip an alter statement on databases with
|
|
|
|
interprets_empty_strings_as_null when changing a TextField to null.
|
|
|
|
"""
|
|
|
|
# Create the table
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.create_model(Note)
|
|
|
|
# Change the TextField to null
|
|
|
|
old_field = Note._meta.get_field('info')
|
|
|
|
new_field = copy(old_field)
|
|
|
|
new_field.null = True
|
|
|
|
with connection.schema_editor() as editor:
|
2016-07-09 00:03:33 +08:00
|
|
|
editor.alter_field(Note, old_field, new_field, strict=True)
|
2015-02-18 06:50:45 +08:00
|
|
|
|
2015-08-08 23:27:06 +08:00
|
|
|
@skipUnlessDBFeature('supports_combined_alters')
|
2014-10-31 21:08:24 +08:00
|
|
|
def test_alter_null_to_not_null_keeping_default(self):
|
|
|
|
"""
|
|
|
|
#23738 - Can change a nullable field with default to non-nullable
|
|
|
|
with the same default.
|
|
|
|
"""
|
|
|
|
# Create the table
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.create_model(AuthorWithDefaultHeight)
|
|
|
|
# Ensure the field is right to begin with
|
|
|
|
columns = self.column_classes(AuthorWithDefaultHeight)
|
|
|
|
self.assertTrue(columns['height'][1][6])
|
|
|
|
# Alter the height field to NOT NULL keeping the previous default
|
2015-01-29 22:14:55 +08:00
|
|
|
old_field = AuthorWithDefaultHeight._meta.get_field("height")
|
2014-10-31 21:08:24 +08:00
|
|
|
new_field = PositiveIntegerField(default=42)
|
|
|
|
new_field.set_attributes_from_name("height")
|
|
|
|
with connection.schema_editor() as editor:
|
2016-07-09 00:03:33 +08:00
|
|
|
editor.alter_field(AuthorWithDefaultHeight, old_field, new_field, strict=True)
|
2014-10-31 21:08:24 +08:00
|
|
|
# Ensure the field is right afterwards
|
|
|
|
columns = self.column_classes(AuthorWithDefaultHeight)
|
|
|
|
self.assertFalse(columns['height'][1][6])
|
|
|
|
|
2015-08-08 23:27:06 +08:00
|
|
|
@skipUnlessDBFeature('supports_foreign_keys')
|
2014-08-04 09:58:44 +08:00
|
|
|
def test_alter_fk(self):
|
|
|
|
"""
|
|
|
|
Tests altering of FKs
|
|
|
|
"""
|
|
|
|
# Create the table
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.create_model(Author)
|
|
|
|
editor.create_model(Book)
|
|
|
|
# Ensure the field is right to begin with
|
|
|
|
columns = self.column_classes(Book)
|
|
|
|
self.assertEqual(columns['author_id'][0], "IntegerField")
|
2017-05-24 22:43:56 +08:00
|
|
|
self.assertForeignKeyExists(Book, 'author_id', 'schema_author')
|
2014-08-04 09:58:44 +08:00
|
|
|
# Alter the FK
|
2015-01-29 22:14:55 +08:00
|
|
|
old_field = Book._meta.get_field("author")
|
2015-07-22 22:43:21 +08:00
|
|
|
new_field = ForeignKey(Author, CASCADE, editable=False)
|
2014-08-04 09:58:44 +08:00
|
|
|
new_field.set_attributes_from_name("author")
|
|
|
|
with connection.schema_editor() as editor:
|
2015-01-29 22:14:55 +08:00
|
|
|
editor.alter_field(Book, old_field, new_field, strict=True)
|
2014-08-04 09:58:44 +08:00
|
|
|
# Ensure the field is right afterwards
|
|
|
|
columns = self.column_classes(Book)
|
|
|
|
self.assertEqual(columns['author_id'][0], "IntegerField")
|
2017-05-24 22:43:56 +08:00
|
|
|
self.assertForeignKeyExists(Book, 'author_id', 'schema_author')
|
2015-03-05 20:41:15 +08:00
|
|
|
|
2015-08-08 23:27:06 +08:00
|
|
|
@skipUnlessDBFeature('supports_foreign_keys')
|
2015-03-05 20:41:15 +08:00
|
|
|
def test_alter_to_fk(self):
|
|
|
|
"""
|
|
|
|
#24447 - Tests adding a FK constraint for an existing column
|
|
|
|
"""
|
|
|
|
class LocalBook(Model):
|
|
|
|
author = IntegerField()
|
|
|
|
title = CharField(max_length=100, db_index=True)
|
|
|
|
pub_date = DateTimeField()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
app_label = 'schema'
|
|
|
|
apps = new_apps
|
|
|
|
|
|
|
|
self.local_models = [LocalBook]
|
|
|
|
|
|
|
|
# Create the tables
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.create_model(Author)
|
|
|
|
editor.create_model(LocalBook)
|
|
|
|
# Ensure no FK constraint exists
|
|
|
|
constraints = self.get_constraints(LocalBook._meta.db_table)
|
|
|
|
for name, details in constraints.items():
|
|
|
|
if details['foreign_key']:
|
|
|
|
self.fail('Found an unexpected FK constraint to %s' % details['columns'])
|
|
|
|
old_field = LocalBook._meta.get_field("author")
|
2015-07-22 22:43:21 +08:00
|
|
|
new_field = ForeignKey(Author, CASCADE)
|
2015-03-05 20:41:15 +08:00
|
|
|
new_field.set_attributes_from_name("author")
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.alter_field(LocalBook, old_field, new_field, strict=True)
|
2017-05-24 22:43:56 +08:00
|
|
|
self.assertForeignKeyExists(LocalBook, 'author_id', 'schema_author')
|
2014-08-04 09:58:44 +08:00
|
|
|
|
2015-08-08 23:27:06 +08:00
|
|
|
@skipUnlessDBFeature('supports_foreign_keys')
|
2015-01-19 22:31:23 +08:00
|
|
|
def test_alter_o2o_to_fk(self):
|
|
|
|
"""
|
2015-01-20 18:39:23 +08:00
|
|
|
#24163 - Tests altering of OneToOneField to ForeignKey
|
2015-01-19 22:31:23 +08:00
|
|
|
"""
|
|
|
|
# Create the table
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.create_model(Author)
|
|
|
|
editor.create_model(BookWithO2O)
|
|
|
|
# Ensure the field is right to begin with
|
|
|
|
columns = self.column_classes(BookWithO2O)
|
|
|
|
self.assertEqual(columns['author_id'][0], "IntegerField")
|
2015-01-20 18:39:23 +08:00
|
|
|
# Ensure the field is unique
|
|
|
|
author = Author.objects.create(name="Joe")
|
|
|
|
BookWithO2O.objects.create(author=author, title="Django 1", pub_date=datetime.datetime.now())
|
|
|
|
with self.assertRaises(IntegrityError):
|
|
|
|
BookWithO2O.objects.create(author=author, title="Django 2", pub_date=datetime.datetime.now())
|
|
|
|
BookWithO2O.objects.all().delete()
|
2017-05-24 22:43:56 +08:00
|
|
|
self.assertForeignKeyExists(BookWithO2O, 'author_id', 'schema_author')
|
2015-01-20 18:39:23 +08:00
|
|
|
# Alter the OneToOneField to ForeignKey
|
2015-01-29 22:14:55 +08:00
|
|
|
old_field = BookWithO2O._meta.get_field("author")
|
2015-07-22 22:43:21 +08:00
|
|
|
new_field = ForeignKey(Author, CASCADE)
|
2015-01-19 22:31:23 +08:00
|
|
|
new_field.set_attributes_from_name("author")
|
|
|
|
with connection.schema_editor() as editor:
|
2015-01-29 22:14:55 +08:00
|
|
|
editor.alter_field(BookWithO2O, old_field, new_field, strict=True)
|
2015-01-19 22:31:23 +08:00
|
|
|
# Ensure the field is right afterwards
|
|
|
|
columns = self.column_classes(Book)
|
|
|
|
self.assertEqual(columns['author_id'][0], "IntegerField")
|
2015-01-20 18:39:23 +08:00
|
|
|
# Ensure the field is not unique anymore
|
|
|
|
Book.objects.create(author=author, title="Django 1", pub_date=datetime.datetime.now())
|
|
|
|
Book.objects.create(author=author, title="Django 2", pub_date=datetime.datetime.now())
|
2017-05-24 22:43:56 +08:00
|
|
|
self.assertForeignKeyExists(Book, 'author_id', 'schema_author')
|
2015-01-19 22:31:23 +08:00
|
|
|
|
2015-08-08 23:27:06 +08:00
|
|
|
@skipUnlessDBFeature('supports_foreign_keys')
|
2015-01-19 22:31:23 +08:00
|
|
|
def test_alter_fk_to_o2o(self):
|
|
|
|
"""
|
2015-01-20 18:39:23 +08:00
|
|
|
#24163 - Tests altering of ForeignKey to OneToOneField
|
2015-01-19 22:31:23 +08:00
|
|
|
"""
|
|
|
|
# Create the table
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.create_model(Author)
|
|
|
|
editor.create_model(Book)
|
|
|
|
# Ensure the field is right to begin with
|
|
|
|
columns = self.column_classes(Book)
|
|
|
|
self.assertEqual(columns['author_id'][0], "IntegerField")
|
2015-01-20 18:39:23 +08:00
|
|
|
# Ensure the field is not unique
|
|
|
|
author = Author.objects.create(name="Joe")
|
|
|
|
Book.objects.create(author=author, title="Django 1", pub_date=datetime.datetime.now())
|
|
|
|
Book.objects.create(author=author, title="Django 2", pub_date=datetime.datetime.now())
|
|
|
|
Book.objects.all().delete()
|
2017-05-24 22:43:56 +08:00
|
|
|
self.assertForeignKeyExists(Book, 'author_id', 'schema_author')
|
2015-01-20 18:39:23 +08:00
|
|
|
# Alter the ForeignKey to OneToOneField
|
2015-01-29 22:14:55 +08:00
|
|
|
old_field = Book._meta.get_field("author")
|
2015-07-22 22:43:21 +08:00
|
|
|
new_field = OneToOneField(Author, CASCADE)
|
2015-01-19 22:31:23 +08:00
|
|
|
new_field.set_attributes_from_name("author")
|
|
|
|
with connection.schema_editor() as editor:
|
2015-01-29 22:14:55 +08:00
|
|
|
editor.alter_field(Book, old_field, new_field, strict=True)
|
2015-01-19 22:31:23 +08:00
|
|
|
# Ensure the field is right afterwards
|
|
|
|
columns = self.column_classes(BookWithO2O)
|
|
|
|
self.assertEqual(columns['author_id'][0], "IntegerField")
|
2015-01-20 18:39:23 +08:00
|
|
|
# Ensure the field is unique now
|
|
|
|
BookWithO2O.objects.create(author=author, title="Django 1", pub_date=datetime.datetime.now())
|
|
|
|
with self.assertRaises(IntegrityError):
|
|
|
|
BookWithO2O.objects.create(author=author, title="Django 2", pub_date=datetime.datetime.now())
|
2017-05-24 22:43:56 +08:00
|
|
|
self.assertForeignKeyExists(BookWithO2O, 'author_id', 'schema_author')
|
2015-01-19 22:31:23 +08:00
|
|
|
|
2016-06-23 12:46:11 +08:00
|
|
|
def test_alter_field_fk_to_o2o(self):
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.create_model(Author)
|
|
|
|
editor.create_model(Book)
|
|
|
|
expected_fks = 1 if connection.features.supports_foreign_keys else 0
|
|
|
|
|
|
|
|
# Check the index is right to begin with.
|
|
|
|
counts = self.get_constraints_count(
|
|
|
|
Book._meta.db_table,
|
|
|
|
Book._meta.get_field('author').column,
|
|
|
|
(Author._meta.db_table, Author._meta.pk.column),
|
|
|
|
)
|
|
|
|
self.assertEqual(counts, {'fks': expected_fks, 'uniques': 0, 'indexes': 1})
|
|
|
|
|
|
|
|
old_field = Book._meta.get_field('author')
|
|
|
|
new_field = OneToOneField(Author, CASCADE)
|
|
|
|
new_field.set_attributes_from_name('author')
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.alter_field(Book, old_field, new_field, strict=True)
|
|
|
|
|
|
|
|
counts = self.get_constraints_count(
|
|
|
|
Book._meta.db_table,
|
|
|
|
Book._meta.get_field('author').column,
|
|
|
|
(Author._meta.db_table, Author._meta.pk.column),
|
|
|
|
)
|
|
|
|
# The index on ForeignKey is replaced with a unique constraint for OneToOneField.
|
|
|
|
self.assertEqual(counts, {'fks': expected_fks, 'uniques': 1, 'indexes': 0})
|
|
|
|
|
|
|
|
def test_alter_field_fk_keeps_index(self):
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.create_model(Author)
|
|
|
|
editor.create_model(Book)
|
|
|
|
expected_fks = 1 if connection.features.supports_foreign_keys else 0
|
|
|
|
|
|
|
|
# Check the index is right to begin with.
|
|
|
|
counts = self.get_constraints_count(
|
|
|
|
Book._meta.db_table,
|
|
|
|
Book._meta.get_field('author').column,
|
|
|
|
(Author._meta.db_table, Author._meta.pk.column),
|
|
|
|
)
|
|
|
|
self.assertEqual(counts, {'fks': expected_fks, 'uniques': 0, 'indexes': 1})
|
|
|
|
|
|
|
|
old_field = Book._meta.get_field('author')
|
|
|
|
# on_delete changed from CASCADE.
|
|
|
|
new_field = ForeignKey(Author, PROTECT)
|
|
|
|
new_field.set_attributes_from_name('author')
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.alter_field(Book, old_field, new_field, strict=True)
|
|
|
|
|
|
|
|
counts = self.get_constraints_count(
|
|
|
|
Book._meta.db_table,
|
|
|
|
Book._meta.get_field('author').column,
|
|
|
|
(Author._meta.db_table, Author._meta.pk.column),
|
|
|
|
)
|
|
|
|
# The index remains.
|
|
|
|
self.assertEqual(counts, {'fks': expected_fks, 'uniques': 0, 'indexes': 1})
|
|
|
|
|
|
|
|
def test_alter_field_o2o_to_fk(self):
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.create_model(Author)
|
|
|
|
editor.create_model(BookWithO2O)
|
|
|
|
expected_fks = 1 if connection.features.supports_foreign_keys else 0
|
|
|
|
|
|
|
|
# Check the unique constraint is right to begin with.
|
|
|
|
counts = self.get_constraints_count(
|
|
|
|
BookWithO2O._meta.db_table,
|
|
|
|
BookWithO2O._meta.get_field('author').column,
|
|
|
|
(Author._meta.db_table, Author._meta.pk.column),
|
|
|
|
)
|
|
|
|
self.assertEqual(counts, {'fks': expected_fks, 'uniques': 1, 'indexes': 0})
|
|
|
|
|
|
|
|
old_field = BookWithO2O._meta.get_field('author')
|
|
|
|
new_field = ForeignKey(Author, CASCADE)
|
|
|
|
new_field.set_attributes_from_name('author')
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.alter_field(BookWithO2O, old_field, new_field, strict=True)
|
|
|
|
|
|
|
|
counts = self.get_constraints_count(
|
|
|
|
BookWithO2O._meta.db_table,
|
|
|
|
BookWithO2O._meta.get_field('author').column,
|
|
|
|
(Author._meta.db_table, Author._meta.pk.column),
|
|
|
|
)
|
|
|
|
# The unique constraint on OneToOneField is replaced with an index for ForeignKey.
|
|
|
|
self.assertEqual(counts, {'fks': expected_fks, 'uniques': 0, 'indexes': 1})
|
|
|
|
|
|
|
|
def test_alter_field_o2o_keeps_unique(self):
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.create_model(Author)
|
|
|
|
editor.create_model(BookWithO2O)
|
|
|
|
expected_fks = 1 if connection.features.supports_foreign_keys else 0
|
|
|
|
|
|
|
|
# Check the unique constraint is right to begin with.
|
|
|
|
counts = self.get_constraints_count(
|
|
|
|
BookWithO2O._meta.db_table,
|
|
|
|
BookWithO2O._meta.get_field('author').column,
|
|
|
|
(Author._meta.db_table, Author._meta.pk.column),
|
|
|
|
)
|
|
|
|
self.assertEqual(counts, {'fks': expected_fks, 'uniques': 1, 'indexes': 0})
|
|
|
|
|
|
|
|
old_field = BookWithO2O._meta.get_field('author')
|
|
|
|
# on_delete changed from CASCADE.
|
|
|
|
new_field = OneToOneField(Author, PROTECT)
|
|
|
|
new_field.set_attributes_from_name('author')
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.alter_field(BookWithO2O, old_field, new_field, strict=True)
|
|
|
|
|
|
|
|
counts = self.get_constraints_count(
|
|
|
|
BookWithO2O._meta.db_table,
|
|
|
|
BookWithO2O._meta.get_field('author').column,
|
|
|
|
(Author._meta.db_table, Author._meta.pk.column),
|
|
|
|
)
|
|
|
|
# The unique constraint remains.
|
|
|
|
self.assertEqual(counts, {'fks': expected_fks, 'uniques': 1, 'indexes': 0})
|
|
|
|
|
2016-06-21 12:01:03 +08:00
|
|
|
def test_alter_db_table_case(self):
|
|
|
|
# Create the table
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.create_model(Author)
|
|
|
|
# Alter the case of the table
|
|
|
|
old_table_name = Author._meta.db_table
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.alter_db_table(Author, old_table_name, old_table_name.upper())
|
|
|
|
|
2014-11-04 06:48:03 +08:00
|
|
|
def test_alter_implicit_id_to_explicit(self):
|
2014-10-24 00:24:34 +08:00
|
|
|
"""
|
|
|
|
Should be able to convert an implicit "id" field to an explicit "id"
|
|
|
|
primary key field.
|
|
|
|
"""
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.create_model(Author)
|
|
|
|
|
2015-01-29 22:14:55 +08:00
|
|
|
old_field = Author._meta.get_field("id")
|
2016-01-22 23:55:31 +08:00
|
|
|
new_field = AutoField(primary_key=True)
|
2014-10-24 00:24:34 +08:00
|
|
|
new_field.set_attributes_from_name("id")
|
|
|
|
new_field.model = Author
|
|
|
|
with connection.schema_editor() as editor:
|
2015-01-29 22:14:55 +08:00
|
|
|
editor.alter_field(Author, old_field, new_field, strict=True)
|
2014-12-30 05:23:18 +08:00
|
|
|
# This will fail if DROP DEFAULT is inadvertently executed on this
|
|
|
|
# field which drops the id sequence, at least on PostgreSQL.
|
|
|
|
Author.objects.create(name='Foo')
|
2016-01-22 23:55:31 +08:00
|
|
|
Author.objects.create(name='Bar')
|
2014-12-30 05:23:18 +08:00
|
|
|
|
2015-06-02 05:06:54 +08:00
|
|
|
def test_alter_int_pk_to_autofield_pk(self):
|
|
|
|
"""
|
|
|
|
Should be able to rename an IntegerField(primary_key=True) to
|
|
|
|
AutoField(primary_key=True).
|
|
|
|
"""
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.create_model(IntegerPK)
|
|
|
|
|
|
|
|
old_field = IntegerPK._meta.get_field('i')
|
|
|
|
new_field = AutoField(primary_key=True)
|
|
|
|
new_field.model = IntegerPK
|
|
|
|
new_field.set_attributes_from_name('i')
|
|
|
|
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.alter_field(IntegerPK, old_field, new_field, strict=True)
|
|
|
|
|
2017-06-01 17:49:57 +08:00
|
|
|
def test_alter_int_pk_to_bigautofield_pk(self):
|
|
|
|
"""
|
|
|
|
Should be able to rename an IntegerField(primary_key=True) to
|
|
|
|
BigAutoField(primary_key=True).
|
|
|
|
"""
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.create_model(IntegerPK)
|
|
|
|
|
|
|
|
old_field = IntegerPK._meta.get_field('i')
|
|
|
|
new_field = BigAutoField(primary_key=True)
|
|
|
|
new_field.model = IntegerPK
|
|
|
|
new_field.set_attributes_from_name('i')
|
|
|
|
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.alter_field(IntegerPK, old_field, new_field, strict=True)
|
|
|
|
|
2015-06-02 07:27:28 +08:00
|
|
|
def test_alter_int_pk_to_int_unique(self):
|
|
|
|
"""
|
|
|
|
Should be able to rename an IntegerField(primary_key=True) to
|
|
|
|
IntegerField(unique=True).
|
|
|
|
"""
|
|
|
|
class IntegerUnique(Model):
|
|
|
|
i = IntegerField(unique=True)
|
|
|
|
j = IntegerField(primary_key=True)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
app_label = 'schema'
|
|
|
|
apps = new_apps
|
|
|
|
db_table = 'INTEGERPK'
|
|
|
|
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.create_model(IntegerPK)
|
|
|
|
|
|
|
|
# model requires a new PK
|
|
|
|
old_field = IntegerPK._meta.get_field('j')
|
|
|
|
new_field = IntegerField(primary_key=True)
|
|
|
|
new_field.model = IntegerPK
|
|
|
|
new_field.set_attributes_from_name('j')
|
|
|
|
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.alter_field(IntegerPK, old_field, new_field, strict=True)
|
|
|
|
|
|
|
|
old_field = IntegerPK._meta.get_field('i')
|
|
|
|
new_field = IntegerField(unique=True)
|
|
|
|
new_field.model = IntegerPK
|
|
|
|
new_field.set_attributes_from_name('i')
|
|
|
|
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.alter_field(IntegerPK, old_field, new_field, strict=True)
|
|
|
|
|
|
|
|
# Ensure unique constraint works.
|
|
|
|
IntegerUnique.objects.create(i=1, j=1)
|
|
|
|
with self.assertRaises(IntegrityError):
|
|
|
|
IntegerUnique.objects.create(i=1, j=2)
|
|
|
|
|
2012-08-02 22:08:39 +08:00
|
|
|
def test_rename(self):
|
|
|
|
"""
|
|
|
|
Tests simple altering of fields
|
|
|
|
"""
|
|
|
|
# Create the table
|
2013-05-18 17:48:46 +08:00
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.create_model(Author)
|
2012-08-02 22:08:39 +08:00
|
|
|
# Ensure the field is right to begin with
|
|
|
|
columns = self.column_classes(Author)
|
|
|
|
self.assertEqual(columns['name'][0], "CharField")
|
|
|
|
self.assertNotIn("display_name", columns)
|
|
|
|
# Alter the name field's name
|
2015-01-29 22:14:55 +08:00
|
|
|
old_field = Author._meta.get_field("name")
|
2012-08-02 22:08:39 +08:00
|
|
|
new_field = CharField(max_length=254)
|
|
|
|
new_field.set_attributes_from_name("display_name")
|
2013-05-18 17:48:46 +08:00
|
|
|
with connection.schema_editor() as editor:
|
2015-01-29 22:14:55 +08:00
|
|
|
editor.alter_field(Author, old_field, new_field, strict=True)
|
2012-08-02 22:08:39 +08:00
|
|
|
# Ensure the field is right afterwards
|
|
|
|
columns = self.column_classes(Author)
|
|
|
|
self.assertEqual(columns['display_name'][0], "CharField")
|
|
|
|
self.assertNotIn("name", columns)
|
|
|
|
|
2015-05-27 06:18:21 +08:00
|
|
|
@skipIfDBFeature('interprets_empty_strings_as_nulls')
|
|
|
|
def test_rename_keep_null_status(self):
|
|
|
|
"""
|
|
|
|
Renaming a field shouldn't affect the not null status.
|
|
|
|
"""
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.create_model(Note)
|
|
|
|
with self.assertRaises(IntegrityError):
|
|
|
|
Note.objects.create(info=None)
|
|
|
|
old_field = Note._meta.get_field("info")
|
|
|
|
new_field = TextField()
|
|
|
|
new_field.set_attributes_from_name("detail_info")
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.alter_field(Note, old_field, new_field, strict=True)
|
|
|
|
columns = self.column_classes(Note)
|
|
|
|
self.assertEqual(columns['detail_info'][0], "TextField")
|
|
|
|
self.assertNotIn("info", columns)
|
|
|
|
with self.assertRaises(IntegrityError):
|
|
|
|
NoteRename.objects.create(detail_info=None)
|
|
|
|
|
2015-01-29 22:14:55 +08:00
|
|
|
def _test_m2m_create(self, M2MFieldClass):
|
2012-09-08 02:39:22 +08:00
|
|
|
"""
|
|
|
|
Tests M2M fields on models during creation
|
|
|
|
"""
|
2015-01-29 22:14:55 +08:00
|
|
|
class LocalBookWithM2M(Model):
|
2015-07-22 22:43:21 +08:00
|
|
|
author = ForeignKey(Author, CASCADE)
|
2015-01-29 22:14:55 +08:00
|
|
|
title = CharField(max_length=100, db_index=True)
|
|
|
|
pub_date = DateTimeField()
|
|
|
|
tags = M2MFieldClass("TagM2MTest", related_name="books")
|
|
|
|
|
|
|
|
class Meta:
|
2015-02-06 07:40:36 +08:00
|
|
|
app_label = 'schema'
|
2015-01-29 22:14:55 +08:00
|
|
|
apps = new_apps
|
2015-10-16 03:36:28 +08:00
|
|
|
self.local_models = [LocalBookWithM2M]
|
2012-09-08 02:39:22 +08:00
|
|
|
# Create the tables
|
2013-05-18 17:48:46 +08:00
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.create_model(Author)
|
2013-08-23 19:07:43 +08:00
|
|
|
editor.create_model(TagM2MTest)
|
2015-01-29 22:14:55 +08:00
|
|
|
editor.create_model(LocalBookWithM2M)
|
2012-09-08 02:39:22 +08:00
|
|
|
# Ensure there is now an m2m table there
|
2015-02-26 22:19:17 +08:00
|
|
|
columns = self.column_classes(LocalBookWithM2M._meta.get_field("tags").remote_field.through)
|
2013-08-19 20:50:26 +08:00
|
|
|
self.assertEqual(columns['tagm2mtest_id'][0], "IntegerField")
|
2012-09-08 02:39:22 +08:00
|
|
|
|
2015-01-29 22:14:55 +08:00
|
|
|
def test_m2m_create(self):
|
|
|
|
self._test_m2m_create(ManyToManyField)
|
|
|
|
|
|
|
|
def test_m2m_create_custom(self):
|
|
|
|
self._test_m2m_create(CustomManyToManyField)
|
|
|
|
|
|
|
|
def test_m2m_create_inherited(self):
|
|
|
|
self._test_m2m_create(InheritedManyToManyField)
|
|
|
|
|
|
|
|
def _test_m2m_create_through(self, M2MFieldClass):
|
2014-03-09 07:57:25 +08:00
|
|
|
"""
|
|
|
|
Tests M2M fields on models during creation with through models
|
|
|
|
"""
|
2015-01-29 22:14:55 +08:00
|
|
|
class LocalTagThrough(Model):
|
2015-07-22 22:43:21 +08:00
|
|
|
book = ForeignKey("schema.LocalBookWithM2MThrough", CASCADE)
|
|
|
|
tag = ForeignKey("schema.TagM2MTest", CASCADE)
|
2015-01-29 22:14:55 +08:00
|
|
|
|
|
|
|
class Meta:
|
2015-02-06 07:40:36 +08:00
|
|
|
app_label = 'schema'
|
2015-01-29 22:14:55 +08:00
|
|
|
apps = new_apps
|
|
|
|
|
|
|
|
class LocalBookWithM2MThrough(Model):
|
|
|
|
tags = M2MFieldClass("TagM2MTest", related_name="books", through=LocalTagThrough)
|
|
|
|
|
|
|
|
class Meta:
|
2015-02-06 07:40:36 +08:00
|
|
|
app_label = 'schema'
|
2015-01-29 22:14:55 +08:00
|
|
|
apps = new_apps
|
|
|
|
|
|
|
|
self.local_models = [LocalTagThrough, LocalBookWithM2MThrough]
|
|
|
|
|
2014-03-09 07:57:25 +08:00
|
|
|
# Create the tables
|
|
|
|
with connection.schema_editor() as editor:
|
2015-01-29 22:14:55 +08:00
|
|
|
editor.create_model(LocalTagThrough)
|
2014-03-09 07:57:25 +08:00
|
|
|
editor.create_model(TagM2MTest)
|
2015-01-29 22:14:55 +08:00
|
|
|
editor.create_model(LocalBookWithM2MThrough)
|
2014-03-09 07:57:25 +08:00
|
|
|
# Ensure there is now an m2m table there
|
2015-01-29 22:14:55 +08:00
|
|
|
columns = self.column_classes(LocalTagThrough)
|
2014-03-09 07:57:25 +08:00
|
|
|
self.assertEqual(columns['book_id'][0], "IntegerField")
|
|
|
|
self.assertEqual(columns['tag_id'][0], "IntegerField")
|
|
|
|
|
2015-01-29 22:14:55 +08:00
|
|
|
def test_m2m_create_through(self):
|
|
|
|
self._test_m2m_create_through(ManyToManyField)
|
|
|
|
|
|
|
|
def test_m2m_create_through_custom(self):
|
|
|
|
self._test_m2m_create_through(CustomManyToManyField)
|
|
|
|
|
|
|
|
def test_m2m_create_through_inherited(self):
|
|
|
|
self._test_m2m_create_through(InheritedManyToManyField)
|
|
|
|
|
|
|
|
def _test_m2m(self, M2MFieldClass):
|
2012-08-02 22:08:39 +08:00
|
|
|
"""
|
|
|
|
Tests adding/removing M2M fields on models
|
|
|
|
"""
|
2015-01-29 22:14:55 +08:00
|
|
|
class LocalAuthorWithM2M(Model):
|
|
|
|
name = CharField(max_length=255)
|
|
|
|
|
|
|
|
class Meta:
|
2015-02-06 07:40:36 +08:00
|
|
|
app_label = 'schema'
|
2015-01-29 22:14:55 +08:00
|
|
|
apps = new_apps
|
|
|
|
|
|
|
|
self.local_models = [LocalAuthorWithM2M]
|
|
|
|
|
2012-08-02 22:08:39 +08:00
|
|
|
# Create the tables
|
2013-05-18 17:48:46 +08:00
|
|
|
with connection.schema_editor() as editor:
|
2015-01-29 22:14:55 +08:00
|
|
|
editor.create_model(LocalAuthorWithM2M)
|
2013-08-19 20:50:26 +08:00
|
|
|
editor.create_model(TagM2MTest)
|
2012-08-02 22:08:39 +08:00
|
|
|
# Create an M2M field
|
2015-01-29 22:14:55 +08:00
|
|
|
new_field = M2MFieldClass("schema.TagM2MTest", related_name="authors")
|
|
|
|
new_field.contribute_to_class(LocalAuthorWithM2M, "tags")
|
|
|
|
# Ensure there's no m2m table there
|
2016-01-17 19:26:39 +08:00
|
|
|
with self.assertRaises(DatabaseError):
|
|
|
|
self.column_classes(new_field.remote_field.through)
|
2015-01-29 22:14:55 +08:00
|
|
|
# Add the field
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.add_field(LocalAuthorWithM2M, new_field)
|
|
|
|
# Ensure there is now an m2m table there
|
2015-02-26 22:19:17 +08:00
|
|
|
columns = self.column_classes(new_field.remote_field.through)
|
2015-01-29 22:14:55 +08:00
|
|
|
self.assertEqual(columns['tagm2mtest_id'][0], "IntegerField")
|
2014-03-20 12:08:28 +08:00
|
|
|
|
2015-01-29 22:14:55 +08:00
|
|
|
# "Alter" the field. This should not rename the DB table to itself.
|
|
|
|
with connection.schema_editor() as editor:
|
2016-07-09 00:03:33 +08:00
|
|
|
editor.alter_field(LocalAuthorWithM2M, new_field, new_field, strict=True)
|
2012-08-02 22:08:39 +08:00
|
|
|
|
2015-01-29 22:14:55 +08:00
|
|
|
# Remove the M2M table again
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.remove_field(LocalAuthorWithM2M, new_field)
|
|
|
|
# Ensure there's no m2m table there
|
2016-01-17 19:26:39 +08:00
|
|
|
with self.assertRaises(DatabaseError):
|
|
|
|
self.column_classes(new_field.remote_field.through)
|
2015-01-29 22:14:55 +08:00
|
|
|
|
2015-11-14 06:13:27 +08:00
|
|
|
# Make sure the model state is coherent with the table one now that
|
|
|
|
# we've removed the tags field.
|
|
|
|
opts = LocalAuthorWithM2M._meta
|
|
|
|
opts.local_many_to_many.remove(new_field)
|
|
|
|
del new_apps.all_models['schema'][new_field.remote_field.through._meta.model_name]
|
|
|
|
opts._expire_cache()
|
2015-10-16 03:36:28 +08:00
|
|
|
|
2015-01-29 22:14:55 +08:00
|
|
|
def test_m2m(self):
|
|
|
|
self._test_m2m(ManyToManyField)
|
|
|
|
|
|
|
|
def test_m2m_custom(self):
|
|
|
|
self._test_m2m(CustomManyToManyField)
|
|
|
|
|
|
|
|
def test_m2m_inherited(self):
|
|
|
|
self._test_m2m(InheritedManyToManyField)
|
|
|
|
|
|
|
|
def _test_m2m_through_alter(self, M2MFieldClass):
|
2014-05-09 01:33:59 +08:00
|
|
|
"""
|
|
|
|
Tests altering M2Ms with explicit through models (should no-op)
|
|
|
|
"""
|
2015-01-29 22:14:55 +08:00
|
|
|
class LocalAuthorTag(Model):
|
2015-07-22 22:43:21 +08:00
|
|
|
author = ForeignKey("schema.LocalAuthorWithM2MThrough", CASCADE)
|
|
|
|
tag = ForeignKey("schema.TagM2MTest", CASCADE)
|
2015-01-29 22:14:55 +08:00
|
|
|
|
|
|
|
class Meta:
|
2015-02-06 07:40:36 +08:00
|
|
|
app_label = 'schema'
|
2015-01-29 22:14:55 +08:00
|
|
|
apps = new_apps
|
|
|
|
|
|
|
|
class LocalAuthorWithM2MThrough(Model):
|
|
|
|
name = CharField(max_length=255)
|
|
|
|
tags = M2MFieldClass("schema.TagM2MTest", related_name="authors", through=LocalAuthorTag)
|
|
|
|
|
|
|
|
class Meta:
|
2015-02-06 07:40:36 +08:00
|
|
|
app_label = 'schema'
|
2015-01-29 22:14:55 +08:00
|
|
|
apps = new_apps
|
|
|
|
|
|
|
|
self.local_models = [LocalAuthorTag, LocalAuthorWithM2MThrough]
|
|
|
|
|
2014-05-09 01:33:59 +08:00
|
|
|
# Create the tables
|
|
|
|
with connection.schema_editor() as editor:
|
2015-01-29 22:14:55 +08:00
|
|
|
editor.create_model(LocalAuthorTag)
|
|
|
|
editor.create_model(LocalAuthorWithM2MThrough)
|
2014-05-09 01:33:59 +08:00
|
|
|
editor.create_model(TagM2MTest)
|
|
|
|
# Ensure the m2m table is there
|
2015-01-29 22:14:55 +08:00
|
|
|
self.assertEqual(len(self.column_classes(LocalAuthorTag)), 3)
|
2014-05-09 01:33:59 +08:00
|
|
|
# "Alter" the field's blankness. This should not actually do anything.
|
2015-01-29 22:14:55 +08:00
|
|
|
old_field = LocalAuthorWithM2MThrough._meta.get_field("tags")
|
|
|
|
new_field = M2MFieldClass("schema.TagM2MTest", related_name="authors", through=LocalAuthorTag)
|
|
|
|
new_field.contribute_to_class(LocalAuthorWithM2MThrough, "tags")
|
2014-05-09 01:33:59 +08:00
|
|
|
with connection.schema_editor() as editor:
|
2016-07-09 00:03:33 +08:00
|
|
|
editor.alter_field(LocalAuthorWithM2MThrough, old_field, new_field, strict=True)
|
2014-05-09 01:33:59 +08:00
|
|
|
# Ensure the m2m table is still there
|
2015-01-29 22:14:55 +08:00
|
|
|
self.assertEqual(len(self.column_classes(LocalAuthorTag)), 3)
|
2014-05-09 01:33:59 +08:00
|
|
|
|
2015-01-29 22:14:55 +08:00
|
|
|
def test_m2m_through_alter(self):
|
|
|
|
self._test_m2m_through_alter(ManyToManyField)
|
|
|
|
|
|
|
|
def test_m2m_through_alter_custom(self):
|
|
|
|
self._test_m2m_through_alter(CustomManyToManyField)
|
|
|
|
|
|
|
|
def test_m2m_through_alter_inherited(self):
|
|
|
|
self._test_m2m_through_alter(InheritedManyToManyField)
|
|
|
|
|
|
|
|
def _test_m2m_repoint(self, M2MFieldClass):
|
2012-09-08 02:39:22 +08:00
|
|
|
"""
|
|
|
|
Tests repointing M2M fields
|
|
|
|
"""
|
2015-01-29 22:14:55 +08:00
|
|
|
class LocalBookWithM2M(Model):
|
2015-07-22 22:43:21 +08:00
|
|
|
author = ForeignKey(Author, CASCADE)
|
2015-01-29 22:14:55 +08:00
|
|
|
title = CharField(max_length=100, db_index=True)
|
|
|
|
pub_date = DateTimeField()
|
|
|
|
tags = M2MFieldClass("TagM2MTest", related_name="books")
|
|
|
|
|
|
|
|
class Meta:
|
2015-02-06 07:40:36 +08:00
|
|
|
app_label = 'schema'
|
2015-01-29 22:14:55 +08:00
|
|
|
apps = new_apps
|
2015-10-16 03:36:28 +08:00
|
|
|
self.local_models = [LocalBookWithM2M]
|
2012-09-08 02:39:22 +08:00
|
|
|
# Create the tables
|
2013-05-18 17:48:46 +08:00
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.create_model(Author)
|
2015-01-29 22:14:55 +08:00
|
|
|
editor.create_model(LocalBookWithM2M)
|
2013-08-19 20:50:26 +08:00
|
|
|
editor.create_model(TagM2MTest)
|
2013-05-18 17:48:46 +08:00
|
|
|
editor.create_model(UniqueTest)
|
2013-08-19 20:50:26 +08:00
|
|
|
# Ensure the M2M exists and points to TagM2MTest
|
2012-09-08 02:39:22 +08:00
|
|
|
if connection.features.supports_foreign_keys:
|
2017-05-24 22:43:56 +08:00
|
|
|
self.assertForeignKeyExists(
|
|
|
|
LocalBookWithM2M._meta.get_field("tags").remote_field.through,
|
|
|
|
'tagm2mtest_id',
|
|
|
|
'schema_tagm2mtest',
|
|
|
|
)
|
2012-09-08 02:39:22 +08:00
|
|
|
# Repoint the M2M
|
2015-01-29 22:14:55 +08:00
|
|
|
old_field = LocalBookWithM2M._meta.get_field("tags")
|
|
|
|
new_field = M2MFieldClass(UniqueTest)
|
|
|
|
new_field.contribute_to_class(LocalBookWithM2M, "uniques")
|
|
|
|
with connection.schema_editor() as editor:
|
2016-07-09 00:03:33 +08:00
|
|
|
editor.alter_field(LocalBookWithM2M, old_field, new_field, strict=True)
|
2015-01-29 22:14:55 +08:00
|
|
|
# Ensure old M2M is gone
|
2016-01-17 19:26:39 +08:00
|
|
|
with self.assertRaises(DatabaseError):
|
|
|
|
self.column_classes(LocalBookWithM2M._meta.get_field("tags").remote_field.through)
|
2015-10-16 03:36:28 +08:00
|
|
|
|
|
|
|
# This model looks like the new model and is used for teardown.
|
2015-11-14 06:13:27 +08:00
|
|
|
opts = LocalBookWithM2M._meta
|
|
|
|
opts.local_many_to_many.remove(old_field)
|
2015-01-29 22:14:55 +08:00
|
|
|
# Ensure the new M2M exists and points to UniqueTest
|
|
|
|
if connection.features.supports_foreign_keys:
|
2017-05-24 22:43:56 +08:00
|
|
|
self.assertForeignKeyExists(new_field.remote_field.through, 'uniquetest_id', 'schema_uniquetest')
|
2015-01-29 22:14:55 +08:00
|
|
|
|
|
|
|
def test_m2m_repoint(self):
|
|
|
|
self._test_m2m_repoint(ManyToManyField)
|
|
|
|
|
|
|
|
def test_m2m_repoint_custom(self):
|
|
|
|
self._test_m2m_repoint(CustomManyToManyField)
|
|
|
|
|
|
|
|
def test_m2m_repoint_inherited(self):
|
|
|
|
self._test_m2m_repoint(InheritedManyToManyField)
|
2012-09-08 02:39:22 +08:00
|
|
|
|
2015-08-08 23:27:06 +08:00
|
|
|
@skipUnlessDBFeature('supports_column_check_constraints')
|
2012-09-08 03:40:59 +08:00
|
|
|
def test_check_constraints(self):
|
|
|
|
"""
|
|
|
|
Tests creating/deleting CHECK constraints
|
|
|
|
"""
|
|
|
|
# Create the tables
|
2013-05-18 17:48:46 +08:00
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.create_model(Author)
|
2012-09-08 03:40:59 +08:00
|
|
|
# Ensure the constraint exists
|
2014-01-09 23:05:15 +08:00
|
|
|
constraints = self.get_constraints(Author._meta.db_table)
|
2012-09-08 03:40:59 +08:00
|
|
|
for name, details in constraints.items():
|
2013-07-03 01:02:20 +08:00
|
|
|
if details['columns'] == ["height"] and details['check']:
|
2012-09-08 03:40:59 +08:00
|
|
|
break
|
|
|
|
else:
|
|
|
|
self.fail("No check constraint for height found")
|
|
|
|
# Alter the column to remove it
|
2015-01-29 22:14:55 +08:00
|
|
|
old_field = Author._meta.get_field("height")
|
2012-09-08 03:40:59 +08:00
|
|
|
new_field = IntegerField(null=True, blank=True)
|
|
|
|
new_field.set_attributes_from_name("height")
|
2013-05-18 17:48:46 +08:00
|
|
|
with connection.schema_editor() as editor:
|
2015-01-29 22:14:55 +08:00
|
|
|
editor.alter_field(Author, old_field, new_field, strict=True)
|
2014-01-09 23:05:15 +08:00
|
|
|
constraints = self.get_constraints(Author._meta.db_table)
|
2012-09-08 03:40:59 +08:00
|
|
|
for name, details in constraints.items():
|
2013-07-03 01:02:20 +08:00
|
|
|
if details['columns'] == ["height"] and details['check']:
|
2012-09-08 03:40:59 +08:00
|
|
|
self.fail("Check constraint for height found")
|
|
|
|
# Alter the column to re-add it
|
2015-01-29 22:14:55 +08:00
|
|
|
new_field2 = Author._meta.get_field("height")
|
2013-05-18 17:48:46 +08:00
|
|
|
with connection.schema_editor() as editor:
|
2015-01-29 22:14:55 +08:00
|
|
|
editor.alter_field(Author, new_field, new_field2, strict=True)
|
2014-01-09 23:05:15 +08:00
|
|
|
constraints = self.get_constraints(Author._meta.db_table)
|
2012-09-08 03:40:59 +08:00
|
|
|
for name, details in constraints.items():
|
2013-07-03 01:02:20 +08:00
|
|
|
if details['columns'] == ["height"] and details['check']:
|
2012-09-08 03:40:59 +08:00
|
|
|
break
|
|
|
|
else:
|
|
|
|
self.fail("No check constraint for height found")
|
|
|
|
|
2012-08-02 22:08:39 +08:00
|
|
|
def test_unique(self):
|
|
|
|
"""
|
|
|
|
Tests removing and adding unique constraints to a single column.
|
|
|
|
"""
|
|
|
|
# Create the table
|
2013-05-18 17:48:46 +08:00
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.create_model(Tag)
|
2012-08-02 22:08:39 +08:00
|
|
|
# Ensure the field is unique to begin with
|
|
|
|
Tag.objects.create(title="foo", slug="foo")
|
2016-01-17 19:26:39 +08:00
|
|
|
with self.assertRaises(IntegrityError):
|
|
|
|
Tag.objects.create(title="bar", slug="foo")
|
2013-05-18 17:48:46 +08:00
|
|
|
Tag.objects.all().delete()
|
2012-08-02 22:08:39 +08:00
|
|
|
# Alter the slug field to be non-unique
|
2015-01-29 22:14:55 +08:00
|
|
|
old_field = Tag._meta.get_field("slug")
|
2012-08-02 22:08:39 +08:00
|
|
|
new_field = SlugField(unique=False)
|
|
|
|
new_field.set_attributes_from_name("slug")
|
2013-05-18 17:48:46 +08:00
|
|
|
with connection.schema_editor() as editor:
|
2015-01-29 22:14:55 +08:00
|
|
|
editor.alter_field(Tag, old_field, new_field, strict=True)
|
2012-08-02 22:08:39 +08:00
|
|
|
# Ensure the field is no longer unique
|
|
|
|
Tag.objects.create(title="foo", slug="foo")
|
|
|
|
Tag.objects.create(title="bar", slug="foo")
|
2013-05-18 17:48:46 +08:00
|
|
|
Tag.objects.all().delete()
|
2013-04-19 16:01:45 +08:00
|
|
|
# Alter the slug field to be unique
|
2015-01-29 22:14:55 +08:00
|
|
|
new_field2 = SlugField(unique=True)
|
|
|
|
new_field2.set_attributes_from_name("slug")
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.alter_field(Tag, new_field, new_field2, strict=True)
|
2012-08-02 22:08:39 +08:00
|
|
|
# Ensure the field is unique again
|
|
|
|
Tag.objects.create(title="foo", slug="foo")
|
2016-01-17 19:26:39 +08:00
|
|
|
with self.assertRaises(IntegrityError):
|
|
|
|
Tag.objects.create(title="bar", slug="foo")
|
2013-05-18 17:48:46 +08:00
|
|
|
Tag.objects.all().delete()
|
2012-08-18 21:00:42 +08:00
|
|
|
# Rename the field
|
2015-01-29 22:14:55 +08:00
|
|
|
new_field3 = SlugField(unique=True)
|
|
|
|
new_field3.set_attributes_from_name("slug2")
|
2013-05-18 17:48:46 +08:00
|
|
|
with connection.schema_editor() as editor:
|
2015-01-29 22:14:55 +08:00
|
|
|
editor.alter_field(Tag, new_field2, new_field3, strict=True)
|
2012-08-18 21:00:42 +08:00
|
|
|
# Ensure the field is still unique
|
|
|
|
TagUniqueRename.objects.create(title="foo", slug2="foo")
|
2016-01-17 19:26:39 +08:00
|
|
|
with self.assertRaises(IntegrityError):
|
|
|
|
TagUniqueRename.objects.create(title="bar", slug2="foo")
|
2013-05-18 17:48:46 +08:00
|
|
|
Tag.objects.all().delete()
|
2012-08-10 19:38:18 +08:00
|
|
|
|
|
|
|
def test_unique_together(self):
|
|
|
|
"""
|
|
|
|
Tests removing and adding unique_together constraints on a model.
|
|
|
|
"""
|
|
|
|
# Create the table
|
2013-05-18 17:48:46 +08:00
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.create_model(UniqueTest)
|
2012-08-10 19:38:18 +08:00
|
|
|
# Ensure the fields are unique to begin with
|
|
|
|
UniqueTest.objects.create(year=2012, slug="foo")
|
|
|
|
UniqueTest.objects.create(year=2011, slug="foo")
|
|
|
|
UniqueTest.objects.create(year=2011, slug="bar")
|
2016-01-17 19:26:39 +08:00
|
|
|
with self.assertRaises(IntegrityError):
|
|
|
|
UniqueTest.objects.create(year=2012, slug="foo")
|
2013-05-18 17:48:46 +08:00
|
|
|
UniqueTest.objects.all().delete()
|
2014-04-27 01:18:45 +08:00
|
|
|
# Alter the model to its non-unique-together companion
|
2013-05-18 17:48:46 +08:00
|
|
|
with connection.schema_editor() as editor:
|
2015-01-29 22:14:55 +08:00
|
|
|
editor.alter_unique_together(UniqueTest, UniqueTest._meta.unique_together, [])
|
2012-08-10 19:38:18 +08:00
|
|
|
# Ensure the fields are no longer unique
|
|
|
|
UniqueTest.objects.create(year=2012, slug="foo")
|
|
|
|
UniqueTest.objects.create(year=2012, slug="foo")
|
2013-05-18 17:48:46 +08:00
|
|
|
UniqueTest.objects.all().delete()
|
2012-08-10 19:38:18 +08:00
|
|
|
# Alter it back
|
2015-01-29 22:14:55 +08:00
|
|
|
new_field2 = SlugField(unique=True)
|
|
|
|
new_field2.set_attributes_from_name("slug")
|
2013-05-18 17:48:46 +08:00
|
|
|
with connection.schema_editor() as editor:
|
2015-01-29 22:14:55 +08:00
|
|
|
editor.alter_unique_together(UniqueTest, [], UniqueTest._meta.unique_together)
|
2012-08-10 19:38:18 +08:00
|
|
|
# Ensure the fields are unique again
|
|
|
|
UniqueTest.objects.create(year=2012, slug="foo")
|
2016-01-17 19:26:39 +08:00
|
|
|
with self.assertRaises(IntegrityError):
|
|
|
|
UniqueTest.objects.create(year=2012, slug="foo")
|
2013-05-18 17:48:46 +08:00
|
|
|
UniqueTest.objects.all().delete()
|
2012-08-10 22:03:18 +08:00
|
|
|
|
2015-05-13 23:40:57 +08:00
|
|
|
def test_unique_together_with_fk(self):
|
|
|
|
"""
|
|
|
|
Tests removing and adding unique_together constraints that include
|
|
|
|
a foreign key.
|
|
|
|
"""
|
|
|
|
# Create the table
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.create_model(Author)
|
|
|
|
editor.create_model(Book)
|
|
|
|
# Ensure the fields are unique to begin with
|
|
|
|
self.assertEqual(Book._meta.unique_together, ())
|
|
|
|
# Add the unique_together constraint
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.alter_unique_together(Book, [], [['author', 'title']])
|
|
|
|
# Alter it back
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.alter_unique_together(Book, [['author', 'title']], [])
|
2015-06-12 14:22:08 +08:00
|
|
|
|
|
|
|
def test_unique_together_with_fk_with_existing_index(self):
|
|
|
|
"""
|
|
|
|
Tests removing and adding unique_together constraints that include
|
|
|
|
a foreign key, where the foreign key is added after the model is
|
|
|
|
created.
|
|
|
|
"""
|
|
|
|
# Create the tables
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.create_model(Author)
|
|
|
|
editor.create_model(BookWithoutAuthor)
|
2015-07-22 22:43:21 +08:00
|
|
|
new_field = ForeignKey(Author, CASCADE)
|
2015-06-12 14:22:08 +08:00
|
|
|
new_field.set_attributes_from_name('author')
|
|
|
|
editor.add_field(BookWithoutAuthor, new_field)
|
|
|
|
# Ensure the fields aren't unique to begin with
|
|
|
|
self.assertEqual(Book._meta.unique_together, ())
|
|
|
|
# Add the unique_together constraint
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.alter_unique_together(Book, [], [['author', 'title']])
|
|
|
|
# Alter it back
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.alter_unique_together(Book, [['author', 'title']], [])
|
2015-05-13 23:40:57 +08:00
|
|
|
|
2013-07-02 18:43:44 +08:00
|
|
|
def test_index_together(self):
|
|
|
|
"""
|
|
|
|
Tests removing and adding index_together constraints on a model.
|
|
|
|
"""
|
|
|
|
# Create the table
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.create_model(Tag)
|
|
|
|
# Ensure there's no index on the year/slug columns first
|
|
|
|
self.assertEqual(
|
|
|
|
False,
|
|
|
|
any(
|
|
|
|
c["index"]
|
2014-01-09 23:05:15 +08:00
|
|
|
for c in self.get_constraints("schema_tag").values()
|
2013-07-03 01:02:20 +08:00
|
|
|
if c['columns'] == ["slug", "title"]
|
2013-07-02 18:43:44 +08:00
|
|
|
),
|
|
|
|
)
|
|
|
|
# Alter the model to add an index
|
|
|
|
with connection.schema_editor() as editor:
|
2015-01-29 22:14:55 +08:00
|
|
|
editor.alter_index_together(Tag, [], [("slug", "title")])
|
2013-07-02 18:43:44 +08:00
|
|
|
# Ensure there is now an index
|
|
|
|
self.assertEqual(
|
|
|
|
True,
|
|
|
|
any(
|
|
|
|
c["index"]
|
2014-01-09 23:05:15 +08:00
|
|
|
for c in self.get_constraints("schema_tag").values()
|
2013-07-03 01:02:20 +08:00
|
|
|
if c['columns'] == ["slug", "title"]
|
2013-07-02 18:43:44 +08:00
|
|
|
),
|
|
|
|
)
|
|
|
|
# Alter it back
|
2015-01-29 22:14:55 +08:00
|
|
|
new_field2 = SlugField(unique=True)
|
|
|
|
new_field2.set_attributes_from_name("slug")
|
2013-07-02 18:43:44 +08:00
|
|
|
with connection.schema_editor() as editor:
|
2015-01-29 22:14:55 +08:00
|
|
|
editor.alter_index_together(Tag, [("slug", "title")], [])
|
2013-07-02 18:43:44 +08:00
|
|
|
# Ensure there's no index
|
|
|
|
self.assertEqual(
|
|
|
|
False,
|
|
|
|
any(
|
|
|
|
c["index"]
|
2014-01-09 23:05:15 +08:00
|
|
|
for c in self.get_constraints("schema_tag").values()
|
2013-07-03 01:02:20 +08:00
|
|
|
if c['columns'] == ["slug", "title"]
|
2013-07-02 18:43:44 +08:00
|
|
|
),
|
|
|
|
)
|
|
|
|
|
2015-05-13 23:40:57 +08:00
|
|
|
def test_index_together_with_fk(self):
|
|
|
|
"""
|
|
|
|
Tests removing and adding index_together constraints that include
|
|
|
|
a foreign key.
|
|
|
|
"""
|
|
|
|
# Create the table
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.create_model(Author)
|
|
|
|
editor.create_model(Book)
|
|
|
|
# Ensure the fields are unique to begin with
|
|
|
|
self.assertEqual(Book._meta.index_together, ())
|
|
|
|
# Add the unique_together constraint
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.alter_index_together(Book, [], [['author', 'title']])
|
|
|
|
# Alter it back
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.alter_index_together(Book, [['author', 'title']], [])
|
|
|
|
|
2013-08-11 21:23:31 +08:00
|
|
|
def test_create_index_together(self):
|
|
|
|
"""
|
|
|
|
Tests creating models with index_together already defined
|
|
|
|
"""
|
|
|
|
# Create the table
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.create_model(TagIndexed)
|
|
|
|
# Ensure there is an index
|
|
|
|
self.assertEqual(
|
|
|
|
True,
|
|
|
|
any(
|
|
|
|
c["index"]
|
2014-01-09 23:05:15 +08:00
|
|
|
for c in self.get_constraints("schema_tagindexed").values()
|
2013-08-11 21:23:31 +08:00
|
|
|
if c['columns'] == ["slug", "title"]
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
2012-08-10 22:03:18 +08:00
|
|
|
def test_db_table(self):
|
|
|
|
"""
|
|
|
|
Tests renaming of the table
|
|
|
|
"""
|
|
|
|
# Create the table
|
2013-05-18 17:48:46 +08:00
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.create_model(Author)
|
2012-08-10 22:03:18 +08:00
|
|
|
# Ensure the table is there to begin with
|
|
|
|
columns = self.column_classes(Author)
|
|
|
|
self.assertEqual(columns['name'][0], "CharField")
|
|
|
|
# Alter the table
|
2013-05-18 17:48:46 +08:00
|
|
|
with connection.schema_editor() as editor:
|
2015-01-29 22:14:55 +08:00
|
|
|
editor.alter_db_table(Author, "schema_author", "schema_otherauthor")
|
2012-08-10 22:03:18 +08:00
|
|
|
# Ensure the table is there afterwards
|
|
|
|
Author._meta.db_table = "schema_otherauthor"
|
|
|
|
columns = self.column_classes(Author)
|
|
|
|
self.assertEqual(columns['name'][0], "CharField")
|
|
|
|
# Alter the table again
|
2013-05-18 17:48:46 +08:00
|
|
|
with connection.schema_editor() as editor:
|
2015-01-29 22:14:55 +08:00
|
|
|
editor.alter_db_table(Author, "schema_otherauthor", "schema_author")
|
2012-08-10 22:03:18 +08:00
|
|
|
# Ensure the table is still there
|
|
|
|
Author._meta.db_table = "schema_author"
|
|
|
|
columns = self.column_classes(Author)
|
|
|
|
self.assertEqual(columns['name'][0], "CharField")
|
2012-08-31 06:11:56 +08:00
|
|
|
|
2016-06-26 00:32:56 +08:00
|
|
|
def test_add_remove_index(self):
|
|
|
|
"""
|
|
|
|
Tests index addition and removal
|
|
|
|
"""
|
|
|
|
# Create the table
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.create_model(Author)
|
|
|
|
# Ensure the table is there and has no index
|
|
|
|
self.assertNotIn('title', self.get_indexes(Author._meta.db_table))
|
|
|
|
# Add the index
|
|
|
|
index = Index(fields=['name'], name='author_title_idx')
|
|
|
|
with connection.schema_editor() as editor:
|
2016-07-07 00:57:17 +08:00
|
|
|
editor.add_index(Author, index)
|
2016-06-26 00:32:56 +08:00
|
|
|
self.assertIn('name', self.get_indexes(Author._meta.db_table))
|
|
|
|
# Drop the index
|
|
|
|
with connection.schema_editor() as editor:
|
2016-07-07 00:57:17 +08:00
|
|
|
editor.remove_index(Author, index)
|
2016-06-26 00:32:56 +08:00
|
|
|
self.assertNotIn('name', self.get_indexes(Author._meta.db_table))
|
|
|
|
|
2017-04-07 21:54:40 +08:00
|
|
|
def test_remove_db_index_doesnt_remove_custom_indexes(self):
|
|
|
|
"""
|
|
|
|
Changing db_index to False doesn't remove indexes from Meta.indexes.
|
|
|
|
"""
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.create_model(AuthorWithIndexedName)
|
|
|
|
# Ensure the table has its index
|
|
|
|
self.assertIn('name', self.get_indexes(AuthorWithIndexedName._meta.db_table))
|
|
|
|
|
|
|
|
# Add the custom index
|
|
|
|
index = Index(fields=['-name'], name='author_name_idx')
|
|
|
|
author_index_name = index.name
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
db_index_name = editor._create_index_name(
|
|
|
|
model=AuthorWithIndexedName,
|
|
|
|
column_names=('name',),
|
|
|
|
)
|
|
|
|
if connection.features.uppercases_column_names:
|
|
|
|
author_index_name = author_index_name.upper()
|
|
|
|
db_index_name = db_index_name.upper()
|
|
|
|
try:
|
|
|
|
AuthorWithIndexedName._meta.indexes = [index]
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.add_index(AuthorWithIndexedName, index)
|
|
|
|
old_constraints = self.get_constraints(AuthorWithIndexedName._meta.db_table)
|
|
|
|
self.assertIn(author_index_name, old_constraints)
|
|
|
|
self.assertIn(db_index_name, old_constraints)
|
|
|
|
# Change name field to db_index=False
|
|
|
|
old_field = AuthorWithIndexedName._meta.get_field('name')
|
|
|
|
new_field = CharField(max_length=255)
|
|
|
|
new_field.set_attributes_from_name('name')
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.alter_field(AuthorWithIndexedName, old_field, new_field, strict=True)
|
|
|
|
new_constraints = self.get_constraints(AuthorWithIndexedName._meta.db_table)
|
|
|
|
self.assertNotIn(db_index_name, new_constraints)
|
|
|
|
# The index from Meta.indexes is still in the database.
|
|
|
|
self.assertIn(author_index_name, new_constraints)
|
2017-05-02 10:18:18 +08:00
|
|
|
# Drop the index
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.remove_index(AuthorWithIndexedName, index)
|
2017-04-07 21:54:40 +08:00
|
|
|
finally:
|
|
|
|
AuthorWithIndexedName._meta.indexes = []
|
|
|
|
|
2016-07-22 20:52:44 +08:00
|
|
|
def test_order_index(self):
|
|
|
|
"""
|
|
|
|
Indexes defined with ordering (ASC/DESC) defined on column
|
|
|
|
"""
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.create_model(Author)
|
|
|
|
# The table doesn't have an index
|
|
|
|
self.assertNotIn('title', self.get_indexes(Author._meta.db_table))
|
|
|
|
index_name = 'author_name_idx'
|
|
|
|
# Add the index
|
|
|
|
index = Index(fields=['name', '-weight'], name=index_name)
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.add_index(Author, index)
|
|
|
|
if connection.features.supports_index_column_ordering:
|
|
|
|
if connection.features.uppercases_column_names:
|
|
|
|
index_name = index_name.upper()
|
|
|
|
self.assertIndexOrder(Author._meta.db_table, index_name, ['ASC', 'DESC'])
|
2017-05-02 10:18:18 +08:00
|
|
|
# Drop the index
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.remove_index(Author, index)
|
2016-07-22 20:52:44 +08:00
|
|
|
|
2012-08-31 06:11:56 +08:00
|
|
|
def test_indexes(self):
|
|
|
|
"""
|
|
|
|
Tests creation/altering of indexes
|
|
|
|
"""
|
|
|
|
# Create the table
|
2013-05-18 17:48:46 +08:00
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.create_model(Author)
|
|
|
|
editor.create_model(Book)
|
2012-08-31 06:11:56 +08:00
|
|
|
# Ensure the table is there and has the right index
|
|
|
|
self.assertIn(
|
|
|
|
"title",
|
2014-01-09 23:05:15 +08:00
|
|
|
self.get_indexes(Book._meta.db_table),
|
2012-08-31 06:11:56 +08:00
|
|
|
)
|
|
|
|
# Alter to remove the index
|
2015-01-29 22:14:55 +08:00
|
|
|
old_field = Book._meta.get_field("title")
|
2012-08-31 06:11:56 +08:00
|
|
|
new_field = CharField(max_length=100, db_index=False)
|
|
|
|
new_field.set_attributes_from_name("title")
|
2013-05-18 17:48:46 +08:00
|
|
|
with connection.schema_editor() as editor:
|
2015-01-29 22:14:55 +08:00
|
|
|
editor.alter_field(Book, old_field, new_field, strict=True)
|
2012-08-31 06:11:56 +08:00
|
|
|
# Ensure the table is there and has no index
|
|
|
|
self.assertNotIn(
|
|
|
|
"title",
|
2014-01-09 23:05:15 +08:00
|
|
|
self.get_indexes(Book._meta.db_table),
|
2012-08-31 06:11:56 +08:00
|
|
|
)
|
|
|
|
# Alter to re-add the index
|
2015-01-29 22:14:55 +08:00
|
|
|
new_field2 = Book._meta.get_field("title")
|
2013-05-18 17:48:46 +08:00
|
|
|
with connection.schema_editor() as editor:
|
2015-01-29 22:14:55 +08:00
|
|
|
editor.alter_field(Book, new_field, new_field2, strict=True)
|
2012-08-31 06:11:56 +08:00
|
|
|
# Ensure the table is there and has the index again
|
|
|
|
self.assertIn(
|
|
|
|
"title",
|
2014-01-09 23:05:15 +08:00
|
|
|
self.get_indexes(Book._meta.db_table),
|
2012-08-31 06:11:56 +08:00
|
|
|
)
|
|
|
|
# Add a unique column, verify that creates an implicit index
|
2015-01-29 22:14:55 +08:00
|
|
|
new_field3 = BookWithSlug._meta.get_field("slug")
|
2013-05-18 17:48:46 +08:00
|
|
|
with connection.schema_editor() as editor:
|
2015-01-29 22:14:55 +08:00
|
|
|
editor.add_field(Book, new_field3)
|
2012-08-31 06:11:56 +08:00
|
|
|
self.assertIn(
|
|
|
|
"slug",
|
2014-01-09 23:05:15 +08:00
|
|
|
self.get_indexes(Book._meta.db_table),
|
2012-08-31 06:11:56 +08:00
|
|
|
)
|
|
|
|
# Remove the unique, check the index goes with it
|
2015-01-29 22:14:55 +08:00
|
|
|
new_field4 = CharField(max_length=20, unique=False)
|
|
|
|
new_field4.set_attributes_from_name("slug")
|
2013-05-18 17:48:46 +08:00
|
|
|
with connection.schema_editor() as editor:
|
2015-01-29 22:14:55 +08:00
|
|
|
editor.alter_field(BookWithSlug, new_field3, new_field4, strict=True)
|
2012-08-31 06:11:56 +08:00
|
|
|
self.assertNotIn(
|
|
|
|
"slug",
|
2014-01-09 23:05:15 +08:00
|
|
|
self.get_indexes(Book._meta.db_table),
|
2012-08-31 06:11:56 +08:00
|
|
|
)
|
2012-09-05 00:53:31 +08:00
|
|
|
|
2017-05-23 23:02:40 +08:00
|
|
|
def test_text_field_with_db_index(self):
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.create_model(AuthorTextFieldWithIndex)
|
|
|
|
# The text_field index is present if the database supports it.
|
|
|
|
assertion = self.assertIn if connection.features.supports_index_on_text_field else self.assertNotIn
|
|
|
|
assertion('text_field', self.get_indexes(AuthorTextFieldWithIndex._meta.db_table))
|
|
|
|
|
2012-09-05 00:53:31 +08:00
|
|
|
def test_primary_key(self):
|
|
|
|
"""
|
|
|
|
Tests altering of the primary key
|
|
|
|
"""
|
|
|
|
# Create the table
|
2013-05-18 17:48:46 +08:00
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.create_model(Tag)
|
2012-09-05 00:53:31 +08:00
|
|
|
# Ensure the table is there and has the right PK
|
2016-08-20 18:14:02 +08:00
|
|
|
self.assertEqual(self.get_primary_key(Tag._meta.db_table), 'id')
|
2012-09-05 00:53:31 +08:00
|
|
|
# Alter to change the PK
|
2015-01-29 22:14:55 +08:00
|
|
|
id_field = Tag._meta.get_field("id")
|
|
|
|
old_field = Tag._meta.get_field("slug")
|
2012-09-05 00:53:31 +08:00
|
|
|
new_field = SlugField(primary_key=True)
|
|
|
|
new_field.set_attributes_from_name("slug")
|
2013-12-11 22:19:05 +08:00
|
|
|
new_field.model = Tag
|
2013-05-18 17:48:46 +08:00
|
|
|
with connection.schema_editor() as editor:
|
2015-01-29 22:14:55 +08:00
|
|
|
editor.remove_field(Tag, id_field)
|
|
|
|
editor.alter_field(Tag, old_field, new_field)
|
2012-09-05 00:53:31 +08:00
|
|
|
# Ensure the PK changed
|
|
|
|
self.assertNotIn(
|
|
|
|
'id',
|
2014-01-09 23:05:15 +08:00
|
|
|
self.get_indexes(Tag._meta.db_table),
|
2012-09-05 00:53:31 +08:00
|
|
|
)
|
2016-08-20 18:14:02 +08:00
|
|
|
self.assertEqual(self.get_primary_key(Tag._meta.db_table), 'slug')
|
2013-09-07 00:46:33 +08:00
|
|
|
|
|
|
|
def test_context_manager_exit(self):
|
|
|
|
"""
|
|
|
|
Ensures transaction is correctly closed when an error occurs
|
|
|
|
inside a SchemaEditor context.
|
|
|
|
"""
|
|
|
|
class SomeError(Exception):
|
|
|
|
pass
|
|
|
|
try:
|
2013-10-19 20:31:38 +08:00
|
|
|
with connection.schema_editor():
|
2013-09-07 00:46:33 +08:00
|
|
|
raise SomeError
|
|
|
|
except SomeError:
|
|
|
|
self.assertFalse(connection.in_atomic_block)
|
2013-11-23 06:31:50 +08:00
|
|
|
|
2016-12-24 11:34:52 +08:00
|
|
|
@skipIfDBFeature('can_rollback_ddl')
|
|
|
|
def test_unsupported_transactional_ddl_disallowed(self):
|
|
|
|
message = (
|
|
|
|
"Executing DDL statements while in a transaction on databases "
|
|
|
|
"that can't perform a rollback is prohibited."
|
|
|
|
)
|
|
|
|
with atomic(), connection.schema_editor() as editor:
|
|
|
|
with self.assertRaisesMessage(TransactionManagementError, message):
|
|
|
|
editor.execute(editor.sql_create_table % {'table': 'foo', 'definition': ''})
|
|
|
|
|
2015-08-08 23:27:06 +08:00
|
|
|
@skipUnlessDBFeature('supports_foreign_keys')
|
2013-11-23 06:31:50 +08:00
|
|
|
def test_foreign_key_index_long_names_regression(self):
|
|
|
|
"""
|
2014-07-21 17:50:21 +08:00
|
|
|
Regression test for #21497.
|
|
|
|
Only affects databases that supports foreign keys.
|
2013-11-23 06:31:50 +08:00
|
|
|
"""
|
|
|
|
# Create the table
|
|
|
|
with connection.schema_editor() as editor:
|
2014-07-21 17:50:21 +08:00
|
|
|
editor.create_model(AuthorWithEvenLongerName)
|
2013-11-23 06:31:50 +08:00
|
|
|
editor.create_model(BookWithLongName)
|
2013-11-25 21:11:50 +08:00
|
|
|
# Find the properly shortened column name
|
|
|
|
column_name = connection.ops.quote_name("author_foreign_key_with_really_long_field_name_id")
|
2013-11-25 23:21:25 +08:00
|
|
|
column_name = column_name[1:-1].lower() # unquote, and, for Oracle, un-upcase
|
2013-11-25 21:11:50 +08:00
|
|
|
# Ensure the table is there and has an index on the column
|
2013-11-23 06:31:50 +08:00
|
|
|
self.assertIn(
|
2013-11-25 21:11:50 +08:00
|
|
|
column_name,
|
2014-01-09 23:05:15 +08:00
|
|
|
self.get_indexes(BookWithLongName._meta.db_table),
|
2013-11-23 06:31:50 +08:00
|
|
|
)
|
2013-12-23 03:44:49 +08:00
|
|
|
|
2015-08-08 23:27:06 +08:00
|
|
|
@skipUnlessDBFeature('supports_foreign_keys')
|
2014-07-21 17:50:21 +08:00
|
|
|
def test_add_foreign_key_long_names(self):
|
|
|
|
"""
|
|
|
|
Regression test for #23009.
|
|
|
|
Only affects databases that supports foreign keys.
|
|
|
|
"""
|
|
|
|
# Create the initial tables
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.create_model(AuthorWithEvenLongerName)
|
|
|
|
editor.create_model(BookWithLongName)
|
|
|
|
# Add a second FK, this would fail due to long ref name before the fix
|
2015-07-22 22:43:21 +08:00
|
|
|
new_field = ForeignKey(AuthorWithEvenLongerName, CASCADE, related_name="something")
|
2014-07-21 17:50:21 +08:00
|
|
|
new_field.set_attributes_from_name("author_other_really_long_named_i_mean_so_long_fk")
|
|
|
|
with connection.schema_editor() as editor:
|
2015-01-29 22:14:55 +08:00
|
|
|
editor.add_field(BookWithLongName, new_field)
|
2014-07-21 17:50:21 +08:00
|
|
|
|
2015-07-16 02:38:10 +08:00
|
|
|
def test_add_foreign_object(self):
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.create_model(BookForeignObj)
|
|
|
|
|
2015-07-22 22:43:21 +08:00
|
|
|
new_field = ForeignObject(Author, on_delete=CASCADE, from_fields=['author_id'], to_fields=['id'])
|
2015-07-16 02:38:10 +08:00
|
|
|
new_field.set_attributes_from_name('author')
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.add_field(BookForeignObj, new_field)
|
|
|
|
|
2013-12-23 03:44:49 +08:00
|
|
|
def test_creation_deletion_reserved_names(self):
|
|
|
|
"""
|
|
|
|
Tries creating a model's table, and then deleting it when it has a
|
|
|
|
SQL reserved name.
|
|
|
|
"""
|
|
|
|
# Create the table
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
try:
|
|
|
|
editor.create_model(Thing)
|
|
|
|
except OperationalError as e:
|
|
|
|
self.fail("Errors when applying initial migration for a model "
|
2016-05-04 07:30:48 +08:00
|
|
|
"with a table named after an SQL reserved word: %s" % e)
|
2016-10-27 15:53:39 +08:00
|
|
|
# The table is there
|
2013-12-23 03:44:49 +08:00
|
|
|
list(Thing.objects.all())
|
|
|
|
# Clean up that table
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.delete_model(Thing)
|
2016-10-27 15:53:39 +08:00
|
|
|
# The table is gone
|
2016-01-17 19:26:39 +08:00
|
|
|
with self.assertRaises(DatabaseError):
|
|
|
|
list(Thing.objects.all())
|
2014-09-17 01:37:18 +08:00
|
|
|
|
|
|
|
def test_remove_constraints_capital_letters(self):
|
|
|
|
"""
|
|
|
|
#23065 - Constraint names must be quoted if they contain capital letters.
|
|
|
|
"""
|
2017-02-02 00:41:56 +08:00
|
|
|
def get_field(*args, field_class=IntegerField, **kwargs):
|
2014-09-17 01:37:18 +08:00
|
|
|
kwargs['db_column'] = "CamelCase"
|
2017-02-02 00:41:56 +08:00
|
|
|
field = field_class(*args, **kwargs)
|
2014-09-17 01:37:18 +08:00
|
|
|
field.set_attributes_from_name("CamelCase")
|
|
|
|
return field
|
|
|
|
|
|
|
|
model = Author
|
|
|
|
field = get_field()
|
|
|
|
table = model._meta.db_table
|
|
|
|
column = field.column
|
|
|
|
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.create_model(model)
|
|
|
|
editor.add_field(model, field)
|
|
|
|
|
2016-07-07 20:09:42 +08:00
|
|
|
constraint_name = "CamelCaseIndex"
|
2014-09-17 01:37:18 +08:00
|
|
|
editor.execute(
|
|
|
|
editor.sql_create_index % {
|
|
|
|
"table": editor.quote_name(table),
|
2016-07-07 20:09:42 +08:00
|
|
|
"name": editor.quote_name(constraint_name),
|
2016-08-08 19:50:25 +08:00
|
|
|
"using": "",
|
2014-09-17 01:37:18 +08:00
|
|
|
"columns": editor.quote_name(column),
|
|
|
|
"extra": "",
|
|
|
|
}
|
|
|
|
)
|
2016-07-07 20:09:42 +08:00
|
|
|
if connection.features.uppercases_column_names:
|
|
|
|
constraint_name = constraint_name.upper()
|
|
|
|
self.assertIn(constraint_name, self.get_constraints(model._meta.db_table))
|
|
|
|
editor.alter_field(model, get_field(db_index=True), field, strict=True)
|
|
|
|
self.assertNotIn(constraint_name, self.get_constraints(model._meta.db_table))
|
2014-09-17 01:37:18 +08:00
|
|
|
|
2016-07-07 20:09:42 +08:00
|
|
|
constraint_name = "CamelCaseUniqConstraint"
|
2014-09-17 01:37:18 +08:00
|
|
|
editor.execute(
|
|
|
|
editor.sql_create_unique % {
|
|
|
|
"table": editor.quote_name(table),
|
2016-07-07 20:09:42 +08:00
|
|
|
"name": editor.quote_name(constraint_name),
|
2014-09-17 01:37:18 +08:00
|
|
|
"columns": editor.quote_name(field.column),
|
|
|
|
}
|
|
|
|
)
|
2016-07-07 20:09:42 +08:00
|
|
|
if connection.features.uppercases_column_names:
|
|
|
|
constraint_name = constraint_name.upper()
|
|
|
|
self.assertIn(constraint_name, self.get_constraints(model._meta.db_table))
|
|
|
|
editor.alter_field(model, get_field(unique=True), field, strict=True)
|
|
|
|
self.assertNotIn(constraint_name, self.get_constraints(model._meta.db_table))
|
|
|
|
|
|
|
|
if connection.features.supports_foreign_keys:
|
|
|
|
constraint_name = "CamelCaseFKConstraint"
|
|
|
|
editor.execute(
|
|
|
|
editor.sql_create_fk % {
|
|
|
|
"table": editor.quote_name(table),
|
|
|
|
"name": editor.quote_name(constraint_name),
|
|
|
|
"column": editor.quote_name(column),
|
|
|
|
"to_table": editor.quote_name(table),
|
|
|
|
"to_column": editor.quote_name(model._meta.auto_field.column),
|
|
|
|
"deferrable": connection.ops.deferrable_sql(),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
if connection.features.uppercases_column_names:
|
|
|
|
constraint_name = constraint_name.upper()
|
|
|
|
self.assertIn(constraint_name, self.get_constraints(model._meta.db_table))
|
|
|
|
editor.alter_field(model, get_field(Author, CASCADE, field_class=ForeignKey), field, strict=True)
|
|
|
|
self.assertNotIn(constraint_name, self.get_constraints(model._meta.db_table))
|
2014-12-15 22:14:39 +08:00
|
|
|
|
|
|
|
def test_add_field_use_effective_default(self):
|
|
|
|
"""
|
|
|
|
#23987 - effective_default() should be used as the field default when
|
|
|
|
adding a new field.
|
|
|
|
"""
|
|
|
|
# Create the table
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.create_model(Author)
|
|
|
|
# Ensure there's no surname field
|
|
|
|
columns = self.column_classes(Author)
|
|
|
|
self.assertNotIn("surname", columns)
|
|
|
|
# Create a row
|
|
|
|
Author.objects.create(name='Anonymous1')
|
|
|
|
# Add new CharField to ensure default will be used from effective_default
|
|
|
|
new_field = CharField(max_length=15, blank=True)
|
|
|
|
new_field.set_attributes_from_name("surname")
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.add_field(Author, new_field)
|
|
|
|
# Ensure field was added with the right default
|
|
|
|
with connection.cursor() as cursor:
|
|
|
|
cursor.execute("SELECT surname FROM schema_author;")
|
|
|
|
item = cursor.fetchall()[0]
|
2014-12-16 07:15:55 +08:00
|
|
|
self.assertEqual(item[0], None if connection.features.interprets_empty_strings_as_nulls else '')
|
2015-01-09 06:51:00 +08:00
|
|
|
|
2014-12-30 00:41:16 +08:00
|
|
|
def test_add_field_default_dropped(self):
|
|
|
|
# Create the table
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.create_model(Author)
|
|
|
|
# Ensure there's no surname field
|
|
|
|
columns = self.column_classes(Author)
|
|
|
|
self.assertNotIn("surname", columns)
|
|
|
|
# Create a row
|
|
|
|
Author.objects.create(name='Anonymous1')
|
|
|
|
# Add new CharField with a default
|
|
|
|
new_field = CharField(max_length=15, blank=True, default='surname default')
|
|
|
|
new_field.set_attributes_from_name("surname")
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.add_field(Author, new_field)
|
|
|
|
# Ensure field was added with the right default
|
|
|
|
with connection.cursor() as cursor:
|
|
|
|
cursor.execute("SELECT surname FROM schema_author;")
|
|
|
|
item = cursor.fetchall()[0]
|
|
|
|
self.assertEqual(item[0], 'surname default')
|
|
|
|
# And that the default is no longer set in the database.
|
|
|
|
field = next(
|
|
|
|
f for f in connection.introspection.get_table_description(cursor, "schema_author")
|
|
|
|
if f.name == "surname"
|
|
|
|
)
|
|
|
|
if connection.features.can_introspect_default:
|
|
|
|
self.assertIsNone(field.default)
|
|
|
|
|
|
|
|
def test_alter_field_default_dropped(self):
|
|
|
|
# Create the table
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.create_model(Author)
|
|
|
|
# Create a row
|
|
|
|
Author.objects.create(name='Anonymous1')
|
2016-06-17 02:19:18 +08:00
|
|
|
self.assertIsNone(Author.objects.get().height)
|
2014-12-30 00:41:16 +08:00
|
|
|
old_field = Author._meta.get_field('height')
|
|
|
|
# The default from the new field is used in updating existing rows.
|
|
|
|
new_field = IntegerField(blank=True, default=42)
|
|
|
|
new_field.set_attributes_from_name('height')
|
|
|
|
with connection.schema_editor() as editor:
|
2016-07-09 00:03:33 +08:00
|
|
|
editor.alter_field(Author, old_field, new_field, strict=True)
|
2014-12-30 00:41:16 +08:00
|
|
|
self.assertEqual(Author.objects.get().height, 42)
|
|
|
|
# The database default should be removed.
|
|
|
|
with connection.cursor() as cursor:
|
|
|
|
field = next(
|
|
|
|
f for f in connection.introspection.get_table_description(cursor, "schema_author")
|
|
|
|
if f.name == "height"
|
|
|
|
)
|
|
|
|
if connection.features.can_introspect_default:
|
|
|
|
self.assertIsNone(field.default)
|
2015-09-13 03:06:35 +08:00
|
|
|
|
2017-04-01 06:42:08 +08:00
|
|
|
@unittest.skipIf(connection.vendor == 'sqlite', 'SQLite naively remakes the table on field alteration.')
|
|
|
|
def test_alter_field_default_doesnt_perfom_queries(self):
|
|
|
|
"""
|
|
|
|
No queries are performed if a field default changes and the field's
|
|
|
|
not changing from null to non-null.
|
|
|
|
"""
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.create_model(AuthorWithDefaultHeight)
|
|
|
|
old_field = AuthorWithDefaultHeight._meta.get_field('height')
|
|
|
|
new_default = old_field.default * 2
|
|
|
|
new_field = PositiveIntegerField(null=True, blank=True, default=new_default)
|
|
|
|
new_field.set_attributes_from_name('height')
|
|
|
|
with connection.schema_editor() as editor, self.assertNumQueries(0):
|
|
|
|
editor.alter_field(AuthorWithDefaultHeight, old_field, new_field, strict=True)
|
|
|
|
|
2015-09-13 03:06:35 +08:00
|
|
|
def test_add_textfield_unhashable_default(self):
|
|
|
|
# Create the table
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.create_model(Author)
|
|
|
|
# Create a row
|
|
|
|
Author.objects.create(name='Anonymous1')
|
|
|
|
# Create a field that has an unhashable default
|
|
|
|
new_field = TextField(default={})
|
|
|
|
new_field.set_attributes_from_name("info")
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.add_field(Author, new_field)
|
2015-11-08 00:08:03 +08:00
|
|
|
|
2016-07-13 08:50:17 +08:00
|
|
|
@unittest.skipUnless(connection.vendor == 'postgresql', "PostgreSQL specific")
|
|
|
|
def test_add_indexed_charfield(self):
|
|
|
|
field = CharField(max_length=255, db_index=True)
|
|
|
|
field.set_attributes_from_name('nom_de_plume')
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.create_model(Author)
|
|
|
|
editor.add_field(Author, field)
|
|
|
|
# Should create two indexes; one for like operator.
|
|
|
|
self.assertEqual(
|
|
|
|
self.get_constraints_for_column(Author, 'nom_de_plume'),
|
2016-07-08 22:11:19 +08:00
|
|
|
['schema_author_nom_de_plume_7570a851', 'schema_author_nom_de_plume_7570a851_like'],
|
2016-07-13 08:50:17 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
@unittest.skipUnless(connection.vendor == 'postgresql', "PostgreSQL specific")
|
|
|
|
def test_add_unique_charfield(self):
|
|
|
|
field = CharField(max_length=255, unique=True)
|
|
|
|
field.set_attributes_from_name('nom_de_plume')
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.create_model(Author)
|
|
|
|
editor.add_field(Author, field)
|
|
|
|
# Should create two indexes; one for like operator.
|
|
|
|
self.assertEqual(
|
|
|
|
self.get_constraints_for_column(Author, 'nom_de_plume'),
|
|
|
|
['schema_author_nom_de_plume_7570a851_like', 'schema_author_nom_de_plume_key']
|
|
|
|
)
|
|
|
|
|
2015-11-08 00:08:03 +08:00
|
|
|
@unittest.skipUnless(connection.vendor == 'postgresql', "PostgreSQL specific")
|
|
|
|
def test_alter_field_add_index_to_charfield(self):
|
2016-01-08 08:42:58 +08:00
|
|
|
# Create the table and verify no initial indexes.
|
2015-11-08 00:08:03 +08:00
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.create_model(Author)
|
2016-01-08 08:42:58 +08:00
|
|
|
self.assertEqual(self.get_constraints_for_column(Author, 'name'), [])
|
|
|
|
# Alter to add db_index=True and create 2 indexes.
|
2015-11-08 00:08:03 +08:00
|
|
|
old_field = Author._meta.get_field('name')
|
|
|
|
new_field = CharField(max_length=255, db_index=True)
|
|
|
|
new_field.set_attributes_from_name('name')
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.alter_field(Author, old_field, new_field, strict=True)
|
2016-01-08 08:42:58 +08:00
|
|
|
self.assertEqual(
|
|
|
|
self.get_constraints_for_column(Author, 'name'),
|
2016-07-08 22:11:19 +08:00
|
|
|
['schema_author_name_1fbc5617', 'schema_author_name_1fbc5617_like']
|
2016-01-08 08:42:58 +08:00
|
|
|
)
|
|
|
|
# Remove db_index=True to drop both indexes.
|
2015-11-08 00:08:03 +08:00
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.alter_field(Author, new_field, old_field, strict=True)
|
2016-01-08 08:42:58 +08:00
|
|
|
self.assertEqual(self.get_constraints_for_column(Author, 'name'), [])
|
2015-11-08 00:08:03 +08:00
|
|
|
|
2016-06-23 12:46:11 +08:00
|
|
|
@unittest.skipUnless(connection.vendor == 'postgresql', "PostgreSQL specific")
|
|
|
|
def test_alter_field_add_unique_to_charfield(self):
|
|
|
|
# Create the table and verify no initial indexes.
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.create_model(Author)
|
|
|
|
self.assertEqual(self.get_constraints_for_column(Author, 'name'), [])
|
|
|
|
# Alter to add unique=True and create 2 indexes.
|
|
|
|
old_field = Author._meta.get_field('name')
|
|
|
|
new_field = CharField(max_length=255, unique=True)
|
|
|
|
new_field.set_attributes_from_name('name')
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.alter_field(Author, old_field, new_field, strict=True)
|
|
|
|
self.assertEqual(
|
|
|
|
self.get_constraints_for_column(Author, 'name'),
|
|
|
|
['schema_author_name_1fbc5617_like', 'schema_author_name_1fbc5617_uniq']
|
|
|
|
)
|
|
|
|
# Remove unique=True to drop both indexes.
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.alter_field(Author, new_field, old_field, strict=True)
|
|
|
|
self.assertEqual(self.get_constraints_for_column(Author, 'name'), [])
|
|
|
|
|
2015-11-08 00:08:03 +08:00
|
|
|
@unittest.skipUnless(connection.vendor == 'postgresql', "PostgreSQL specific")
|
|
|
|
def test_alter_field_add_index_to_textfield(self):
|
2016-01-08 08:42:58 +08:00
|
|
|
# Create the table and verify no initial indexes.
|
2015-11-08 00:08:03 +08:00
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.create_model(Note)
|
2016-01-08 08:42:58 +08:00
|
|
|
self.assertEqual(self.get_constraints_for_column(Note, 'info'), [])
|
|
|
|
# Alter to add db_index=True and create 2 indexes.
|
2015-11-08 00:08:03 +08:00
|
|
|
old_field = Note._meta.get_field('info')
|
|
|
|
new_field = TextField(db_index=True)
|
|
|
|
new_field.set_attributes_from_name('info')
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.alter_field(Note, old_field, new_field, strict=True)
|
2016-01-08 08:42:58 +08:00
|
|
|
self.assertEqual(
|
|
|
|
self.get_constraints_for_column(Note, 'info'),
|
2016-07-08 22:11:19 +08:00
|
|
|
['schema_note_info_4b0ea695', 'schema_note_info_4b0ea695_like']
|
2016-01-08 08:42:58 +08:00
|
|
|
)
|
|
|
|
# Remove db_index=True to drop both indexes.
|
2015-11-08 00:08:03 +08:00
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.alter_field(Note, new_field, old_field, strict=True)
|
2016-01-08 08:42:58 +08:00
|
|
|
self.assertEqual(self.get_constraints_for_column(Note, 'info'), [])
|
2016-01-08 09:06:58 +08:00
|
|
|
|
|
|
|
@unittest.skipUnless(connection.vendor == 'postgresql', "PostgreSQL specific")
|
|
|
|
def test_alter_field_add_unique_to_charfield_with_db_index(self):
|
|
|
|
# Create the table and verify initial indexes.
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.create_model(BookWithoutAuthor)
|
|
|
|
self.assertEqual(
|
|
|
|
self.get_constraints_for_column(BookWithoutAuthor, 'title'),
|
2016-07-08 22:11:19 +08:00
|
|
|
['schema_book_title_2dfb2dff', 'schema_book_title_2dfb2dff_like']
|
2016-01-08 09:06:58 +08:00
|
|
|
)
|
2016-06-23 12:46:11 +08:00
|
|
|
# Alter to add unique=True (should replace the index)
|
2016-01-08 09:06:58 +08:00
|
|
|
old_field = BookWithoutAuthor._meta.get_field('title')
|
|
|
|
new_field = CharField(max_length=100, db_index=True, unique=True)
|
|
|
|
new_field.set_attributes_from_name('title')
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.alter_field(BookWithoutAuthor, old_field, new_field, strict=True)
|
|
|
|
self.assertEqual(
|
|
|
|
self.get_constraints_for_column(BookWithoutAuthor, 'title'),
|
2016-06-23 12:46:11 +08:00
|
|
|
['schema_book_title_2dfb2dff_like', 'schema_book_title_2dfb2dff_uniq']
|
2016-01-08 09:06:58 +08:00
|
|
|
)
|
2016-06-28 00:13:15 +08:00
|
|
|
# Alter to remove unique=True (should drop unique index)
|
|
|
|
new_field2 = CharField(max_length=100, db_index=True)
|
|
|
|
new_field2.set_attributes_from_name('title')
|
2016-01-08 09:06:58 +08:00
|
|
|
with connection.schema_editor() as editor:
|
2016-06-28 00:13:15 +08:00
|
|
|
editor.alter_field(BookWithoutAuthor, new_field, new_field2, strict=True)
|
2016-01-08 09:06:58 +08:00
|
|
|
self.assertEqual(
|
|
|
|
self.get_constraints_for_column(BookWithoutAuthor, 'title'),
|
2016-07-08 22:11:19 +08:00
|
|
|
['schema_book_title_2dfb2dff', 'schema_book_title_2dfb2dff_like']
|
2016-06-23 12:46:11 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
@unittest.skipUnless(connection.vendor == 'postgresql', "PostgreSQL specific")
|
|
|
|
def test_alter_field_remove_unique_and_db_index_from_charfield(self):
|
|
|
|
# Create the table and verify initial indexes.
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.create_model(BookWithoutAuthor)
|
|
|
|
self.assertEqual(
|
|
|
|
self.get_constraints_for_column(BookWithoutAuthor, 'title'),
|
2016-07-08 22:11:19 +08:00
|
|
|
['schema_book_title_2dfb2dff', 'schema_book_title_2dfb2dff_like']
|
2016-06-23 12:46:11 +08:00
|
|
|
)
|
|
|
|
# Alter to add unique=True (should replace the index)
|
|
|
|
old_field = BookWithoutAuthor._meta.get_field('title')
|
|
|
|
new_field = CharField(max_length=100, db_index=True, unique=True)
|
|
|
|
new_field.set_attributes_from_name('title')
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.alter_field(BookWithoutAuthor, old_field, new_field, strict=True)
|
|
|
|
self.assertEqual(
|
|
|
|
self.get_constraints_for_column(BookWithoutAuthor, 'title'),
|
|
|
|
['schema_book_title_2dfb2dff_like', 'schema_book_title_2dfb2dff_uniq']
|
|
|
|
)
|
|
|
|
# Alter to remove both unique=True and db_index=True (should drop all indexes)
|
|
|
|
new_field2 = CharField(max_length=100)
|
|
|
|
new_field2.set_attributes_from_name('title')
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.alter_field(BookWithoutAuthor, new_field, new_field2, strict=True)
|
|
|
|
self.assertEqual(self.get_constraints_for_column(BookWithoutAuthor, 'title'), [])
|
|
|
|
|
|
|
|
@unittest.skipUnless(connection.vendor == 'postgresql', "PostgreSQL specific")
|
|
|
|
def test_alter_field_swap_unique_and_db_index_with_charfield(self):
|
|
|
|
# Create the table and verify initial indexes.
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.create_model(BookWithoutAuthor)
|
|
|
|
self.assertEqual(
|
|
|
|
self.get_constraints_for_column(BookWithoutAuthor, 'title'),
|
2016-07-08 22:11:19 +08:00
|
|
|
['schema_book_title_2dfb2dff', 'schema_book_title_2dfb2dff_like']
|
2016-06-23 12:46:11 +08:00
|
|
|
)
|
|
|
|
# Alter to set unique=True and remove db_index=True (should replace the index)
|
|
|
|
old_field = BookWithoutAuthor._meta.get_field('title')
|
|
|
|
new_field = CharField(max_length=100, unique=True)
|
|
|
|
new_field.set_attributes_from_name('title')
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.alter_field(BookWithoutAuthor, old_field, new_field, strict=True)
|
|
|
|
self.assertEqual(
|
|
|
|
self.get_constraints_for_column(BookWithoutAuthor, 'title'),
|
|
|
|
['schema_book_title_2dfb2dff_like', 'schema_book_title_2dfb2dff_uniq']
|
|
|
|
)
|
|
|
|
# Alter to set db_index=True and remove unique=True (should restore index)
|
|
|
|
new_field2 = CharField(max_length=100, db_index=True)
|
|
|
|
new_field2.set_attributes_from_name('title')
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.alter_field(BookWithoutAuthor, new_field, new_field2, strict=True)
|
|
|
|
self.assertEqual(
|
|
|
|
self.get_constraints_for_column(BookWithoutAuthor, 'title'),
|
2016-07-08 22:11:19 +08:00
|
|
|
['schema_book_title_2dfb2dff', 'schema_book_title_2dfb2dff_like']
|
2016-01-08 09:06:58 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
@unittest.skipUnless(connection.vendor == 'postgresql', "PostgreSQL specific")
|
|
|
|
def test_alter_field_add_db_index_to_charfield_with_unique(self):
|
|
|
|
# Create the table and verify initial indexes.
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.create_model(Tag)
|
|
|
|
self.assertEqual(
|
|
|
|
self.get_constraints_for_column(Tag, 'slug'),
|
|
|
|
['schema_tag_slug_2c418ba3_like', 'schema_tag_slug_key']
|
|
|
|
)
|
|
|
|
# Alter to add db_index=True
|
|
|
|
old_field = Tag._meta.get_field('slug')
|
|
|
|
new_field = SlugField(db_index=True, unique=True)
|
|
|
|
new_field.set_attributes_from_name('slug')
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.alter_field(Tag, old_field, new_field, strict=True)
|
|
|
|
self.assertEqual(
|
|
|
|
self.get_constraints_for_column(Tag, 'slug'),
|
|
|
|
['schema_tag_slug_2c418ba3_like', 'schema_tag_slug_key']
|
|
|
|
)
|
|
|
|
# Alter to remove db_index=True
|
2016-06-28 00:13:15 +08:00
|
|
|
new_field2 = SlugField(unique=True)
|
|
|
|
new_field2.set_attributes_from_name('slug')
|
2016-01-08 09:06:58 +08:00
|
|
|
with connection.schema_editor() as editor:
|
2016-06-28 00:13:15 +08:00
|
|
|
editor.alter_field(Tag, new_field, new_field2, strict=True)
|
2016-01-08 09:06:58 +08:00
|
|
|
self.assertEqual(
|
|
|
|
self.get_constraints_for_column(Tag, 'slug'),
|
|
|
|
['schema_tag_slug_2c418ba3_like', 'schema_tag_slug_key']
|
|
|
|
)
|
2016-03-29 16:58:04 +08:00
|
|
|
|
2016-06-25 11:25:39 +08:00
|
|
|
def test_alter_field_add_index_to_integerfield(self):
|
|
|
|
# Create the table and verify no initial indexes.
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.create_model(Author)
|
|
|
|
self.assertEqual(self.get_constraints_for_column(Author, 'weight'), [])
|
|
|
|
|
|
|
|
# Alter to add db_index=True and create index.
|
|
|
|
old_field = Author._meta.get_field('weight')
|
|
|
|
new_field = IntegerField(null=True, db_index=True)
|
|
|
|
new_field.set_attributes_from_name('weight')
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.alter_field(Author, old_field, new_field, strict=True)
|
|
|
|
|
2016-07-08 22:11:19 +08:00
|
|
|
expected = 'schema_author_weight_587740f9'
|
2016-06-25 11:25:39 +08:00
|
|
|
if connection.features.uppercases_column_names:
|
|
|
|
expected = expected.upper()
|
|
|
|
self.assertEqual(self.get_constraints_for_column(Author, 'weight'), [expected])
|
|
|
|
|
|
|
|
# Remove db_index=True to drop index.
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.alter_field(Author, new_field, old_field, strict=True)
|
|
|
|
self.assertEqual(self.get_constraints_for_column(Author, 'weight'), [])
|
|
|
|
|
2016-03-29 16:58:04 +08:00
|
|
|
def test_alter_pk_with_self_referential_field(self):
|
|
|
|
"""
|
|
|
|
Changing the primary key field name of a model with a self-referential
|
|
|
|
foreign key (#26384).
|
|
|
|
"""
|
2016-04-02 20:17:35 +08:00
|
|
|
if connection.vendor == 'mysql' and connection.mysql_version < (5, 6, 6):
|
|
|
|
self.skipTest('Skip known bug renaming primary keys on older MySQL versions (#24995).')
|
2016-12-18 04:02:57 +08:00
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.create_model(Node)
|
2016-03-29 16:58:04 +08:00
|
|
|
old_field = Node._meta.get_field('node_id')
|
|
|
|
new_field = AutoField(primary_key=True)
|
|
|
|
new_field.set_attributes_from_name('id')
|
|
|
|
with connection.schema_editor() as editor:
|
2016-07-09 00:03:33 +08:00
|
|
|
editor.alter_field(Node, old_field, new_field, strict=True)
|
2016-04-02 21:49:32 +08:00
|
|
|
|
|
|
|
@mock.patch('django.db.backends.base.schema.datetime')
|
|
|
|
@mock.patch('django.db.backends.base.schema.timezone')
|
|
|
|
def test_add_datefield_and_datetimefield_use_effective_default(self, mocked_datetime, mocked_tz):
|
|
|
|
"""
|
|
|
|
effective_default() should be used for DateField, DateTimeField, and
|
|
|
|
TimeField if auto_now or auto_add_now is set (#25005).
|
|
|
|
"""
|
|
|
|
now = datetime.datetime(month=1, day=1, year=2000, hour=1, minute=1)
|
2016-10-08 09:06:49 +08:00
|
|
|
now_tz = datetime.datetime(month=1, day=1, year=2000, hour=1, minute=1, tzinfo=timezone.utc)
|
2016-04-02 21:49:32 +08:00
|
|
|
mocked_datetime.now = mock.MagicMock(return_value=now)
|
|
|
|
mocked_tz.now = mock.MagicMock(return_value=now_tz)
|
|
|
|
# Create the table
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.create_model(Author)
|
|
|
|
# Check auto_now/auto_now_add attributes are not defined
|
|
|
|
columns = self.column_classes(Author)
|
|
|
|
self.assertNotIn("dob_auto_now", columns)
|
|
|
|
self.assertNotIn("dob_auto_now_add", columns)
|
|
|
|
self.assertNotIn("dtob_auto_now", columns)
|
|
|
|
self.assertNotIn("dtob_auto_now_add", columns)
|
|
|
|
self.assertNotIn("tob_auto_now", columns)
|
|
|
|
self.assertNotIn("tob_auto_now_add", columns)
|
|
|
|
# Create a row
|
|
|
|
Author.objects.create(name='Anonymous1')
|
|
|
|
# Ensure fields were added with the correct defaults
|
|
|
|
dob_auto_now = DateField(auto_now=True)
|
|
|
|
dob_auto_now.set_attributes_from_name('dob_auto_now')
|
|
|
|
self.check_added_field_default(
|
|
|
|
editor, Author, dob_auto_now, 'dob_auto_now', now.date(),
|
|
|
|
cast_function=lambda x: x.date(),
|
|
|
|
)
|
|
|
|
dob_auto_now_add = DateField(auto_now_add=True)
|
|
|
|
dob_auto_now_add.set_attributes_from_name('dob_auto_now_add')
|
|
|
|
self.check_added_field_default(
|
|
|
|
editor, Author, dob_auto_now_add, 'dob_auto_now_add', now.date(),
|
|
|
|
cast_function=lambda x: x.date(),
|
|
|
|
)
|
|
|
|
dtob_auto_now = DateTimeField(auto_now=True)
|
|
|
|
dtob_auto_now.set_attributes_from_name('dtob_auto_now')
|
|
|
|
self.check_added_field_default(
|
|
|
|
editor, Author, dtob_auto_now, 'dtob_auto_now', now,
|
|
|
|
)
|
|
|
|
dt_tm_of_birth_auto_now_add = DateTimeField(auto_now_add=True)
|
|
|
|
dt_tm_of_birth_auto_now_add.set_attributes_from_name('dtob_auto_now_add')
|
|
|
|
self.check_added_field_default(
|
|
|
|
editor, Author, dt_tm_of_birth_auto_now_add, 'dtob_auto_now_add', now,
|
|
|
|
)
|
|
|
|
tob_auto_now = TimeField(auto_now=True)
|
|
|
|
tob_auto_now.set_attributes_from_name('tob_auto_now')
|
|
|
|
self.check_added_field_default(
|
|
|
|
editor, Author, tob_auto_now, 'tob_auto_now', now.time(),
|
|
|
|
cast_function=lambda x: x.time(),
|
|
|
|
)
|
|
|
|
tob_auto_now_add = TimeField(auto_now_add=True)
|
|
|
|
tob_auto_now_add.set_attributes_from_name('tob_auto_now_add')
|
|
|
|
self.check_added_field_default(
|
|
|
|
editor, Author, tob_auto_now_add, 'tob_auto_now_add', now.time(),
|
|
|
|
cast_function=lambda x: x.time(),
|
|
|
|
)
|
2016-12-31 06:11:12 +08:00
|
|
|
|
|
|
|
@unittest.skipUnless(connection.vendor == 'oracle', 'Oracle specific db_table syntax')
|
|
|
|
def test_creation_with_db_table_double_quotes(self):
|
|
|
|
oracle_user = connection.creation._test_database_user()
|
|
|
|
|
|
|
|
class Student(Model):
|
|
|
|
name = CharField(max_length=30)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
app_label = 'schema'
|
|
|
|
apps = new_apps
|
|
|
|
db_table = '"%s"."DJANGO_STUDENT_TABLE"' % oracle_user
|
|
|
|
|
|
|
|
class Document(Model):
|
|
|
|
name = CharField(max_length=30)
|
|
|
|
students = ManyToManyField(Student)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
app_label = 'schema'
|
|
|
|
apps = new_apps
|
|
|
|
db_table = '"%s"."DJANGO_DOCUMENT_TABLE"' % oracle_user
|
|
|
|
|
|
|
|
self.local_models = [Student, Document]
|
|
|
|
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
editor.create_model(Student)
|
|
|
|
editor.create_model(Document)
|
|
|
|
|
|
|
|
doc = Document.objects.create(name='Test Name')
|
|
|
|
student = Student.objects.create(name='Some man')
|
|
|
|
doc.students.add(student)
|