tests.py 139 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148
  1. import datetime
  2. import itertools
  3. import unittest
  4. from copy import copy
  5. from unittest import mock
  6. from django.core.management.color import no_style
  7. from django.db import (
  8. DatabaseError, DataError, IntegrityError, OperationalError, connection,
  9. )
  10. from django.db.models import (
  11. CASCADE, PROTECT, AutoField, BigAutoField, BigIntegerField, BinaryField,
  12. BooleanField, CharField, CheckConstraint, DateField, DateTimeField,
  13. ForeignKey, ForeignObject, Index, IntegerField, ManyToManyField, Model,
  14. OneToOneField, PositiveIntegerField, Q, SlugField, SmallAutoField,
  15. SmallIntegerField, TextField, TimeField, UniqueConstraint, UUIDField,
  16. )
  17. from django.db.transaction import TransactionManagementError, atomic
  18. from django.test import (
  19. TransactionTestCase, skipIfDBFeature, skipUnlessDBFeature,
  20. )
  21. from django.test.utils import CaptureQueriesContext, isolate_apps
  22. from django.utils import timezone
  23. from .fields import (
  24. CustomManyToManyField, InheritedManyToManyField, MediumBlobField,
  25. )
  26. from .models import (
  27. Author, AuthorCharFieldWithIndex, AuthorTextFieldWithIndex,
  28. AuthorWithDefaultHeight, AuthorWithEvenLongerName, AuthorWithIndexedName,
  29. AuthorWithIndexedNameAndBirthday, AuthorWithUniqueName,
  30. AuthorWithUniqueNameAndBirthday, Book, BookForeignObj, BookWeak,
  31. BookWithLongName, BookWithO2O, BookWithoutAuthor, BookWithSlug, IntegerPK,
  32. Node, Note, NoteRename, Tag, TagIndexed, TagM2MTest, TagUniqueRename,
  33. Thing, UniqueTest, new_apps,
  34. )
  35. class SchemaTests(TransactionTestCase):
  36. """
  37. Tests for the schema-alteration code.
  38. Be aware that these tests are more liable than most to false results,
  39. as sometimes the code to check if a test has worked is almost as complex
  40. as the code it is testing.
  41. """
  42. available_apps = []
  43. models = [
  44. Author, AuthorCharFieldWithIndex, AuthorTextFieldWithIndex,
  45. AuthorWithDefaultHeight, AuthorWithEvenLongerName, Book, BookWeak,
  46. BookWithLongName, BookWithO2O, BookWithSlug, IntegerPK, Node, Note,
  47. Tag, TagIndexed, TagM2MTest, TagUniqueRename, Thing, UniqueTest,
  48. ]
  49. # Utility functions
  50. def setUp(self):
  51. # local_models should contain test dependent model classes that will be
  52. # automatically removed from the app cache on test tear down.
  53. self.local_models = []
  54. # isolated_local_models contains models that are in test methods
  55. # decorated with @isolate_apps.
  56. self.isolated_local_models = []
  57. def tearDown(self):
  58. # Delete any tables made for our models
  59. self.delete_tables()
  60. new_apps.clear_cache()
  61. for model in new_apps.get_models():
  62. model._meta._expire_cache()
  63. if 'schema' in new_apps.all_models:
  64. for model in self.local_models:
  65. for many_to_many in model._meta.many_to_many:
  66. through = many_to_many.remote_field.through
  67. if through and through._meta.auto_created:
  68. del new_apps.all_models['schema'][through._meta.model_name]
  69. del new_apps.all_models['schema'][model._meta.model_name]
  70. if self.isolated_local_models:
  71. with connection.schema_editor() as editor:
  72. for model in self.isolated_local_models:
  73. editor.delete_model(model)
  74. def delete_tables(self):
  75. "Deletes all model tables for our models for a clean test environment"
  76. converter = connection.introspection.identifier_converter
  77. with connection.schema_editor() as editor:
  78. connection.disable_constraint_checking()
  79. table_names = connection.introspection.table_names()
  80. for model in itertools.chain(SchemaTests.models, self.local_models):
  81. tbl = converter(model._meta.db_table)
  82. if tbl in table_names:
  83. editor.delete_model(model)
  84. table_names.remove(tbl)
  85. connection.enable_constraint_checking()
  86. def column_classes(self, model):
  87. with connection.cursor() as cursor:
  88. columns = {
  89. d[0]: (connection.introspection.get_field_type(d[1], d), d)
  90. for d in connection.introspection.get_table_description(
  91. cursor,
  92. model._meta.db_table,
  93. )
  94. }
  95. # SQLite has a different format for field_type
  96. for name, (type, desc) in columns.items():
  97. if isinstance(type, tuple):
  98. columns[name] = (type[0], desc)
  99. # SQLite also doesn't error properly
  100. if not columns:
  101. raise DatabaseError("Table does not exist (empty pragma)")
  102. return columns
  103. def get_primary_key(self, table):
  104. with connection.cursor() as cursor:
  105. return connection.introspection.get_primary_key_column(cursor, table)
  106. def get_indexes(self, table):
  107. """
  108. Get the indexes on the table using a new cursor.
  109. """
  110. with connection.cursor() as cursor:
  111. return [
  112. c['columns'][0]
  113. for c in connection.introspection.get_constraints(cursor, table).values()
  114. if c['index'] and len(c['columns']) == 1
  115. ]
  116. def get_uniques(self, table):
  117. with connection.cursor() as cursor:
  118. return [
  119. c['columns'][0]
  120. for c in connection.introspection.get_constraints(cursor, table).values()
  121. if c['unique'] and len(c['columns']) == 1
  122. ]
  123. def get_constraints(self, table):
  124. """
  125. Get the constraints on a table using a new cursor.
  126. """
  127. with connection.cursor() as cursor:
  128. return connection.introspection.get_constraints(cursor, table)
  129. def get_constraints_for_column(self, model, column_name):
  130. constraints = self.get_constraints(model._meta.db_table)
  131. constraints_for_column = []
  132. for name, details in constraints.items():
  133. if details['columns'] == [column_name]:
  134. constraints_for_column.append(name)
  135. return sorted(constraints_for_column)
  136. def check_added_field_default(self, schema_editor, model, field, field_name, expected_default,
  137. cast_function=None):
  138. with connection.cursor() as cursor:
  139. schema_editor.add_field(model, field)
  140. cursor.execute("SELECT {} FROM {};".format(field_name, model._meta.db_table))
  141. database_default = cursor.fetchall()[0][0]
  142. if cast_function and not type(database_default) == type(expected_default):
  143. database_default = cast_function(database_default)
  144. self.assertEqual(database_default, expected_default)
  145. def get_constraints_count(self, table, column, fk_to):
  146. """
  147. Return a dict with keys 'fks', 'uniques, and 'indexes' indicating the
  148. number of foreign keys, unique constraints, and indexes on
  149. `table`.`column`. The `fk_to` argument is a 2-tuple specifying the
  150. expected foreign key relationship's (table, column).
  151. """
  152. with connection.cursor() as cursor:
  153. constraints = connection.introspection.get_constraints(cursor, table)
  154. counts = {'fks': 0, 'uniques': 0, 'indexes': 0}
  155. for c in constraints.values():
  156. if c['columns'] == [column]:
  157. if c['foreign_key'] == fk_to:
  158. counts['fks'] += 1
  159. if c['unique']:
  160. counts['uniques'] += 1
  161. elif c['index']:
  162. counts['indexes'] += 1
  163. return counts
  164. def assertIndexOrder(self, table, index, order):
  165. constraints = self.get_constraints(table)
  166. self.assertIn(index, constraints)
  167. index_orders = constraints[index]['orders']
  168. self.assertTrue(all(val == expected for val, expected in zip(index_orders, order)))
  169. def assertForeignKeyExists(self, model, column, expected_fk_table, field='id'):
  170. """
  171. Fail if the FK constraint on `model.Meta.db_table`.`column` to
  172. `expected_fk_table`.id doesn't exist.
  173. """
  174. constraints = self.get_constraints(model._meta.db_table)
  175. constraint_fk = None
  176. for details in constraints.values():
  177. if details['columns'] == [column] and details['foreign_key']:
  178. constraint_fk = details['foreign_key']
  179. break
  180. self.assertEqual(constraint_fk, (expected_fk_table, field))
  181. def assertForeignKeyNotExists(self, model, column, expected_fk_table):
  182. with self.assertRaises(AssertionError):
  183. self.assertForeignKeyExists(model, column, expected_fk_table)
  184. # Tests
  185. def test_creation_deletion(self):
  186. """
  187. Tries creating a model's table, and then deleting it.
  188. """
  189. with connection.schema_editor() as editor:
  190. # Create the table
  191. editor.create_model(Author)
  192. # The table is there
  193. list(Author.objects.all())
  194. # Clean up that table
  195. editor.delete_model(Author)
  196. # No deferred SQL should be left over.
  197. self.assertEqual(editor.deferred_sql, [])
  198. # The table is gone
  199. with self.assertRaises(DatabaseError):
  200. list(Author.objects.all())
  201. @skipUnlessDBFeature('supports_foreign_keys')
  202. def test_fk(self):
  203. "Creating tables out of FK order, then repointing, works"
  204. # Create the table
  205. with connection.schema_editor() as editor:
  206. editor.create_model(Book)
  207. editor.create_model(Author)
  208. editor.create_model(Tag)
  209. # Initial tables are there
  210. list(Author.objects.all())
  211. list(Book.objects.all())
  212. # Make sure the FK constraint is present
  213. with self.assertRaises(IntegrityError):
  214. Book.objects.create(
  215. author_id=1,
  216. title="Much Ado About Foreign Keys",
  217. pub_date=datetime.datetime.now(),
  218. )
  219. # Repoint the FK constraint
  220. old_field = Book._meta.get_field("author")
  221. new_field = ForeignKey(Tag, CASCADE)
  222. new_field.set_attributes_from_name("author")
  223. with connection.schema_editor() as editor:
  224. editor.alter_field(Book, old_field, new_field, strict=True)
  225. self.assertForeignKeyExists(Book, 'author_id', 'schema_tag')
  226. @skipUnlessDBFeature('can_create_inline_fk')
  227. def test_inline_fk(self):
  228. # Create some tables.
  229. with connection.schema_editor() as editor:
  230. editor.create_model(Author)
  231. editor.create_model(Book)
  232. editor.create_model(Note)
  233. self.assertForeignKeyNotExists(Note, 'book_id', 'schema_book')
  234. # Add a foreign key from one to the other.
  235. with connection.schema_editor() as editor:
  236. new_field = ForeignKey(Book, CASCADE)
  237. new_field.set_attributes_from_name('book')
  238. editor.add_field(Note, new_field)
  239. self.assertForeignKeyExists(Note, 'book_id', 'schema_book')
  240. # Creating a FK field with a constraint uses a single statement without
  241. # a deferred ALTER TABLE.
  242. self.assertFalse([
  243. sql for sql in (str(statement) for statement in editor.deferred_sql)
  244. if sql.startswith('ALTER TABLE') and 'ADD CONSTRAINT' in sql
  245. ])
  246. @skipUnlessDBFeature('can_create_inline_fk')
  247. def test_add_inline_fk_update_data(self):
  248. with connection.schema_editor() as editor:
  249. editor.create_model(Node)
  250. # Add an inline foreign key and update data in the same transaction.
  251. new_field = ForeignKey(Node, CASCADE, related_name='new_fk', null=True)
  252. new_field.set_attributes_from_name('new_parent_fk')
  253. parent = Node.objects.create()
  254. with connection.schema_editor() as editor:
  255. editor.add_field(Node, new_field)
  256. editor.execute('UPDATE schema_node SET new_parent_fk_id = %s;', [parent.pk])
  257. self.assertIn('new_parent_fk_id', self.get_indexes(Node._meta.db_table))
  258. @skipUnlessDBFeature(
  259. 'can_create_inline_fk',
  260. 'allows_multiple_constraints_on_same_fields',
  261. )
  262. @isolate_apps('schema')
  263. def test_add_inline_fk_index_update_data(self):
  264. class Node(Model):
  265. class Meta:
  266. app_label = 'schema'
  267. with connection.schema_editor() as editor:
  268. editor.create_model(Node)
  269. # Add an inline foreign key, update data, and an index in the same
  270. # transaction.
  271. new_field = ForeignKey(Node, CASCADE, related_name='new_fk', null=True)
  272. new_field.set_attributes_from_name('new_parent_fk')
  273. parent = Node.objects.create()
  274. with connection.schema_editor() as editor:
  275. editor.add_field(Node, new_field)
  276. Node._meta.add_field(new_field)
  277. editor.execute('UPDATE schema_node SET new_parent_fk_id = %s;', [parent.pk])
  278. editor.add_index(Node, Index(fields=['new_parent_fk'], name='new_parent_inline_fk_idx'))
  279. self.assertIn('new_parent_fk_id', self.get_indexes(Node._meta.db_table))
  280. @skipUnlessDBFeature('supports_foreign_keys')
  281. def test_char_field_with_db_index_to_fk(self):
  282. # Create the table
  283. with connection.schema_editor() as editor:
  284. editor.create_model(Author)
  285. editor.create_model(AuthorCharFieldWithIndex)
  286. # Change CharField to FK
  287. old_field = AuthorCharFieldWithIndex._meta.get_field('char_field')
  288. new_field = ForeignKey(Author, CASCADE, blank=True)
  289. new_field.set_attributes_from_name('char_field')
  290. with connection.schema_editor() as editor:
  291. editor.alter_field(AuthorCharFieldWithIndex, old_field, new_field, strict=True)
  292. self.assertForeignKeyExists(AuthorCharFieldWithIndex, 'char_field_id', 'schema_author')
  293. @skipUnlessDBFeature('supports_foreign_keys')
  294. @skipUnlessDBFeature('supports_index_on_text_field')
  295. def test_text_field_with_db_index_to_fk(self):
  296. # Create the table
  297. with connection.schema_editor() as editor:
  298. editor.create_model(Author)
  299. editor.create_model(AuthorTextFieldWithIndex)
  300. # Change TextField to FK
  301. old_field = AuthorTextFieldWithIndex._meta.get_field('text_field')
  302. new_field = ForeignKey(Author, CASCADE, blank=True)
  303. new_field.set_attributes_from_name('text_field')
  304. with connection.schema_editor() as editor:
  305. editor.alter_field(AuthorTextFieldWithIndex, old_field, new_field, strict=True)
  306. self.assertForeignKeyExists(AuthorTextFieldWithIndex, 'text_field_id', 'schema_author')
  307. @isolate_apps('schema')
  308. def test_char_field_pk_to_auto_field(self):
  309. class Foo(Model):
  310. id = CharField(max_length=255, primary_key=True)
  311. class Meta:
  312. app_label = 'schema'
  313. with connection.schema_editor() as editor:
  314. editor.create_model(Foo)
  315. self.isolated_local_models = [Foo]
  316. old_field = Foo._meta.get_field('id')
  317. new_field = AutoField(primary_key=True)
  318. new_field.set_attributes_from_name('id')
  319. new_field.model = Foo
  320. with connection.schema_editor() as editor:
  321. editor.alter_field(Foo, old_field, new_field, strict=True)
  322. @skipUnlessDBFeature('supports_foreign_keys')
  323. def test_fk_to_proxy(self):
  324. "Creating a FK to a proxy model creates database constraints."
  325. class AuthorProxy(Author):
  326. class Meta:
  327. app_label = 'schema'
  328. apps = new_apps
  329. proxy = True
  330. class AuthorRef(Model):
  331. author = ForeignKey(AuthorProxy, on_delete=CASCADE)
  332. class Meta:
  333. app_label = 'schema'
  334. apps = new_apps
  335. self.local_models = [AuthorProxy, AuthorRef]
  336. # Create the table
  337. with connection.schema_editor() as editor:
  338. editor.create_model(Author)
  339. editor.create_model(AuthorRef)
  340. self.assertForeignKeyExists(AuthorRef, 'author_id', 'schema_author')
  341. @skipUnlessDBFeature('supports_foreign_keys')
  342. def test_fk_db_constraint(self):
  343. "The db_constraint parameter is respected"
  344. # Create the table
  345. with connection.schema_editor() as editor:
  346. editor.create_model(Tag)
  347. editor.create_model(Author)
  348. editor.create_model(BookWeak)
  349. # Initial tables are there
  350. list(Author.objects.all())
  351. list(Tag.objects.all())
  352. list(BookWeak.objects.all())
  353. self.assertForeignKeyNotExists(BookWeak, 'author_id', 'schema_author')
  354. # Make a db_constraint=False FK
  355. new_field = ForeignKey(Tag, CASCADE, db_constraint=False)
  356. new_field.set_attributes_from_name("tag")
  357. with connection.schema_editor() as editor:
  358. editor.add_field(Author, new_field)
  359. self.assertForeignKeyNotExists(Author, 'tag_id', 'schema_tag')
  360. # Alter to one with a constraint
  361. new_field2 = ForeignKey(Tag, CASCADE)
  362. new_field2.set_attributes_from_name("tag")
  363. with connection.schema_editor() as editor:
  364. editor.alter_field(Author, new_field, new_field2, strict=True)
  365. self.assertForeignKeyExists(Author, 'tag_id', 'schema_tag')
  366. # Alter to one without a constraint again
  367. new_field2 = ForeignKey(Tag, CASCADE)
  368. new_field2.set_attributes_from_name("tag")
  369. with connection.schema_editor() as editor:
  370. editor.alter_field(Author, new_field2, new_field, strict=True)
  371. self.assertForeignKeyNotExists(Author, 'tag_id', 'schema_tag')
  372. @isolate_apps('schema')
  373. def test_no_db_constraint_added_during_primary_key_change(self):
  374. """
  375. When a primary key that's pointed to by a ForeignKey with
  376. db_constraint=False is altered, a foreign key constraint isn't added.
  377. """
  378. class Author(Model):
  379. class Meta:
  380. app_label = 'schema'
  381. class BookWeak(Model):
  382. author = ForeignKey(Author, CASCADE, db_constraint=False)
  383. class Meta:
  384. app_label = 'schema'
  385. with connection.schema_editor() as editor:
  386. editor.create_model(Author)
  387. editor.create_model(BookWeak)
  388. self.assertForeignKeyNotExists(BookWeak, 'author_id', 'schema_author')
  389. old_field = Author._meta.get_field('id')
  390. new_field = BigAutoField(primary_key=True)
  391. new_field.model = Author
  392. new_field.set_attributes_from_name('id')
  393. # @isolate_apps() and inner models are needed to have the model
  394. # relations populated, otherwise this doesn't act as a regression test.
  395. self.assertEqual(len(new_field.model._meta.related_objects), 1)
  396. with connection.schema_editor() as editor:
  397. editor.alter_field(Author, old_field, new_field, strict=True)
  398. self.assertForeignKeyNotExists(BookWeak, 'author_id', 'schema_author')
  399. def _test_m2m_db_constraint(self, M2MFieldClass):
  400. class LocalAuthorWithM2M(Model):
  401. name = CharField(max_length=255)
  402. class Meta:
  403. app_label = 'schema'
  404. apps = new_apps
  405. self.local_models = [LocalAuthorWithM2M]
  406. # Create the table
  407. with connection.schema_editor() as editor:
  408. editor.create_model(Tag)
  409. editor.create_model(LocalAuthorWithM2M)
  410. # Initial tables are there
  411. list(LocalAuthorWithM2M.objects.all())
  412. list(Tag.objects.all())
  413. # Make a db_constraint=False FK
  414. new_field = M2MFieldClass(Tag, related_name="authors", db_constraint=False)
  415. new_field.contribute_to_class(LocalAuthorWithM2M, "tags")
  416. # Add the field
  417. with connection.schema_editor() as editor:
  418. editor.add_field(LocalAuthorWithM2M, new_field)
  419. self.assertForeignKeyNotExists(new_field.remote_field.through, 'tag_id', 'schema_tag')
  420. @skipUnlessDBFeature('supports_foreign_keys')
  421. def test_m2m_db_constraint(self):
  422. self._test_m2m_db_constraint(ManyToManyField)
  423. @skipUnlessDBFeature('supports_foreign_keys')
  424. def test_m2m_db_constraint_custom(self):
  425. self._test_m2m_db_constraint(CustomManyToManyField)
  426. @skipUnlessDBFeature('supports_foreign_keys')
  427. def test_m2m_db_constraint_inherited(self):
  428. self._test_m2m_db_constraint(InheritedManyToManyField)
  429. def test_add_field(self):
  430. """
  431. Tests adding fields to models
  432. """
  433. # Create the table
  434. with connection.schema_editor() as editor:
  435. editor.create_model(Author)
  436. # Ensure there's no age field
  437. columns = self.column_classes(Author)
  438. self.assertNotIn("age", columns)
  439. # Add the new field
  440. new_field = IntegerField(null=True)
  441. new_field.set_attributes_from_name("age")
  442. with CaptureQueriesContext(connection) as ctx, connection.schema_editor() as editor:
  443. editor.add_field(Author, new_field)
  444. drop_default_sql = editor.sql_alter_column_no_default % {
  445. 'column': editor.quote_name(new_field.name),
  446. }
  447. self.assertFalse(any(drop_default_sql in query['sql'] for query in ctx.captured_queries))
  448. # Ensure the field is right afterwards
  449. columns = self.column_classes(Author)
  450. self.assertEqual(columns['age'][0], connection.features.introspected_field_types['IntegerField'])
  451. self.assertTrue(columns['age'][1][6])
  452. def test_add_field_remove_field(self):
  453. """
  454. Adding a field and removing it removes all deferred sql referring to it.
  455. """
  456. with connection.schema_editor() as editor:
  457. # Create a table with a unique constraint on the slug field.
  458. editor.create_model(Tag)
  459. # Remove the slug column.
  460. editor.remove_field(Tag, Tag._meta.get_field('slug'))
  461. self.assertEqual(editor.deferred_sql, [])
  462. def test_add_field_temp_default(self):
  463. """
  464. Tests adding fields to models with a temporary default
  465. """
  466. # Create the table
  467. with connection.schema_editor() as editor:
  468. editor.create_model(Author)
  469. # Ensure there's no age field
  470. columns = self.column_classes(Author)
  471. self.assertNotIn("age", columns)
  472. # Add some rows of data
  473. Author.objects.create(name="Andrew", height=30)
  474. Author.objects.create(name="Andrea")
  475. # Add a not-null field
  476. new_field = CharField(max_length=30, default="Godwin")
  477. new_field.set_attributes_from_name("surname")
  478. with connection.schema_editor() as editor:
  479. editor.add_field(Author, new_field)
  480. # Ensure the field is right afterwards
  481. columns = self.column_classes(Author)
  482. self.assertEqual(columns['surname'][0], connection.features.introspected_field_types['CharField'])
  483. self.assertEqual(columns['surname'][1][6],
  484. connection.features.interprets_empty_strings_as_nulls)
  485. def test_add_field_temp_default_boolean(self):
  486. """
  487. Tests adding fields to models with a temporary default where
  488. the default is False. (#21783)
  489. """
  490. # Create the table
  491. with connection.schema_editor() as editor:
  492. editor.create_model(Author)
  493. # Ensure there's no age field
  494. columns = self.column_classes(Author)
  495. self.assertNotIn("age", columns)
  496. # Add some rows of data
  497. Author.objects.create(name="Andrew", height=30)
  498. Author.objects.create(name="Andrea")
  499. # Add a not-null field
  500. new_field = BooleanField(default=False)
  501. new_field.set_attributes_from_name("awesome")
  502. with connection.schema_editor() as editor:
  503. editor.add_field(Author, new_field)
  504. # Ensure the field is right afterwards
  505. columns = self.column_classes(Author)
  506. # BooleanField are stored as TINYINT(1) on MySQL.
  507. field_type = columns['awesome'][0]
  508. self.assertEqual(field_type, connection.features.introspected_field_types['BooleanField'])
  509. def test_add_field_default_transform(self):
  510. """
  511. Tests adding fields to models with a default that is not directly
  512. valid in the database (#22581)
  513. """
  514. class TestTransformField(IntegerField):
  515. # Weird field that saves the count of items in its value
  516. def get_default(self):
  517. return self.default
  518. def get_prep_value(self, value):
  519. if value is None:
  520. return 0
  521. return len(value)
  522. # Create the table
  523. with connection.schema_editor() as editor:
  524. editor.create_model(Author)
  525. # Add some rows of data
  526. Author.objects.create(name="Andrew", height=30)
  527. Author.objects.create(name="Andrea")
  528. # Add the field with a default it needs to cast (to string in this case)
  529. new_field = TestTransformField(default={1: 2})
  530. new_field.set_attributes_from_name("thing")
  531. with connection.schema_editor() as editor:
  532. editor.add_field(Author, new_field)
  533. # Ensure the field is there
  534. columns = self.column_classes(Author)
  535. field_type, field_info = columns['thing']
  536. self.assertEqual(field_type, connection.features.introspected_field_types['IntegerField'])
  537. # Make sure the values were transformed correctly
  538. self.assertEqual(Author.objects.extra(where=["thing = 1"]).count(), 2)
  539. def test_add_field_binary(self):
  540. """
  541. Tests binary fields get a sane default (#22851)
  542. """
  543. # Create the table
  544. with connection.schema_editor() as editor:
  545. editor.create_model(Author)
  546. # Add the new field
  547. new_field = BinaryField(blank=True)
  548. new_field.set_attributes_from_name("bits")
  549. with connection.schema_editor() as editor:
  550. editor.add_field(Author, new_field)
  551. # Ensure the field is right afterwards
  552. columns = self.column_classes(Author)
  553. # MySQL annoyingly uses the same backend, so it'll come back as one of
  554. # these two types.
  555. self.assertIn(columns['bits'][0], ("BinaryField", "TextField"))
  556. @unittest.skipUnless(connection.vendor == 'mysql', "MySQL specific")
  557. def test_add_binaryfield_mediumblob(self):
  558. """
  559. Test adding a custom-sized binary field on MySQL (#24846).
  560. """
  561. # Create the table
  562. with connection.schema_editor() as editor:
  563. editor.create_model(Author)
  564. # Add the new field with default
  565. new_field = MediumBlobField(blank=True, default=b'123')
  566. new_field.set_attributes_from_name('bits')
  567. with connection.schema_editor() as editor:
  568. editor.add_field(Author, new_field)
  569. columns = self.column_classes(Author)
  570. # Introspection treats BLOBs as TextFields
  571. self.assertEqual(columns['bits'][0], "TextField")
  572. def test_alter(self):
  573. """
  574. Tests simple altering of fields
  575. """
  576. # Create the table
  577. with connection.schema_editor() as editor:
  578. editor.create_model(Author)
  579. # Ensure the field is right to begin with
  580. columns = self.column_classes(Author)
  581. self.assertEqual(columns['name'][0], connection.features.introspected_field_types['CharField'])
  582. self.assertEqual(bool(columns['name'][1][6]), bool(connection.features.interprets_empty_strings_as_nulls))
  583. # Alter the name field to a TextField
  584. old_field = Author._meta.get_field("name")
  585. new_field = TextField(null=True)
  586. new_field.set_attributes_from_name("name")
  587. with connection.schema_editor() as editor:
  588. editor.alter_field(Author, old_field, new_field, strict=True)
  589. # Ensure the field is right afterwards
  590. columns = self.column_classes(Author)
  591. self.assertEqual(columns['name'][0], "TextField")
  592. self.assertTrue(columns['name'][1][6])
  593. # Change nullability again
  594. new_field2 = TextField(null=False)
  595. new_field2.set_attributes_from_name("name")
  596. with connection.schema_editor() as editor:
  597. editor.alter_field(Author, new_field, new_field2, strict=True)
  598. # Ensure the field is right afterwards
  599. columns = self.column_classes(Author)
  600. self.assertEqual(columns['name'][0], "TextField")
  601. self.assertEqual(bool(columns['name'][1][6]), bool(connection.features.interprets_empty_strings_as_nulls))
  602. def test_alter_auto_field_to_integer_field(self):
  603. # Create the table
  604. with connection.schema_editor() as editor:
  605. editor.create_model(Author)
  606. # Change AutoField to IntegerField
  607. old_field = Author._meta.get_field('id')
  608. new_field = IntegerField(primary_key=True)
  609. new_field.set_attributes_from_name('id')
  610. new_field.model = Author
  611. with connection.schema_editor() as editor:
  612. editor.alter_field(Author, old_field, new_field, strict=True)
  613. def test_alter_auto_field_to_char_field(self):
  614. # Create the table
  615. with connection.schema_editor() as editor:
  616. editor.create_model(Author)
  617. # Change AutoField to CharField
  618. old_field = Author._meta.get_field('id')
  619. new_field = CharField(primary_key=True, max_length=50)
  620. new_field.set_attributes_from_name('id')
  621. new_field.model = Author
  622. with connection.schema_editor() as editor:
  623. editor.alter_field(Author, old_field, new_field, strict=True)
  624. @isolate_apps('schema')
  625. def test_alter_auto_field_quoted_db_column(self):
  626. class Foo(Model):
  627. id = AutoField(primary_key=True, db_column='"quoted_id"')
  628. class Meta:
  629. app_label = 'schema'
  630. with connection.schema_editor() as editor:
  631. editor.create_model(Foo)
  632. self.isolated_local_models = [Foo]
  633. old_field = Foo._meta.get_field('id')
  634. new_field = BigAutoField(primary_key=True)
  635. new_field.model = Foo
  636. new_field.db_column = '"quoted_id"'
  637. new_field.set_attributes_from_name('id')
  638. with connection.schema_editor() as editor:
  639. editor.alter_field(Foo, old_field, new_field, strict=True)
  640. Foo.objects.create()
  641. def test_alter_not_unique_field_to_primary_key(self):
  642. # Create the table.
  643. with connection.schema_editor() as editor:
  644. editor.create_model(Author)
  645. # Change UUIDField to primary key.
  646. old_field = Author._meta.get_field('uuid')
  647. new_field = UUIDField(primary_key=True)
  648. new_field.set_attributes_from_name('uuid')
  649. new_field.model = Author
  650. with connection.schema_editor() as editor:
  651. editor.remove_field(Author, Author._meta.get_field('id'))
  652. editor.alter_field(Author, old_field, new_field, strict=True)
  653. @isolate_apps('schema')
  654. def test_alter_primary_key_quoted_db_table(self):
  655. class Foo(Model):
  656. class Meta:
  657. app_label = 'schema'
  658. db_table = '"foo"'
  659. with connection.schema_editor() as editor:
  660. editor.create_model(Foo)
  661. self.isolated_local_models = [Foo]
  662. old_field = Foo._meta.get_field('id')
  663. new_field = BigAutoField(primary_key=True)
  664. new_field.model = Foo
  665. new_field.set_attributes_from_name('id')
  666. with connection.schema_editor() as editor:
  667. editor.alter_field(Foo, old_field, new_field, strict=True)
  668. Foo.objects.create()
  669. def test_alter_text_field(self):
  670. # Regression for "BLOB/TEXT column 'info' can't have a default value")
  671. # on MySQL.
  672. # Create the table
  673. with connection.schema_editor() as editor:
  674. editor.create_model(Note)
  675. old_field = Note._meta.get_field("info")
  676. new_field = TextField(blank=True)
  677. new_field.set_attributes_from_name("info")
  678. with connection.schema_editor() as editor:
  679. editor.alter_field(Note, old_field, new_field, strict=True)
  680. @skipUnlessDBFeature('can_defer_constraint_checks', 'can_rollback_ddl')
  681. def test_alter_fk_checks_deferred_constraints(self):
  682. """
  683. #25492 - Altering a foreign key's structure and data in the same
  684. transaction.
  685. """
  686. with connection.schema_editor() as editor:
  687. editor.create_model(Node)
  688. old_field = Node._meta.get_field('parent')
  689. new_field = ForeignKey(Node, CASCADE)
  690. new_field.set_attributes_from_name('parent')
  691. parent = Node.objects.create()
  692. with connection.schema_editor() as editor:
  693. # Update the parent FK to create a deferred constraint check.
  694. Node.objects.update(parent=parent)
  695. editor.alter_field(Node, old_field, new_field, strict=True)
  696. def test_alter_text_field_to_date_field(self):
  697. """
  698. #25002 - Test conversion of text field to date field.
  699. """
  700. with connection.schema_editor() as editor:
  701. editor.create_model(Note)
  702. Note.objects.create(info='1988-05-05')
  703. old_field = Note._meta.get_field('info')
  704. new_field = DateField(blank=True)
  705. new_field.set_attributes_from_name('info')
  706. with connection.schema_editor() as editor:
  707. editor.alter_field(Note, old_field, new_field, strict=True)
  708. # Make sure the field isn't nullable
  709. columns = self.column_classes(Note)
  710. self.assertFalse(columns['info'][1][6])
  711. def test_alter_text_field_to_datetime_field(self):
  712. """
  713. #25002 - Test conversion of text field to datetime field.
  714. """
  715. with connection.schema_editor() as editor:
  716. editor.create_model(Note)
  717. Note.objects.create(info='1988-05-05 3:16:17.4567')
  718. old_field = Note._meta.get_field('info')
  719. new_field = DateTimeField(blank=True)
  720. new_field.set_attributes_from_name('info')
  721. with connection.schema_editor() as editor:
  722. editor.alter_field(Note, old_field, new_field, strict=True)
  723. # Make sure the field isn't nullable
  724. columns = self.column_classes(Note)
  725. self.assertFalse(columns['info'][1][6])
  726. def test_alter_text_field_to_time_field(self):
  727. """
  728. #25002 - Test conversion of text field to time field.
  729. """
  730. with connection.schema_editor() as editor:
  731. editor.create_model(Note)
  732. Note.objects.create(info='3:16:17.4567')
  733. old_field = Note._meta.get_field('info')
  734. new_field = TimeField(blank=True)
  735. new_field.set_attributes_from_name('info')
  736. with connection.schema_editor() as editor:
  737. editor.alter_field(Note, old_field, new_field, strict=True)
  738. # Make sure the field isn't nullable
  739. columns = self.column_classes(Note)
  740. self.assertFalse(columns['info'][1][6])
  741. @skipIfDBFeature('interprets_empty_strings_as_nulls')
  742. def test_alter_textual_field_keep_null_status(self):
  743. """
  744. Changing a field type shouldn't affect the not null status.
  745. """
  746. with connection.schema_editor() as editor:
  747. editor.create_model(Note)
  748. with self.assertRaises(IntegrityError):
  749. Note.objects.create(info=None)
  750. old_field = Note._meta.get_field("info")
  751. new_field = CharField(max_length=50)
  752. new_field.set_attributes_from_name("info")
  753. with connection.schema_editor() as editor:
  754. editor.alter_field(Note, old_field, new_field, strict=True)
  755. with self.assertRaises(IntegrityError):
  756. Note.objects.create(info=None)
  757. def test_alter_numeric_field_keep_null_status(self):
  758. """
  759. Changing a field type shouldn't affect the not null status.
  760. """
  761. with connection.schema_editor() as editor:
  762. editor.create_model(UniqueTest)
  763. with self.assertRaises(IntegrityError):
  764. UniqueTest.objects.create(year=None, slug='aaa')
  765. old_field = UniqueTest._meta.get_field("year")
  766. new_field = BigIntegerField()
  767. new_field.set_attributes_from_name("year")
  768. with connection.schema_editor() as editor:
  769. editor.alter_field(UniqueTest, old_field, new_field, strict=True)
  770. with self.assertRaises(IntegrityError):
  771. UniqueTest.objects.create(year=None, slug='bbb')
  772. def test_alter_null_to_not_null(self):
  773. """
  774. #23609 - Tests handling of default values when altering from NULL to NOT NULL.
  775. """
  776. # Create the table
  777. with connection.schema_editor() as editor:
  778. editor.create_model(Author)
  779. # Ensure the field is right to begin with
  780. columns = self.column_classes(Author)
  781. self.assertTrue(columns['height'][1][6])
  782. # Create some test data
  783. Author.objects.create(name='Not null author', height=12)
  784. Author.objects.create(name='Null author')
  785. # Verify null value
  786. self.assertEqual(Author.objects.get(name='Not null author').height, 12)
  787. self.assertIsNone(Author.objects.get(name='Null author').height)
  788. # Alter the height field to NOT NULL with default
  789. old_field = Author._meta.get_field("height")
  790. new_field = PositiveIntegerField(default=42)
  791. new_field.set_attributes_from_name("height")
  792. with connection.schema_editor() as editor:
  793. editor.alter_field(Author, old_field, new_field, strict=True)
  794. # Ensure the field is right afterwards
  795. columns = self.column_classes(Author)
  796. self.assertFalse(columns['height'][1][6])
  797. # Verify default value
  798. self.assertEqual(Author.objects.get(name='Not null author').height, 12)
  799. self.assertEqual(Author.objects.get(name='Null author').height, 42)
  800. def test_alter_charfield_to_null(self):
  801. """
  802. #24307 - Should skip an alter statement on databases with
  803. interprets_empty_strings_as_null when changing a CharField to null.
  804. """
  805. # Create the table
  806. with connection.schema_editor() as editor:
  807. editor.create_model(Author)
  808. # Change the CharField to null
  809. old_field = Author._meta.get_field('name')
  810. new_field = copy(old_field)
  811. new_field.null = True
  812. with connection.schema_editor() as editor:
  813. editor.alter_field(Author, old_field, new_field, strict=True)
  814. @unittest.skipUnless(connection.vendor == 'postgresql', 'PostgreSQL specific')
  815. def test_alter_char_field_decrease_length(self):
  816. # Create the table.
  817. with connection.schema_editor() as editor:
  818. editor.create_model(Author)
  819. Author.objects.create(name='x' * 255)
  820. # Change max_length of CharField.
  821. old_field = Author._meta.get_field('name')
  822. new_field = CharField(max_length=254)
  823. new_field.set_attributes_from_name('name')
  824. with connection.schema_editor() as editor:
  825. msg = 'value too long for type character varying(254)'
  826. with self.assertRaisesMessage(DataError, msg):
  827. editor.alter_field(Author, old_field, new_field, strict=True)
  828. @unittest.skipUnless(connection.vendor == 'postgresql', 'PostgreSQL specific')
  829. def test_alter_field_with_custom_db_type(self):
  830. from django.contrib.postgres.fields import ArrayField
  831. class Foo(Model):
  832. field = ArrayField(CharField(max_length=255))
  833. class Meta:
  834. app_label = 'schema'
  835. with connection.schema_editor() as editor:
  836. editor.create_model(Foo)
  837. self.isolated_local_models = [Foo]
  838. old_field = Foo._meta.get_field('field')
  839. new_field = ArrayField(CharField(max_length=16))
  840. new_field.set_attributes_from_name('field')
  841. new_field.model = Foo
  842. with connection.schema_editor() as editor:
  843. editor.alter_field(Foo, old_field, new_field, strict=True)
  844. @isolate_apps('schema')
  845. @unittest.skipUnless(connection.vendor == 'postgresql', 'PostgreSQL specific')
  846. def test_alter_array_field_decrease_base_field_length(self):
  847. from django.contrib.postgres.fields import ArrayField
  848. class ArrayModel(Model):
  849. field = ArrayField(CharField(max_length=16))
  850. class Meta:
  851. app_label = 'schema'
  852. with connection.schema_editor() as editor:
  853. editor.create_model(ArrayModel)
  854. self.isolated_local_models = [ArrayModel]
  855. ArrayModel.objects.create(field=['x' * 16])
  856. old_field = ArrayModel._meta.get_field('field')
  857. new_field = ArrayField(CharField(max_length=15))
  858. new_field.set_attributes_from_name('field')
  859. new_field.model = ArrayModel
  860. with connection.schema_editor() as editor:
  861. msg = 'value too long for type character varying(15)'
  862. with self.assertRaisesMessage(DataError, msg):
  863. editor.alter_field(ArrayModel, old_field, new_field, strict=True)
  864. @isolate_apps('schema')
  865. @unittest.skipUnless(connection.vendor == 'postgresql', 'PostgreSQL specific')
  866. def test_alter_array_field_decrease_nested_base_field_length(self):
  867. from django.contrib.postgres.fields import ArrayField
  868. class ArrayModel(Model):
  869. field = ArrayField(ArrayField(CharField(max_length=16)))
  870. class Meta:
  871. app_label = 'schema'
  872. with connection.schema_editor() as editor:
  873. editor.create_model(ArrayModel)
  874. self.isolated_local_models = [ArrayModel]
  875. ArrayModel.objects.create(field=[['x' * 16]])
  876. old_field = ArrayModel._meta.get_field('field')
  877. new_field = ArrayField(ArrayField(CharField(max_length=15)))
  878. new_field.set_attributes_from_name('field')
  879. new_field.model = ArrayModel
  880. with connection.schema_editor() as editor:
  881. msg = 'value too long for type character varying(15)'
  882. with self.assertRaisesMessage(DataError, msg):
  883. editor.alter_field(ArrayModel, old_field, new_field, strict=True)
  884. def test_alter_textfield_to_null(self):
  885. """
  886. #24307 - Should skip an alter statement on databases with
  887. interprets_empty_strings_as_null when changing a TextField to null.
  888. """
  889. # Create the table
  890. with connection.schema_editor() as editor:
  891. editor.create_model(Note)
  892. # Change the TextField to null
  893. old_field = Note._meta.get_field('info')
  894. new_field = copy(old_field)
  895. new_field.null = True
  896. with connection.schema_editor() as editor:
  897. editor.alter_field(Note, old_field, new_field, strict=True)
  898. @skipUnlessDBFeature('supports_combined_alters')
  899. def test_alter_null_to_not_null_keeping_default(self):
  900. """
  901. #23738 - Can change a nullable field with default to non-nullable
  902. with the same default.
  903. """
  904. # Create the table
  905. with connection.schema_editor() as editor:
  906. editor.create_model(AuthorWithDefaultHeight)
  907. # Ensure the field is right to begin with
  908. columns = self.column_classes(AuthorWithDefaultHeight)
  909. self.assertTrue(columns['height'][1][6])
  910. # Alter the height field to NOT NULL keeping the previous default
  911. old_field = AuthorWithDefaultHeight._meta.get_field("height")
  912. new_field = PositiveIntegerField(default=42)
  913. new_field.set_attributes_from_name("height")
  914. with connection.schema_editor() as editor:
  915. editor.alter_field(AuthorWithDefaultHeight, old_field, new_field, strict=True)
  916. # Ensure the field is right afterwards
  917. columns = self.column_classes(AuthorWithDefaultHeight)
  918. self.assertFalse(columns['height'][1][6])
  919. @skipUnlessDBFeature('supports_foreign_keys')
  920. def test_alter_fk(self):
  921. """
  922. Tests altering of FKs
  923. """
  924. # Create the table
  925. with connection.schema_editor() as editor:
  926. editor.create_model(Author)
  927. editor.create_model(Book)
  928. # Ensure the field is right to begin with
  929. columns = self.column_classes(Book)
  930. self.assertEqual(columns['author_id'][0], connection.features.introspected_field_types['IntegerField'])
  931. self.assertForeignKeyExists(Book, 'author_id', 'schema_author')
  932. # Alter the FK
  933. old_field = Book._meta.get_field("author")
  934. new_field = ForeignKey(Author, CASCADE, editable=False)
  935. new_field.set_attributes_from_name("author")
  936. with connection.schema_editor() as editor:
  937. editor.alter_field(Book, old_field, new_field, strict=True)
  938. # Ensure the field is right afterwards
  939. columns = self.column_classes(Book)
  940. self.assertEqual(columns['author_id'][0], connection.features.introspected_field_types['IntegerField'])
  941. self.assertForeignKeyExists(Book, 'author_id', 'schema_author')
  942. @skipUnlessDBFeature('supports_foreign_keys')
  943. def test_alter_to_fk(self):
  944. """
  945. #24447 - Tests adding a FK constraint for an existing column
  946. """
  947. class LocalBook(Model):
  948. author = IntegerField()
  949. title = CharField(max_length=100, db_index=True)
  950. pub_date = DateTimeField()
  951. class Meta:
  952. app_label = 'schema'
  953. apps = new_apps
  954. self.local_models = [LocalBook]
  955. # Create the tables
  956. with connection.schema_editor() as editor:
  957. editor.create_model(Author)
  958. editor.create_model(LocalBook)
  959. # Ensure no FK constraint exists
  960. constraints = self.get_constraints(LocalBook._meta.db_table)
  961. for details in constraints.values():
  962. if details['foreign_key']:
  963. self.fail('Found an unexpected FK constraint to %s' % details['columns'])
  964. old_field = LocalBook._meta.get_field("author")
  965. new_field = ForeignKey(Author, CASCADE)
  966. new_field.set_attributes_from_name("author")
  967. with connection.schema_editor() as editor:
  968. editor.alter_field(LocalBook, old_field, new_field, strict=True)
  969. self.assertForeignKeyExists(LocalBook, 'author_id', 'schema_author')
  970. @skipUnlessDBFeature('supports_foreign_keys')
  971. def test_alter_o2o_to_fk(self):
  972. """
  973. #24163 - Tests altering of OneToOneField to ForeignKey
  974. """
  975. # Create the table
  976. with connection.schema_editor() as editor:
  977. editor.create_model(Author)
  978. editor.create_model(BookWithO2O)
  979. # Ensure the field is right to begin with
  980. columns = self.column_classes(BookWithO2O)
  981. self.assertEqual(columns['author_id'][0], connection.features.introspected_field_types['IntegerField'])
  982. # Ensure the field is unique
  983. author = Author.objects.create(name="Joe")
  984. BookWithO2O.objects.create(author=author, title="Django 1", pub_date=datetime.datetime.now())
  985. with self.assertRaises(IntegrityError):
  986. BookWithO2O.objects.create(author=author, title="Django 2", pub_date=datetime.datetime.now())
  987. BookWithO2O.objects.all().delete()
  988. self.assertForeignKeyExists(BookWithO2O, 'author_id', 'schema_author')
  989. # Alter the OneToOneField to ForeignKey
  990. old_field = BookWithO2O._meta.get_field("author")
  991. new_field = ForeignKey(Author, CASCADE)
  992. new_field.set_attributes_from_name("author")
  993. with connection.schema_editor() as editor:
  994. editor.alter_field(BookWithO2O, old_field, new_field, strict=True)
  995. # Ensure the field is right afterwards
  996. columns = self.column_classes(Book)
  997. self.assertEqual(columns['author_id'][0], connection.features.introspected_field_types['IntegerField'])
  998. # Ensure the field is not unique anymore
  999. Book.objects.create(author=author, title="Django 1", pub_date=datetime.datetime.now())
  1000. Book.objects.create(author=author, title="Django 2", pub_date=datetime.datetime.now())
  1001. self.assertForeignKeyExists(Book, 'author_id', 'schema_author')
  1002. @skipUnlessDBFeature('supports_foreign_keys')
  1003. def test_alter_fk_to_o2o(self):
  1004. """
  1005. #24163 - Tests altering of ForeignKey to OneToOneField
  1006. """
  1007. # Create the table
  1008. with connection.schema_editor() as editor:
  1009. editor.create_model(Author)
  1010. editor.create_model(Book)
  1011. # Ensure the field is right to begin with
  1012. columns = self.column_classes(Book)
  1013. self.assertEqual(columns['author_id'][0], connection.features.introspected_field_types['IntegerField'])
  1014. # Ensure the field is not unique
  1015. author = Author.objects.create(name="Joe")
  1016. Book.objects.create(author=author, title="Django 1", pub_date=datetime.datetime.now())
  1017. Book.objects.create(author=author, title="Django 2", pub_date=datetime.datetime.now())
  1018. Book.objects.all().delete()
  1019. self.assertForeignKeyExists(Book, 'author_id', 'schema_author')
  1020. # Alter the ForeignKey to OneToOneField
  1021. old_field = Book._meta.get_field("author")
  1022. new_field = OneToOneField(Author, CASCADE)
  1023. new_field.set_attributes_from_name("author")
  1024. with connection.schema_editor() as editor:
  1025. editor.alter_field(Book, old_field, new_field, strict=True)
  1026. # Ensure the field is right afterwards
  1027. columns = self.column_classes(BookWithO2O)
  1028. self.assertEqual(columns['author_id'][0], connection.features.introspected_field_types['IntegerField'])
  1029. # Ensure the field is unique now
  1030. BookWithO2O.objects.create(author=author, title="Django 1", pub_date=datetime.datetime.now())
  1031. with self.assertRaises(IntegrityError):
  1032. BookWithO2O.objects.create(author=author, title="Django 2", pub_date=datetime.datetime.now())
  1033. self.assertForeignKeyExists(BookWithO2O, 'author_id', 'schema_author')
  1034. def test_alter_field_fk_to_o2o(self):
  1035. with connection.schema_editor() as editor:
  1036. editor.create_model(Author)
  1037. editor.create_model(Book)
  1038. expected_fks = 1 if connection.features.supports_foreign_keys else 0
  1039. # Check the index is right to begin with.
  1040. counts = self.get_constraints_count(
  1041. Book._meta.db_table,
  1042. Book._meta.get_field('author').column,
  1043. (Author._meta.db_table, Author._meta.pk.column),
  1044. )
  1045. self.assertEqual(counts, {'fks': expected_fks, 'uniques': 0, 'indexes': 1})
  1046. old_field = Book._meta.get_field('author')
  1047. new_field = OneToOneField(Author, CASCADE)
  1048. new_field.set_attributes_from_name('author')
  1049. with connection.schema_editor() as editor:
  1050. editor.alter_field(Book, old_field, new_field, strict=True)
  1051. counts = self.get_constraints_count(
  1052. Book._meta.db_table,
  1053. Book._meta.get_field('author').column,
  1054. (Author._meta.db_table, Author._meta.pk.column),
  1055. )
  1056. # The index on ForeignKey is replaced with a unique constraint for OneToOneField.
  1057. self.assertEqual(counts, {'fks': expected_fks, 'uniques': 1, 'indexes': 0})
  1058. def test_alter_field_fk_keeps_index(self):
  1059. with connection.schema_editor() as editor:
  1060. editor.create_model(Author)
  1061. editor.create_model(Book)
  1062. expected_fks = 1 if connection.features.supports_foreign_keys else 0
  1063. # Check the index is right to begin with.
  1064. counts = self.get_constraints_count(
  1065. Book._meta.db_table,
  1066. Book._meta.get_field('author').column,
  1067. (Author._meta.db_table, Author._meta.pk.column),
  1068. )
  1069. self.assertEqual(counts, {'fks': expected_fks, 'uniques': 0, 'indexes': 1})
  1070. old_field = Book._meta.get_field('author')
  1071. # on_delete changed from CASCADE.
  1072. new_field = ForeignKey(Author, PROTECT)
  1073. new_field.set_attributes_from_name('author')
  1074. with connection.schema_editor() as editor:
  1075. editor.alter_field(Book, old_field, new_field, strict=True)
  1076. counts = self.get_constraints_count(
  1077. Book._meta.db_table,
  1078. Book._meta.get_field('author').column,
  1079. (Author._meta.db_table, Author._meta.pk.column),
  1080. )
  1081. # The index remains.
  1082. self.assertEqual(counts, {'fks': expected_fks, 'uniques': 0, 'indexes': 1})
  1083. def test_alter_field_o2o_to_fk(self):
  1084. with connection.schema_editor() as editor:
  1085. editor.create_model(Author)
  1086. editor.create_model(BookWithO2O)
  1087. expected_fks = 1 if connection.features.supports_foreign_keys else 0
  1088. # Check the unique constraint is right to begin with.
  1089. counts = self.get_constraints_count(
  1090. BookWithO2O._meta.db_table,
  1091. BookWithO2O._meta.get_field('author').column,
  1092. (Author._meta.db_table, Author._meta.pk.column),
  1093. )
  1094. self.assertEqual(counts, {'fks': expected_fks, 'uniques': 1, 'indexes': 0})
  1095. old_field = BookWithO2O._meta.get_field('author')
  1096. new_field = ForeignKey(Author, CASCADE)
  1097. new_field.set_attributes_from_name('author')
  1098. with connection.schema_editor() as editor:
  1099. editor.alter_field(BookWithO2O, old_field, new_field, strict=True)
  1100. counts = self.get_constraints_count(
  1101. BookWithO2O._meta.db_table,
  1102. BookWithO2O._meta.get_field('author').column,
  1103. (Author._meta.db_table, Author._meta.pk.column),
  1104. )
  1105. # The unique constraint on OneToOneField is replaced with an index for ForeignKey.
  1106. self.assertEqual(counts, {'fks': expected_fks, 'uniques': 0, 'indexes': 1})
  1107. def test_alter_field_o2o_keeps_unique(self):
  1108. with connection.schema_editor() as editor:
  1109. editor.create_model(Author)
  1110. editor.create_model(BookWithO2O)
  1111. expected_fks = 1 if connection.features.supports_foreign_keys else 0
  1112. # Check the unique constraint is right to begin with.
  1113. counts = self.get_constraints_count(
  1114. BookWithO2O._meta.db_table,
  1115. BookWithO2O._meta.get_field('author').column,
  1116. (Author._meta.db_table, Author._meta.pk.column),
  1117. )
  1118. self.assertEqual(counts, {'fks': expected_fks, 'uniques': 1, 'indexes': 0})
  1119. old_field = BookWithO2O._meta.get_field('author')
  1120. # on_delete changed from CASCADE.
  1121. new_field = OneToOneField(Author, PROTECT)
  1122. new_field.set_attributes_from_name('author')
  1123. with connection.schema_editor() as editor:
  1124. editor.alter_field(BookWithO2O, old_field, new_field, strict=True)
  1125. counts = self.get_constraints_count(
  1126. BookWithO2O._meta.db_table,
  1127. BookWithO2O._meta.get_field('author').column,
  1128. (Author._meta.db_table, Author._meta.pk.column),
  1129. )
  1130. # The unique constraint remains.
  1131. self.assertEqual(counts, {'fks': expected_fks, 'uniques': 1, 'indexes': 0})
  1132. @skipUnlessDBFeature('ignores_table_name_case')
  1133. def test_alter_db_table_case(self):
  1134. # Create the table
  1135. with connection.schema_editor() as editor:
  1136. editor.create_model(Author)
  1137. # Alter the case of the table
  1138. old_table_name = Author._meta.db_table
  1139. with connection.schema_editor() as editor:
  1140. editor.alter_db_table(Author, old_table_name, old_table_name.upper())
  1141. def test_alter_implicit_id_to_explicit(self):
  1142. """
  1143. Should be able to convert an implicit "id" field to an explicit "id"
  1144. primary key field.
  1145. """
  1146. with connection.schema_editor() as editor:
  1147. editor.create_model(Author)
  1148. old_field = Author._meta.get_field("id")
  1149. new_field = AutoField(primary_key=True)
  1150. new_field.set_attributes_from_name("id")
  1151. new_field.model = Author
  1152. with connection.schema_editor() as editor:
  1153. editor.alter_field(Author, old_field, new_field, strict=True)
  1154. # This will fail if DROP DEFAULT is inadvertently executed on this
  1155. # field which drops the id sequence, at least on PostgreSQL.
  1156. Author.objects.create(name='Foo')
  1157. Author.objects.create(name='Bar')
  1158. def test_alter_autofield_pk_to_bigautofield_pk_sequence_owner(self):
  1159. """
  1160. Converting an implicit PK to BigAutoField(primary_key=True) should keep
  1161. a sequence owner on PostgreSQL.
  1162. """
  1163. with connection.schema_editor() as editor:
  1164. editor.create_model(Author)
  1165. old_field = Author._meta.get_field('id')
  1166. new_field = BigAutoField(primary_key=True)
  1167. new_field.set_attributes_from_name('id')
  1168. new_field.model = Author
  1169. with connection.schema_editor() as editor:
  1170. editor.alter_field(Author, old_field, new_field, strict=True)
  1171. Author.objects.create(name='Foo', pk=1)
  1172. with connection.cursor() as cursor:
  1173. sequence_reset_sqls = connection.ops.sequence_reset_sql(no_style(), [Author])
  1174. if sequence_reset_sqls:
  1175. cursor.execute(sequence_reset_sqls[0])
  1176. # Fail on PostgreSQL if sequence is missing an owner.
  1177. self.assertIsNotNone(Author.objects.create(name='Bar'))
  1178. def test_alter_autofield_pk_to_smallautofield_pk_sequence_owner(self):
  1179. """
  1180. Converting an implicit PK to SmallAutoField(primary_key=True) should
  1181. keep a sequence owner on PostgreSQL.
  1182. """
  1183. with connection.schema_editor() as editor:
  1184. editor.create_model(Author)
  1185. old_field = Author._meta.get_field('id')
  1186. new_field = SmallAutoField(primary_key=True)
  1187. new_field.set_attributes_from_name('id')
  1188. new_field.model = Author
  1189. with connection.schema_editor() as editor:
  1190. editor.alter_field(Author, old_field, new_field, strict=True)
  1191. Author.objects.create(name='Foo', pk=1)
  1192. with connection.cursor() as cursor:
  1193. sequence_reset_sqls = connection.ops.sequence_reset_sql(no_style(), [Author])
  1194. if sequence_reset_sqls:
  1195. cursor.execute(sequence_reset_sqls[0])
  1196. # Fail on PostgreSQL if sequence is missing an owner.
  1197. self.assertIsNotNone(Author.objects.create(name='Bar'))
  1198. def test_alter_int_pk_to_autofield_pk(self):
  1199. """
  1200. Should be able to rename an IntegerField(primary_key=True) to
  1201. AutoField(primary_key=True).
  1202. """
  1203. with connection.schema_editor() as editor:
  1204. editor.create_model(IntegerPK)
  1205. old_field = IntegerPK._meta.get_field('i')
  1206. new_field = AutoField(primary_key=True)
  1207. new_field.model = IntegerPK
  1208. new_field.set_attributes_from_name('i')
  1209. with connection.schema_editor() as editor:
  1210. editor.alter_field(IntegerPK, old_field, new_field, strict=True)
  1211. def test_alter_int_pk_to_bigautofield_pk(self):
  1212. """
  1213. Should be able to rename an IntegerField(primary_key=True) to
  1214. BigAutoField(primary_key=True).
  1215. """
  1216. with connection.schema_editor() as editor:
  1217. editor.create_model(IntegerPK)
  1218. old_field = IntegerPK._meta.get_field('i')
  1219. new_field = BigAutoField(primary_key=True)
  1220. new_field.model = IntegerPK
  1221. new_field.set_attributes_from_name('i')
  1222. with connection.schema_editor() as editor:
  1223. editor.alter_field(IntegerPK, old_field, new_field, strict=True)
  1224. @isolate_apps('schema')
  1225. def test_alter_smallint_pk_to_smallautofield_pk(self):
  1226. """
  1227. Should be able to rename an SmallIntegerField(primary_key=True) to
  1228. SmallAutoField(primary_key=True).
  1229. """
  1230. class SmallIntegerPK(Model):
  1231. i = SmallIntegerField(primary_key=True)
  1232. class Meta:
  1233. app_label = 'schema'
  1234. with connection.schema_editor() as editor:
  1235. editor.create_model(SmallIntegerPK)
  1236. self.isolated_local_models = [SmallIntegerPK]
  1237. old_field = SmallIntegerPK._meta.get_field('i')
  1238. new_field = SmallAutoField(primary_key=True)
  1239. new_field.model = SmallIntegerPK
  1240. new_field.set_attributes_from_name('i')
  1241. with connection.schema_editor() as editor:
  1242. editor.alter_field(SmallIntegerPK, old_field, new_field, strict=True)
  1243. def test_alter_int_pk_to_int_unique(self):
  1244. """
  1245. Should be able to rename an IntegerField(primary_key=True) to
  1246. IntegerField(unique=True).
  1247. """
  1248. with connection.schema_editor() as editor:
  1249. editor.create_model(IntegerPK)
  1250. # Delete the old PK
  1251. old_field = IntegerPK._meta.get_field('i')
  1252. new_field = IntegerField(unique=True)
  1253. new_field.model = IntegerPK
  1254. new_field.set_attributes_from_name('i')
  1255. with connection.schema_editor() as editor:
  1256. editor.alter_field(IntegerPK, old_field, new_field, strict=True)
  1257. # The primary key constraint is gone. Result depends on database:
  1258. # 'id' for SQLite, None for others (must not be 'i').
  1259. self.assertIn(self.get_primary_key(IntegerPK._meta.db_table), ('id', None))
  1260. # Set up a model class as it currently stands. The original IntegerPK
  1261. # class is now out of date and some backends make use of the whole
  1262. # model class when modifying a field (such as sqlite3 when remaking a
  1263. # table) so an outdated model class leads to incorrect results.
  1264. class Transitional(Model):
  1265. i = IntegerField(unique=True)
  1266. j = IntegerField(unique=True)
  1267. class Meta:
  1268. app_label = 'schema'
  1269. apps = new_apps
  1270. db_table = 'INTEGERPK'
  1271. # model requires a new PK
  1272. old_field = Transitional._meta.get_field('j')
  1273. new_field = IntegerField(primary_key=True)
  1274. new_field.model = Transitional
  1275. new_field.set_attributes_from_name('j')
  1276. with connection.schema_editor() as editor:
  1277. editor.alter_field(Transitional, old_field, new_field, strict=True)
  1278. # Create a model class representing the updated model.
  1279. class IntegerUnique(Model):
  1280. i = IntegerField(unique=True)
  1281. j = IntegerField(primary_key=True)
  1282. class Meta:
  1283. app_label = 'schema'
  1284. apps = new_apps
  1285. db_table = 'INTEGERPK'
  1286. # Ensure unique constraint works.
  1287. IntegerUnique.objects.create(i=1, j=1)
  1288. with self.assertRaises(IntegrityError):
  1289. IntegerUnique.objects.create(i=1, j=2)
  1290. def test_rename(self):
  1291. """
  1292. Tests simple altering of fields
  1293. """
  1294. # Create the table
  1295. with connection.schema_editor() as editor:
  1296. editor.create_model(Author)
  1297. # Ensure the field is right to begin with
  1298. columns = self.column_classes(Author)
  1299. self.assertEqual(columns['name'][0], connection.features.introspected_field_types['CharField'])
  1300. self.assertNotIn("display_name", columns)
  1301. # Alter the name field's name
  1302. old_field = Author._meta.get_field("name")
  1303. new_field = CharField(max_length=254)
  1304. new_field.set_attributes_from_name("display_name")
  1305. with connection.schema_editor() as editor:
  1306. editor.alter_field(Author, old_field, new_field, strict=True)
  1307. # Ensure the field is right afterwards
  1308. columns = self.column_classes(Author)
  1309. self.assertEqual(columns['display_name'][0], connection.features.introspected_field_types['CharField'])
  1310. self.assertNotIn("name", columns)
  1311. @isolate_apps('schema')
  1312. def test_rename_referenced_field(self):
  1313. class Author(Model):
  1314. name = CharField(max_length=255, unique=True)
  1315. class Meta:
  1316. app_label = 'schema'
  1317. class Book(Model):
  1318. author = ForeignKey(Author, CASCADE, to_field='name')
  1319. class Meta:
  1320. app_label = 'schema'
  1321. with connection.schema_editor() as editor:
  1322. editor.create_model(Author)
  1323. editor.create_model(Book)
  1324. new_field = CharField(max_length=255, unique=True)
  1325. new_field.set_attributes_from_name('renamed')
  1326. with connection.schema_editor(atomic=connection.features.supports_atomic_references_rename) as editor:
  1327. editor.alter_field(Author, Author._meta.get_field('name'), new_field)
  1328. # Ensure the foreign key reference was updated.
  1329. self.assertForeignKeyExists(Book, 'author_id', 'schema_author', 'renamed')
  1330. @skipIfDBFeature('interprets_empty_strings_as_nulls')
  1331. def test_rename_keep_null_status(self):
  1332. """
  1333. Renaming a field shouldn't affect the not null status.
  1334. """
  1335. with connection.schema_editor() as editor:
  1336. editor.create_model(Note)
  1337. with self.assertRaises(IntegrityError):
  1338. Note.objects.create(info=None)
  1339. old_field = Note._meta.get_field("info")
  1340. new_field = TextField()
  1341. new_field.set_attributes_from_name("detail_info")
  1342. with connection.schema_editor() as editor:
  1343. editor.alter_field(Note, old_field, new_field, strict=True)
  1344. columns = self.column_classes(Note)
  1345. self.assertEqual(columns['detail_info'][0], "TextField")
  1346. self.assertNotIn("info", columns)
  1347. with self.assertRaises(IntegrityError):
  1348. NoteRename.objects.create(detail_info=None)
  1349. def _test_m2m_create(self, M2MFieldClass):
  1350. """
  1351. Tests M2M fields on models during creation
  1352. """
  1353. class LocalBookWithM2M(Model):
  1354. author = ForeignKey(Author, CASCADE)
  1355. title = CharField(max_length=100, db_index=True)
  1356. pub_date = DateTimeField()
  1357. tags = M2MFieldClass("TagM2MTest", related_name="books")
  1358. class Meta:
  1359. app_label = 'schema'
  1360. apps = new_apps
  1361. self.local_models = [LocalBookWithM2M]
  1362. # Create the tables
  1363. with connection.schema_editor() as editor:
  1364. editor.create_model(Author)
  1365. editor.create_model(TagM2MTest)
  1366. editor.create_model(LocalBookWithM2M)
  1367. # Ensure there is now an m2m table there
  1368. columns = self.column_classes(LocalBookWithM2M._meta.get_field("tags").remote_field.through)
  1369. self.assertEqual(columns['tagm2mtest_id'][0], connection.features.introspected_field_types['IntegerField'])
  1370. def test_m2m_create(self):
  1371. self._test_m2m_create(ManyToManyField)
  1372. def test_m2m_create_custom(self):
  1373. self._test_m2m_create(CustomManyToManyField)
  1374. def test_m2m_create_inherited(self):
  1375. self._test_m2m_create(InheritedManyToManyField)
  1376. def _test_m2m_create_through(self, M2MFieldClass):
  1377. """
  1378. Tests M2M fields on models during creation with through models
  1379. """
  1380. class LocalTagThrough(Model):
  1381. book = ForeignKey("schema.LocalBookWithM2MThrough", CASCADE)
  1382. tag = ForeignKey("schema.TagM2MTest", CASCADE)
  1383. class Meta:
  1384. app_label = 'schema'
  1385. apps = new_apps
  1386. class LocalBookWithM2MThrough(Model):
  1387. tags = M2MFieldClass("TagM2MTest", related_name="books", through=LocalTagThrough)
  1388. class Meta:
  1389. app_label = 'schema'
  1390. apps = new_apps
  1391. self.local_models = [LocalTagThrough, LocalBookWithM2MThrough]
  1392. # Create the tables
  1393. with connection.schema_editor() as editor:
  1394. editor.create_model(LocalTagThrough)
  1395. editor.create_model(TagM2MTest)
  1396. editor.create_model(LocalBookWithM2MThrough)
  1397. # Ensure there is now an m2m table there
  1398. columns = self.column_classes(LocalTagThrough)
  1399. self.assertEqual(columns['book_id'][0], connection.features.introspected_field_types['IntegerField'])
  1400. self.assertEqual(columns['tag_id'][0], connection.features.introspected_field_types['IntegerField'])
  1401. def test_m2m_create_through(self):
  1402. self._test_m2m_create_through(ManyToManyField)
  1403. def test_m2m_create_through_custom(self):
  1404. self._test_m2m_create_through(CustomManyToManyField)
  1405. def test_m2m_create_through_inherited(self):
  1406. self._test_m2m_create_through(InheritedManyToManyField)
  1407. def _test_m2m(self, M2MFieldClass):
  1408. """
  1409. Tests adding/removing M2M fields on models
  1410. """
  1411. class LocalAuthorWithM2M(Model):
  1412. name = CharField(max_length=255)
  1413. class Meta:
  1414. app_label = 'schema'
  1415. apps = new_apps
  1416. self.local_models = [LocalAuthorWithM2M]
  1417. # Create the tables
  1418. with connection.schema_editor() as editor:
  1419. editor.create_model(LocalAuthorWithM2M)
  1420. editor.create_model(TagM2MTest)
  1421. # Create an M2M field
  1422. new_field = M2MFieldClass("schema.TagM2MTest", related_name="authors")
  1423. new_field.contribute_to_class(LocalAuthorWithM2M, "tags")
  1424. # Ensure there's no m2m table there
  1425. with self.assertRaises(DatabaseError):
  1426. self.column_classes(new_field.remote_field.through)
  1427. # Add the field
  1428. with connection.schema_editor() as editor:
  1429. editor.add_field(LocalAuthorWithM2M, new_field)
  1430. # Ensure there is now an m2m table there
  1431. columns = self.column_classes(new_field.remote_field.through)
  1432. self.assertEqual(columns['tagm2mtest_id'][0], connection.features.introspected_field_types['IntegerField'])
  1433. # "Alter" the field. This should not rename the DB table to itself.
  1434. with connection.schema_editor() as editor:
  1435. editor.alter_field(LocalAuthorWithM2M, new_field, new_field, strict=True)
  1436. # Remove the M2M table again
  1437. with connection.schema_editor() as editor:
  1438. editor.remove_field(LocalAuthorWithM2M, new_field)
  1439. # Ensure there's no m2m table there
  1440. with self.assertRaises(DatabaseError):
  1441. self.column_classes(new_field.remote_field.through)
  1442. # Make sure the model state is coherent with the table one now that
  1443. # we've removed the tags field.
  1444. opts = LocalAuthorWithM2M._meta
  1445. opts.local_many_to_many.remove(new_field)
  1446. del new_apps.all_models['schema'][new_field.remote_field.through._meta.model_name]
  1447. opts._expire_cache()
  1448. def test_m2m(self):
  1449. self._test_m2m(ManyToManyField)
  1450. def test_m2m_custom(self):
  1451. self._test_m2m(CustomManyToManyField)
  1452. def test_m2m_inherited(self):
  1453. self._test_m2m(InheritedManyToManyField)
  1454. def _test_m2m_through_alter(self, M2MFieldClass):
  1455. """
  1456. Tests altering M2Ms with explicit through models (should no-op)
  1457. """
  1458. class LocalAuthorTag(Model):
  1459. author = ForeignKey("schema.LocalAuthorWithM2MThrough", CASCADE)
  1460. tag = ForeignKey("schema.TagM2MTest", CASCADE)
  1461. class Meta:
  1462. app_label = 'schema'
  1463. apps = new_apps
  1464. class LocalAuthorWithM2MThrough(Model):
  1465. name = CharField(max_length=255)
  1466. tags = M2MFieldClass("schema.TagM2MTest", related_name="authors", through=LocalAuthorTag)
  1467. class Meta:
  1468. app_label = 'schema'
  1469. apps = new_apps
  1470. self.local_models = [LocalAuthorTag, LocalAuthorWithM2MThrough]
  1471. # Create the tables
  1472. with connection.schema_editor() as editor:
  1473. editor.create_model(LocalAuthorTag)
  1474. editor.create_model(LocalAuthorWithM2MThrough)
  1475. editor.create_model(TagM2MTest)
  1476. # Ensure the m2m table is there
  1477. self.assertEqual(len(self.column_classes(LocalAuthorTag)), 3)
  1478. # "Alter" the field's blankness. This should not actually do anything.
  1479. old_field = LocalAuthorWithM2MThrough._meta.get_field("tags")
  1480. new_field = M2MFieldClass("schema.TagM2MTest", related_name="authors", through=LocalAuthorTag)
  1481. new_field.contribute_to_class(LocalAuthorWithM2MThrough, "tags")
  1482. with connection.schema_editor() as editor:
  1483. editor.alter_field(LocalAuthorWithM2MThrough, old_field, new_field, strict=True)
  1484. # Ensure the m2m table is still there
  1485. self.assertEqual(len(self.column_classes(LocalAuthorTag)), 3)
  1486. def test_m2m_through_alter(self):
  1487. self._test_m2m_through_alter(ManyToManyField)
  1488. def test_m2m_through_alter_custom(self):
  1489. self._test_m2m_through_alter(CustomManyToManyField)
  1490. def test_m2m_through_alter_inherited(self):
  1491. self._test_m2m_through_alter(InheritedManyToManyField)
  1492. def _test_m2m_repoint(self, M2MFieldClass):
  1493. """
  1494. Tests repointing M2M fields
  1495. """
  1496. class LocalBookWithM2M(Model):
  1497. author = ForeignKey(Author, CASCADE)
  1498. title = CharField(max_length=100, db_index=True)
  1499. pub_date = DateTimeField()
  1500. tags = M2MFieldClass("TagM2MTest", related_name="books")
  1501. class Meta:
  1502. app_label = 'schema'
  1503. apps = new_apps
  1504. self.local_models = [LocalBookWithM2M]
  1505. # Create the tables
  1506. with connection.schema_editor() as editor:
  1507. editor.create_model(Author)
  1508. editor.create_model(LocalBookWithM2M)
  1509. editor.create_model(TagM2MTest)
  1510. editor.create_model(UniqueTest)
  1511. # Ensure the M2M exists and points to TagM2MTest
  1512. if connection.features.supports_foreign_keys:
  1513. self.assertForeignKeyExists(
  1514. LocalBookWithM2M._meta.get_field("tags").remote_field.through,
  1515. 'tagm2mtest_id',
  1516. 'schema_tagm2mtest',
  1517. )
  1518. # Repoint the M2M
  1519. old_field = LocalBookWithM2M._meta.get_field("tags")
  1520. new_field = M2MFieldClass(UniqueTest)
  1521. new_field.contribute_to_class(LocalBookWithM2M, "uniques")
  1522. with connection.schema_editor() as editor:
  1523. editor.alter_field(LocalBookWithM2M, old_field, new_field, strict=True)
  1524. # Ensure old M2M is gone
  1525. with self.assertRaises(DatabaseError):
  1526. self.column_classes(LocalBookWithM2M._meta.get_field("tags").remote_field.through)
  1527. # This model looks like the new model and is used for teardown.
  1528. opts = LocalBookWithM2M._meta
  1529. opts.local_many_to_many.remove(old_field)
  1530. # Ensure the new M2M exists and points to UniqueTest
  1531. if connection.features.supports_foreign_keys:
  1532. self.assertForeignKeyExists(new_field.remote_field.through, 'uniquetest_id', 'schema_uniquetest')
  1533. def test_m2m_repoint(self):
  1534. self._test_m2m_repoint(ManyToManyField)
  1535. def test_m2m_repoint_custom(self):
  1536. self._test_m2m_repoint(CustomManyToManyField)
  1537. def test_m2m_repoint_inherited(self):
  1538. self._test_m2m_repoint(InheritedManyToManyField)
  1539. @isolate_apps('schema')
  1540. def test_m2m_rename_field_in_target_model(self):
  1541. class LocalTagM2MTest(Model):
  1542. title = CharField(max_length=255)
  1543. class Meta:
  1544. app_label = 'schema'
  1545. class LocalM2M(Model):
  1546. tags = ManyToManyField(LocalTagM2MTest)
  1547. class Meta:
  1548. app_label = 'schema'
  1549. # Create the tables.
  1550. with connection.schema_editor() as editor:
  1551. editor.create_model(LocalM2M)
  1552. editor.create_model(LocalTagM2MTest)
  1553. self.isolated_local_models = [LocalM2M, LocalTagM2MTest]
  1554. # Ensure the m2m table is there.
  1555. self.assertEqual(len(self.column_classes(LocalM2M)), 1)
  1556. # Alter a field in LocalTagM2MTest.
  1557. old_field = LocalTagM2MTest._meta.get_field('title')
  1558. new_field = CharField(max_length=254)
  1559. new_field.contribute_to_class(LocalTagM2MTest, 'title1')
  1560. # @isolate_apps() and inner models are needed to have the model
  1561. # relations populated, otherwise this doesn't act as a regression test.
  1562. self.assertEqual(len(new_field.model._meta.related_objects), 1)
  1563. with connection.schema_editor() as editor:
  1564. editor.alter_field(LocalTagM2MTest, old_field, new_field, strict=True)
  1565. # Ensure the m2m table is still there.
  1566. self.assertEqual(len(self.column_classes(LocalM2M)), 1)
  1567. @skipUnlessDBFeature('supports_column_check_constraints', 'can_introspect_check_constraints')
  1568. def test_check_constraints(self):
  1569. """
  1570. Tests creating/deleting CHECK constraints
  1571. """
  1572. # Create the tables
  1573. with connection.schema_editor() as editor:
  1574. editor.create_model(Author)
  1575. # Ensure the constraint exists
  1576. constraints = self.get_constraints(Author._meta.db_table)
  1577. if not any(details['columns'] == ['height'] and details['check'] for details in constraints.values()):
  1578. self.fail("No check constraint for height found")
  1579. # Alter the column to remove it
  1580. old_field = Author._meta.get_field("height")
  1581. new_field = IntegerField(null=True, blank=True)
  1582. new_field.set_attributes_from_name("height")
  1583. with connection.schema_editor() as editor:
  1584. editor.alter_field(Author, old_field, new_field, strict=True)
  1585. constraints = self.get_constraints(Author._meta.db_table)
  1586. for details in constraints.values():
  1587. if details['columns'] == ["height"] and details['check']:
  1588. self.fail("Check constraint for height found")
  1589. # Alter the column to re-add it
  1590. new_field2 = Author._meta.get_field("height")
  1591. with connection.schema_editor() as editor:
  1592. editor.alter_field(Author, new_field, new_field2, strict=True)
  1593. constraints = self.get_constraints(Author._meta.db_table)
  1594. if not any(details['columns'] == ['height'] and details['check'] for details in constraints.values()):
  1595. self.fail("No check constraint for height found")
  1596. @skipUnlessDBFeature('supports_column_check_constraints', 'can_introspect_check_constraints')
  1597. def test_remove_field_check_does_not_remove_meta_constraints(self):
  1598. with connection.schema_editor() as editor:
  1599. editor.create_model(Author)
  1600. # Add the custom check constraint
  1601. constraint = CheckConstraint(check=Q(height__gte=0), name='author_height_gte_0_check')
  1602. custom_constraint_name = constraint.name
  1603. Author._meta.constraints = [constraint]
  1604. with connection.schema_editor() as editor:
  1605. editor.add_constraint(Author, constraint)
  1606. # Ensure the constraints exist
  1607. constraints = self.get_constraints(Author._meta.db_table)
  1608. self.assertIn(custom_constraint_name, constraints)
  1609. other_constraints = [
  1610. name for name, details in constraints.items()
  1611. if details['columns'] == ['height'] and details['check'] and name != custom_constraint_name
  1612. ]
  1613. self.assertEqual(len(other_constraints), 1)
  1614. # Alter the column to remove field check
  1615. old_field = Author._meta.get_field('height')
  1616. new_field = IntegerField(null=True, blank=True)
  1617. new_field.set_attributes_from_name('height')
  1618. with connection.schema_editor() as editor:
  1619. editor.alter_field(Author, old_field, new_field, strict=True)
  1620. constraints = self.get_constraints(Author._meta.db_table)
  1621. self.assertIn(custom_constraint_name, constraints)
  1622. other_constraints = [
  1623. name for name, details in constraints.items()
  1624. if details['columns'] == ['height'] and details['check'] and name != custom_constraint_name
  1625. ]
  1626. self.assertEqual(len(other_constraints), 0)
  1627. # Alter the column to re-add field check
  1628. new_field2 = Author._meta.get_field('height')
  1629. with connection.schema_editor() as editor:
  1630. editor.alter_field(Author, new_field, new_field2, strict=True)
  1631. constraints = self.get_constraints(Author._meta.db_table)
  1632. self.assertIn(custom_constraint_name, constraints)
  1633. other_constraints = [
  1634. name for name, details in constraints.items()
  1635. if details['columns'] == ['height'] and details['check'] and name != custom_constraint_name
  1636. ]
  1637. self.assertEqual(len(other_constraints), 1)
  1638. # Drop the check constraint
  1639. with connection.schema_editor() as editor:
  1640. Author._meta.constraints = []
  1641. editor.remove_constraint(Author, constraint)
  1642. def test_unique(self):
  1643. """
  1644. Tests removing and adding unique constraints to a single column.
  1645. """
  1646. # Create the table
  1647. with connection.schema_editor() as editor:
  1648. editor.create_model(Tag)
  1649. # Ensure the field is unique to begin with
  1650. Tag.objects.create(title="foo", slug="foo")
  1651. with self.assertRaises(IntegrityError):
  1652. Tag.objects.create(title="bar", slug="foo")
  1653. Tag.objects.all().delete()
  1654. # Alter the slug field to be non-unique
  1655. old_field = Tag._meta.get_field("slug")
  1656. new_field = SlugField(unique=False)
  1657. new_field.set_attributes_from_name("slug")
  1658. with connection.schema_editor() as editor:
  1659. editor.alter_field(Tag, old_field, new_field, strict=True)
  1660. # Ensure the field is no longer unique
  1661. Tag.objects.create(title="foo", slug="foo")
  1662. Tag.objects.create(title="bar", slug="foo")
  1663. Tag.objects.all().delete()
  1664. # Alter the slug field to be unique
  1665. new_field2 = SlugField(unique=True)
  1666. new_field2.set_attributes_from_name("slug")
  1667. with connection.schema_editor() as editor:
  1668. editor.alter_field(Tag, new_field, new_field2, strict=True)
  1669. # Ensure the field is unique again
  1670. Tag.objects.create(title="foo", slug="foo")
  1671. with self.assertRaises(IntegrityError):
  1672. Tag.objects.create(title="bar", slug="foo")
  1673. Tag.objects.all().delete()
  1674. # Rename the field
  1675. new_field3 = SlugField(unique=True)
  1676. new_field3.set_attributes_from_name("slug2")
  1677. with connection.schema_editor() as editor:
  1678. editor.alter_field(Tag, new_field2, new_field3, strict=True)
  1679. # Ensure the field is still unique
  1680. TagUniqueRename.objects.create(title="foo", slug2="foo")
  1681. with self.assertRaises(IntegrityError):
  1682. TagUniqueRename.objects.create(title="bar", slug2="foo")
  1683. Tag.objects.all().delete()
  1684. def test_unique_name_quoting(self):
  1685. old_table_name = TagUniqueRename._meta.db_table
  1686. try:
  1687. with connection.schema_editor() as editor:
  1688. editor.create_model(TagUniqueRename)
  1689. editor.alter_db_table(TagUniqueRename, old_table_name, 'unique-table')
  1690. TagUniqueRename._meta.db_table = 'unique-table'
  1691. # This fails if the unique index name isn't quoted.
  1692. editor.alter_unique_together(TagUniqueRename, [], (('title', 'slug2'),))
  1693. finally:
  1694. TagUniqueRename._meta.db_table = old_table_name
  1695. @isolate_apps('schema')
  1696. @unittest.skipIf(connection.vendor == 'sqlite', 'SQLite naively remakes the table on field alteration.')
  1697. @skipUnlessDBFeature('supports_foreign_keys')
  1698. def test_unique_no_unnecessary_fk_drops(self):
  1699. """
  1700. If AlterField isn't selective about dropping foreign key constraints
  1701. when modifying a field with a unique constraint, the AlterField
  1702. incorrectly drops and recreates the Book.author foreign key even though
  1703. it doesn't restrict the field being changed (#29193).
  1704. """
  1705. class Author(Model):
  1706. name = CharField(max_length=254, unique=True)
  1707. class Meta:
  1708. app_label = 'schema'
  1709. class Book(Model):
  1710. author = ForeignKey(Author, CASCADE)
  1711. class Meta:
  1712. app_label = 'schema'
  1713. with connection.schema_editor() as editor:
  1714. editor.create_model(Author)
  1715. editor.create_model(Book)
  1716. new_field = CharField(max_length=255, unique=True)
  1717. new_field.model = Author
  1718. new_field.set_attributes_from_name('name')
  1719. with self.assertLogs('django.db.backends.schema', 'DEBUG') as cm:
  1720. with connection.schema_editor() as editor:
  1721. editor.alter_field(Author, Author._meta.get_field('name'), new_field)
  1722. # One SQL statement is executed to alter the field.
  1723. self.assertEqual(len(cm.records), 1)
  1724. @isolate_apps('schema')
  1725. @unittest.skipIf(connection.vendor == 'sqlite', 'SQLite remakes the table on field alteration.')
  1726. def test_unique_and_reverse_m2m(self):
  1727. """
  1728. AlterField can modify a unique field when there's a reverse M2M
  1729. relation on the model.
  1730. """
  1731. class Tag(Model):
  1732. title = CharField(max_length=255)
  1733. slug = SlugField(unique=True)
  1734. class Meta:
  1735. app_label = 'schema'
  1736. class Book(Model):
  1737. tags = ManyToManyField(Tag, related_name='books')
  1738. class Meta:
  1739. app_label = 'schema'
  1740. self.isolated_local_models = [Book._meta.get_field('tags').remote_field.through]
  1741. with connection.schema_editor() as editor:
  1742. editor.create_model(Tag)
  1743. editor.create_model(Book)
  1744. new_field = SlugField(max_length=75, unique=True)
  1745. new_field.model = Tag
  1746. new_field.set_attributes_from_name('slug')
  1747. with self.assertLogs('django.db.backends.schema', 'DEBUG') as cm:
  1748. with connection.schema_editor() as editor:
  1749. editor.alter_field(Tag, Tag._meta.get_field('slug'), new_field)
  1750. # One SQL statement is executed to alter the field.
  1751. self.assertEqual(len(cm.records), 1)
  1752. # Ensure that the field is still unique.
  1753. Tag.objects.create(title='foo', slug='foo')
  1754. with self.assertRaises(IntegrityError):
  1755. Tag.objects.create(title='bar', slug='foo')
  1756. @skipUnlessDBFeature('allows_multiple_constraints_on_same_fields')
  1757. def test_remove_field_unique_does_not_remove_meta_constraints(self):
  1758. with connection.schema_editor() as editor:
  1759. editor.create_model(AuthorWithUniqueName)
  1760. # Add the custom unique constraint
  1761. constraint = UniqueConstraint(fields=['name'], name='author_name_uniq')
  1762. custom_constraint_name = constraint.name
  1763. AuthorWithUniqueName._meta.constraints = [constraint]
  1764. with connection.schema_editor() as editor:
  1765. editor.add_constraint(AuthorWithUniqueName, constraint)
  1766. # Ensure the constraints exist
  1767. constraints = self.get_constraints(AuthorWithUniqueName._meta.db_table)
  1768. self.assertIn(custom_constraint_name, constraints)
  1769. other_constraints = [
  1770. name for name, details in constraints.items()
  1771. if details['columns'] == ['name'] and details['unique'] and name != custom_constraint_name
  1772. ]
  1773. self.assertEqual(len(other_constraints), 1)
  1774. # Alter the column to remove field uniqueness
  1775. old_field = AuthorWithUniqueName._meta.get_field('name')
  1776. new_field = CharField(max_length=255)
  1777. new_field.set_attributes_from_name('name')
  1778. with connection.schema_editor() as editor:
  1779. editor.alter_field(AuthorWithUniqueName, old_field, new_field, strict=True)
  1780. constraints = self.get_constraints(AuthorWithUniqueName._meta.db_table)
  1781. self.assertIn(custom_constraint_name, constraints)
  1782. other_constraints = [
  1783. name for name, details in constraints.items()
  1784. if details['columns'] == ['name'] and details['unique'] and name != custom_constraint_name
  1785. ]
  1786. self.assertEqual(len(other_constraints), 0)
  1787. # Alter the column to re-add field uniqueness
  1788. new_field2 = AuthorWithUniqueName._meta.get_field('name')
  1789. with connection.schema_editor() as editor:
  1790. editor.alter_field(AuthorWithUniqueName, new_field, new_field2, strict=True)
  1791. constraints = self.get_constraints(AuthorWithUniqueName._meta.db_table)
  1792. self.assertIn(custom_constraint_name, constraints)
  1793. other_constraints = [
  1794. name for name, details in constraints.items()
  1795. if details['columns'] == ['name'] and details['unique'] and name != custom_constraint_name
  1796. ]
  1797. self.assertEqual(len(other_constraints), 1)
  1798. # Drop the unique constraint
  1799. with connection.schema_editor() as editor:
  1800. AuthorWithUniqueName._meta.constraints = []
  1801. editor.remove_constraint(AuthorWithUniqueName, constraint)
  1802. def test_unique_together(self):
  1803. """
  1804. Tests removing and adding unique_together constraints on a model.
  1805. """
  1806. # Create the table
  1807. with connection.schema_editor() as editor:
  1808. editor.create_model(UniqueTest)
  1809. # Ensure the fields are unique to begin with
  1810. UniqueTest.objects.create(year=2012, slug="foo")
  1811. UniqueTest.objects.create(year=2011, slug="foo")
  1812. UniqueTest.objects.create(year=2011, slug="bar")
  1813. with self.assertRaises(IntegrityError):
  1814. UniqueTest.objects.create(year=2012, slug="foo")
  1815. UniqueTest.objects.all().delete()
  1816. # Alter the model to its non-unique-together companion
  1817. with connection.schema_editor() as editor:
  1818. editor.alter_unique_together(UniqueTest, UniqueTest._meta.unique_together, [])
  1819. # Ensure the fields are no longer unique
  1820. UniqueTest.objects.create(year=2012, slug="foo")
  1821. UniqueTest.objects.create(year=2012, slug="foo")
  1822. UniqueTest.objects.all().delete()
  1823. # Alter it back
  1824. new_field2 = SlugField(unique=True)
  1825. new_field2.set_attributes_from_name("slug")
  1826. with connection.schema_editor() as editor:
  1827. editor.alter_unique_together(UniqueTest, [], UniqueTest._meta.unique_together)
  1828. # Ensure the fields are unique again
  1829. UniqueTest.objects.create(year=2012, slug="foo")
  1830. with self.assertRaises(IntegrityError):
  1831. UniqueTest.objects.create(year=2012, slug="foo")
  1832. UniqueTest.objects.all().delete()
  1833. def test_unique_together_with_fk(self):
  1834. """
  1835. Tests removing and adding unique_together constraints that include
  1836. a foreign key.
  1837. """
  1838. # Create the table
  1839. with connection.schema_editor() as editor:
  1840. editor.create_model(Author)
  1841. editor.create_model(Book)
  1842. # Ensure the fields are unique to begin with
  1843. self.assertEqual(Book._meta.unique_together, ())
  1844. # Add the unique_together constraint
  1845. with connection.schema_editor() as editor:
  1846. editor.alter_unique_together(Book, [], [['author', 'title']])
  1847. # Alter it back
  1848. with connection.schema_editor() as editor:
  1849. editor.alter_unique_together(Book, [['author', 'title']], [])
  1850. def test_unique_together_with_fk_with_existing_index(self):
  1851. """
  1852. Tests removing and adding unique_together constraints that include
  1853. a foreign key, where the foreign key is added after the model is
  1854. created.
  1855. """
  1856. # Create the tables
  1857. with connection.schema_editor() as editor:
  1858. editor.create_model(Author)
  1859. editor.create_model(BookWithoutAuthor)
  1860. new_field = ForeignKey(Author, CASCADE)
  1861. new_field.set_attributes_from_name('author')
  1862. editor.add_field(BookWithoutAuthor, new_field)
  1863. # Ensure the fields aren't unique to begin with
  1864. self.assertEqual(Book._meta.unique_together, ())
  1865. # Add the unique_together constraint
  1866. with connection.schema_editor() as editor:
  1867. editor.alter_unique_together(Book, [], [['author', 'title']])
  1868. # Alter it back
  1869. with connection.schema_editor() as editor:
  1870. editor.alter_unique_together(Book, [['author', 'title']], [])
  1871. @skipUnlessDBFeature('allows_multiple_constraints_on_same_fields')
  1872. def test_remove_unique_together_does_not_remove_meta_constraints(self):
  1873. with connection.schema_editor() as editor:
  1874. editor.create_model(AuthorWithUniqueNameAndBirthday)
  1875. # Add the custom unique constraint
  1876. constraint = UniqueConstraint(fields=['name', 'birthday'], name='author_name_birthday_uniq')
  1877. custom_constraint_name = constraint.name
  1878. AuthorWithUniqueNameAndBirthday._meta.constraints = [constraint]
  1879. with connection.schema_editor() as editor:
  1880. editor.add_constraint(AuthorWithUniqueNameAndBirthday, constraint)
  1881. # Ensure the constraints exist
  1882. constraints = self.get_constraints(AuthorWithUniqueNameAndBirthday._meta.db_table)
  1883. self.assertIn(custom_constraint_name, constraints)
  1884. other_constraints = [
  1885. name for name, details in constraints.items()
  1886. if details['columns'] == ['name', 'birthday'] and details['unique'] and name != custom_constraint_name
  1887. ]
  1888. self.assertEqual(len(other_constraints), 1)
  1889. # Remove unique together
  1890. unique_together = AuthorWithUniqueNameAndBirthday._meta.unique_together
  1891. with connection.schema_editor() as editor:
  1892. editor.alter_unique_together(AuthorWithUniqueNameAndBirthday, unique_together, [])
  1893. constraints = self.get_constraints(AuthorWithUniqueNameAndBirthday._meta.db_table)
  1894. self.assertIn(custom_constraint_name, constraints)
  1895. other_constraints = [
  1896. name for name, details in constraints.items()
  1897. if details['columns'] == ['name', 'birthday'] and details['unique'] and name != custom_constraint_name
  1898. ]
  1899. self.assertEqual(len(other_constraints), 0)
  1900. # Re-add unique together
  1901. with connection.schema_editor() as editor:
  1902. editor.alter_unique_together(AuthorWithUniqueNameAndBirthday, [], unique_together)
  1903. constraints = self.get_constraints(AuthorWithUniqueNameAndBirthday._meta.db_table)
  1904. self.assertIn(custom_constraint_name, constraints)
  1905. other_constraints = [
  1906. name for name, details in constraints.items()
  1907. if details['columns'] == ['name', 'birthday'] and details['unique'] and name != custom_constraint_name
  1908. ]
  1909. self.assertEqual(len(other_constraints), 1)
  1910. # Drop the unique constraint
  1911. with connection.schema_editor() as editor:
  1912. AuthorWithUniqueNameAndBirthday._meta.constraints = []
  1913. editor.remove_constraint(AuthorWithUniqueNameAndBirthday, constraint)
  1914. def test_index_together(self):
  1915. """
  1916. Tests removing and adding index_together constraints on a model.
  1917. """
  1918. # Create the table
  1919. with connection.schema_editor() as editor:
  1920. editor.create_model(Tag)
  1921. # Ensure there's no index on the year/slug columns first
  1922. self.assertIs(
  1923. any(
  1924. c["index"]
  1925. for c in self.get_constraints("schema_tag").values()
  1926. if c['columns'] == ["slug", "title"]
  1927. ),
  1928. False,
  1929. )
  1930. # Alter the model to add an index
  1931. with connection.schema_editor() as editor:
  1932. editor.alter_index_together(Tag, [], [("slug", "title")])
  1933. # Ensure there is now an index
  1934. self.assertIs(
  1935. any(
  1936. c["index"]
  1937. for c in self.get_constraints("schema_tag").values()
  1938. if c['columns'] == ["slug", "title"]
  1939. ),
  1940. True,
  1941. )
  1942. # Alter it back
  1943. new_field2 = SlugField(unique=True)
  1944. new_field2.set_attributes_from_name("slug")
  1945. with connection.schema_editor() as editor:
  1946. editor.alter_index_together(Tag, [("slug", "title")], [])
  1947. # Ensure there's no index
  1948. self.assertIs(
  1949. any(
  1950. c["index"]
  1951. for c in self.get_constraints("schema_tag").values()
  1952. if c['columns'] == ["slug", "title"]
  1953. ),
  1954. False,
  1955. )
  1956. def test_index_together_with_fk(self):
  1957. """
  1958. Tests removing and adding index_together constraints that include
  1959. a foreign key.
  1960. """
  1961. # Create the table
  1962. with connection.schema_editor() as editor:
  1963. editor.create_model(Author)
  1964. editor.create_model(Book)
  1965. # Ensure the fields are unique to begin with
  1966. self.assertEqual(Book._meta.index_together, ())
  1967. # Add the unique_together constraint
  1968. with connection.schema_editor() as editor:
  1969. editor.alter_index_together(Book, [], [['author', 'title']])
  1970. # Alter it back
  1971. with connection.schema_editor() as editor:
  1972. editor.alter_index_together(Book, [['author', 'title']], [])
  1973. def test_create_index_together(self):
  1974. """
  1975. Tests creating models with index_together already defined
  1976. """
  1977. # Create the table
  1978. with connection.schema_editor() as editor:
  1979. editor.create_model(TagIndexed)
  1980. # Ensure there is an index
  1981. self.assertIs(
  1982. any(
  1983. c["index"]
  1984. for c in self.get_constraints("schema_tagindexed").values()
  1985. if c['columns'] == ["slug", "title"]
  1986. ),
  1987. True,
  1988. )
  1989. @skipUnlessDBFeature('allows_multiple_constraints_on_same_fields')
  1990. def test_remove_index_together_does_not_remove_meta_indexes(self):
  1991. with connection.schema_editor() as editor:
  1992. editor.create_model(AuthorWithIndexedNameAndBirthday)
  1993. # Add the custom index
  1994. index = Index(fields=['name', 'birthday'], name='author_name_birthday_idx')
  1995. custom_index_name = index.name
  1996. AuthorWithIndexedNameAndBirthday._meta.indexes = [index]
  1997. with connection.schema_editor() as editor:
  1998. editor.add_index(AuthorWithIndexedNameAndBirthday, index)
  1999. # Ensure the indexes exist
  2000. constraints = self.get_constraints(AuthorWithIndexedNameAndBirthday._meta.db_table)
  2001. self.assertIn(custom_index_name, constraints)
  2002. other_constraints = [
  2003. name for name, details in constraints.items()
  2004. if details['columns'] == ['name', 'birthday'] and details['index'] and name != custom_index_name
  2005. ]
  2006. self.assertEqual(len(other_constraints), 1)
  2007. # Remove index together
  2008. index_together = AuthorWithIndexedNameAndBirthday._meta.index_together
  2009. with connection.schema_editor() as editor:
  2010. editor.alter_index_together(AuthorWithIndexedNameAndBirthday, index_together, [])
  2011. constraints = self.get_constraints(AuthorWithIndexedNameAndBirthday._meta.db_table)
  2012. self.assertIn(custom_index_name, constraints)
  2013. other_constraints = [
  2014. name for name, details in constraints.items()
  2015. if details['columns'] == ['name', 'birthday'] and details['index'] and name != custom_index_name
  2016. ]
  2017. self.assertEqual(len(other_constraints), 0)
  2018. # Re-add index together
  2019. with connection.schema_editor() as editor:
  2020. editor.alter_index_together(AuthorWithIndexedNameAndBirthday, [], index_together)
  2021. constraints = self.get_constraints(AuthorWithIndexedNameAndBirthday._meta.db_table)
  2022. self.assertIn(custom_index_name, constraints)
  2023. other_constraints = [
  2024. name for name, details in constraints.items()
  2025. if details['columns'] == ['name', 'birthday'] and details['index'] and name != custom_index_name
  2026. ]
  2027. self.assertEqual(len(other_constraints), 1)
  2028. # Drop the index
  2029. with connection.schema_editor() as editor:
  2030. AuthorWithIndexedNameAndBirthday._meta.indexes = []
  2031. editor.remove_index(AuthorWithIndexedNameAndBirthday, index)
  2032. @isolate_apps('schema')
  2033. def test_db_table(self):
  2034. """
  2035. Tests renaming of the table
  2036. """
  2037. class Author(Model):
  2038. name = CharField(max_length=255)
  2039. class Meta:
  2040. app_label = 'schema'
  2041. class Book(Model):
  2042. author = ForeignKey(Author, CASCADE)
  2043. class Meta:
  2044. app_label = 'schema'
  2045. # Create the table and one referring it.
  2046. with connection.schema_editor() as editor:
  2047. editor.create_model(Author)
  2048. editor.create_model(Book)
  2049. # Ensure the table is there to begin with
  2050. columns = self.column_classes(Author)
  2051. self.assertEqual(columns['name'][0], connection.features.introspected_field_types['CharField'])
  2052. # Alter the table
  2053. with connection.schema_editor(atomic=connection.features.supports_atomic_references_rename) as editor:
  2054. editor.alter_db_table(Author, "schema_author", "schema_otherauthor")
  2055. # Ensure the table is there afterwards
  2056. Author._meta.db_table = "schema_otherauthor"
  2057. columns = self.column_classes(Author)
  2058. self.assertEqual(columns['name'][0], connection.features.introspected_field_types['CharField'])
  2059. # Ensure the foreign key reference was updated
  2060. self.assertForeignKeyExists(Book, "author_id", "schema_otherauthor")
  2061. # Alter the table again
  2062. with connection.schema_editor(atomic=connection.features.supports_atomic_references_rename) as editor:
  2063. editor.alter_db_table(Author, "schema_otherauthor", "schema_author")
  2064. # Ensure the table is still there
  2065. Author._meta.db_table = "schema_author"
  2066. columns = self.column_classes(Author)
  2067. self.assertEqual(columns['name'][0], connection.features.introspected_field_types['CharField'])
  2068. def test_add_remove_index(self):
  2069. """
  2070. Tests index addition and removal
  2071. """
  2072. # Create the table
  2073. with connection.schema_editor() as editor:
  2074. editor.create_model(Author)
  2075. # Ensure the table is there and has no index
  2076. self.assertNotIn('title', self.get_indexes(Author._meta.db_table))
  2077. # Add the index
  2078. index = Index(fields=['name'], name='author_title_idx')
  2079. with connection.schema_editor() as editor:
  2080. editor.add_index(Author, index)
  2081. self.assertIn('name', self.get_indexes(Author._meta.db_table))
  2082. # Drop the index
  2083. with connection.schema_editor() as editor:
  2084. editor.remove_index(Author, index)
  2085. self.assertNotIn('name', self.get_indexes(Author._meta.db_table))
  2086. def test_remove_db_index_doesnt_remove_custom_indexes(self):
  2087. """
  2088. Changing db_index to False doesn't remove indexes from Meta.indexes.
  2089. """
  2090. with connection.schema_editor() as editor:
  2091. editor.create_model(AuthorWithIndexedName)
  2092. # Ensure the table has its index
  2093. self.assertIn('name', self.get_indexes(AuthorWithIndexedName._meta.db_table))
  2094. # Add the custom index
  2095. index = Index(fields=['-name'], name='author_name_idx')
  2096. author_index_name = index.name
  2097. with connection.schema_editor() as editor:
  2098. db_index_name = editor._create_index_name(
  2099. table_name=AuthorWithIndexedName._meta.db_table,
  2100. column_names=('name',),
  2101. )
  2102. try:
  2103. AuthorWithIndexedName._meta.indexes = [index]
  2104. with connection.schema_editor() as editor:
  2105. editor.add_index(AuthorWithIndexedName, index)
  2106. old_constraints = self.get_constraints(AuthorWithIndexedName._meta.db_table)
  2107. self.assertIn(author_index_name, old_constraints)
  2108. self.assertIn(db_index_name, old_constraints)
  2109. # Change name field to db_index=False
  2110. old_field = AuthorWithIndexedName._meta.get_field('name')
  2111. new_field = CharField(max_length=255)
  2112. new_field.set_attributes_from_name('name')
  2113. with connection.schema_editor() as editor:
  2114. editor.alter_field(AuthorWithIndexedName, old_field, new_field, strict=True)
  2115. new_constraints = self.get_constraints(AuthorWithIndexedName._meta.db_table)
  2116. self.assertNotIn(db_index_name, new_constraints)
  2117. # The index from Meta.indexes is still in the database.
  2118. self.assertIn(author_index_name, new_constraints)
  2119. # Drop the index
  2120. with connection.schema_editor() as editor:
  2121. editor.remove_index(AuthorWithIndexedName, index)
  2122. finally:
  2123. AuthorWithIndexedName._meta.indexes = []
  2124. def test_order_index(self):
  2125. """
  2126. Indexes defined with ordering (ASC/DESC) defined on column
  2127. """
  2128. with connection.schema_editor() as editor:
  2129. editor.create_model(Author)
  2130. # The table doesn't have an index
  2131. self.assertNotIn('title', self.get_indexes(Author._meta.db_table))
  2132. index_name = 'author_name_idx'
  2133. # Add the index
  2134. index = Index(fields=['name', '-weight'], name=index_name)
  2135. with connection.schema_editor() as editor:
  2136. editor.add_index(Author, index)
  2137. if connection.features.supports_index_column_ordering:
  2138. self.assertIndexOrder(Author._meta.db_table, index_name, ['ASC', 'DESC'])
  2139. # Drop the index
  2140. with connection.schema_editor() as editor:
  2141. editor.remove_index(Author, index)
  2142. def test_indexes(self):
  2143. """
  2144. Tests creation/altering of indexes
  2145. """
  2146. # Create the table
  2147. with connection.schema_editor() as editor:
  2148. editor.create_model(Author)
  2149. editor.create_model(Book)
  2150. # Ensure the table is there and has the right index
  2151. self.assertIn(
  2152. "title",
  2153. self.get_indexes(Book._meta.db_table),
  2154. )
  2155. # Alter to remove the index
  2156. old_field = Book._meta.get_field("title")
  2157. new_field = CharField(max_length=100, db_index=False)
  2158. new_field.set_attributes_from_name("title")
  2159. with connection.schema_editor() as editor:
  2160. editor.alter_field(Book, old_field, new_field, strict=True)
  2161. # Ensure the table is there and has no index
  2162. self.assertNotIn(
  2163. "title",
  2164. self.get_indexes(Book._meta.db_table),
  2165. )
  2166. # Alter to re-add the index
  2167. new_field2 = Book._meta.get_field("title")
  2168. with connection.schema_editor() as editor:
  2169. editor.alter_field(Book, new_field, new_field2, strict=True)
  2170. # Ensure the table is there and has the index again
  2171. self.assertIn(
  2172. "title",
  2173. self.get_indexes(Book._meta.db_table),
  2174. )
  2175. # Add a unique column, verify that creates an implicit index
  2176. new_field3 = BookWithSlug._meta.get_field("slug")
  2177. with connection.schema_editor() as editor:
  2178. editor.add_field(Book, new_field3)
  2179. self.assertIn(
  2180. "slug",
  2181. self.get_uniques(Book._meta.db_table),
  2182. )
  2183. # Remove the unique, check the index goes with it
  2184. new_field4 = CharField(max_length=20, unique=False)
  2185. new_field4.set_attributes_from_name("slug")
  2186. with connection.schema_editor() as editor:
  2187. editor.alter_field(BookWithSlug, new_field3, new_field4, strict=True)
  2188. self.assertNotIn(
  2189. "slug",
  2190. self.get_uniques(Book._meta.db_table),
  2191. )
  2192. def test_text_field_with_db_index(self):
  2193. with connection.schema_editor() as editor:
  2194. editor.create_model(AuthorTextFieldWithIndex)
  2195. # The text_field index is present if the database supports it.
  2196. assertion = self.assertIn if connection.features.supports_index_on_text_field else self.assertNotIn
  2197. assertion('text_field', self.get_indexes(AuthorTextFieldWithIndex._meta.db_table))
  2198. def test_primary_key(self):
  2199. """
  2200. Tests altering of the primary key
  2201. """
  2202. # Create the table
  2203. with connection.schema_editor() as editor:
  2204. editor.create_model(Tag)
  2205. # Ensure the table is there and has the right PK
  2206. self.assertEqual(self.get_primary_key(Tag._meta.db_table), 'id')
  2207. # Alter to change the PK
  2208. id_field = Tag._meta.get_field("id")
  2209. old_field = Tag._meta.get_field("slug")
  2210. new_field = SlugField(primary_key=True)
  2211. new_field.set_attributes_from_name("slug")
  2212. new_field.model = Tag
  2213. with connection.schema_editor() as editor:
  2214. editor.remove_field(Tag, id_field)
  2215. editor.alter_field(Tag, old_field, new_field)
  2216. # Ensure the PK changed
  2217. self.assertNotIn(
  2218. 'id',
  2219. self.get_indexes(Tag._meta.db_table),
  2220. )
  2221. self.assertEqual(self.get_primary_key(Tag._meta.db_table), 'slug')
  2222. def test_context_manager_exit(self):
  2223. """
  2224. Ensures transaction is correctly closed when an error occurs
  2225. inside a SchemaEditor context.
  2226. """
  2227. class SomeError(Exception):
  2228. pass
  2229. try:
  2230. with connection.schema_editor():
  2231. raise SomeError
  2232. except SomeError:
  2233. self.assertFalse(connection.in_atomic_block)
  2234. @skipIfDBFeature('can_rollback_ddl')
  2235. def test_unsupported_transactional_ddl_disallowed(self):
  2236. message = (
  2237. "Executing DDL statements while in a transaction on databases "
  2238. "that can't perform a rollback is prohibited."
  2239. )
  2240. with atomic(), connection.schema_editor() as editor:
  2241. with self.assertRaisesMessage(TransactionManagementError, message):
  2242. editor.execute(editor.sql_create_table % {'table': 'foo', 'definition': ''})
  2243. @skipUnlessDBFeature('supports_foreign_keys')
  2244. def test_foreign_key_index_long_names_regression(self):
  2245. """
  2246. Regression test for #21497.
  2247. Only affects databases that supports foreign keys.
  2248. """
  2249. # Create the table
  2250. with connection.schema_editor() as editor:
  2251. editor.create_model(AuthorWithEvenLongerName)
  2252. editor.create_model(BookWithLongName)
  2253. # Find the properly shortened column name
  2254. column_name = connection.ops.quote_name("author_foreign_key_with_really_long_field_name_id")
  2255. column_name = column_name[1:-1].lower() # unquote, and, for Oracle, un-upcase
  2256. # Ensure the table is there and has an index on the column
  2257. self.assertIn(
  2258. column_name,
  2259. self.get_indexes(BookWithLongName._meta.db_table),
  2260. )
  2261. @skipUnlessDBFeature('supports_foreign_keys')
  2262. def test_add_foreign_key_long_names(self):
  2263. """
  2264. Regression test for #23009.
  2265. Only affects databases that supports foreign keys.
  2266. """
  2267. # Create the initial tables
  2268. with connection.schema_editor() as editor:
  2269. editor.create_model(AuthorWithEvenLongerName)
  2270. editor.create_model(BookWithLongName)
  2271. # Add a second FK, this would fail due to long ref name before the fix
  2272. new_field = ForeignKey(AuthorWithEvenLongerName, CASCADE, related_name="something")
  2273. new_field.set_attributes_from_name("author_other_really_long_named_i_mean_so_long_fk")
  2274. with connection.schema_editor() as editor:
  2275. editor.add_field(BookWithLongName, new_field)
  2276. @isolate_apps('schema')
  2277. @skipUnlessDBFeature('supports_foreign_keys')
  2278. def test_add_foreign_key_quoted_db_table(self):
  2279. class Author(Model):
  2280. class Meta:
  2281. db_table = '"table_author_double_quoted"'
  2282. app_label = 'schema'
  2283. class Book(Model):
  2284. author = ForeignKey(Author, CASCADE)
  2285. class Meta:
  2286. app_label = 'schema'
  2287. with connection.schema_editor() as editor:
  2288. editor.create_model(Author)
  2289. editor.create_model(Book)
  2290. if connection.vendor == 'mysql':
  2291. self.assertForeignKeyExists(Book, 'author_id', '"table_author_double_quoted"')
  2292. else:
  2293. self.assertForeignKeyExists(Book, 'author_id', 'table_author_double_quoted')
  2294. def test_add_foreign_object(self):
  2295. with connection.schema_editor() as editor:
  2296. editor.create_model(BookForeignObj)
  2297. new_field = ForeignObject(Author, on_delete=CASCADE, from_fields=['author_id'], to_fields=['id'])
  2298. new_field.set_attributes_from_name('author')
  2299. with connection.schema_editor() as editor:
  2300. editor.add_field(BookForeignObj, new_field)
  2301. def test_creation_deletion_reserved_names(self):
  2302. """
  2303. Tries creating a model's table, and then deleting it when it has a
  2304. SQL reserved name.
  2305. """
  2306. # Create the table
  2307. with connection.schema_editor() as editor:
  2308. try:
  2309. editor.create_model(Thing)
  2310. except OperationalError as e:
  2311. self.fail("Errors when applying initial migration for a model "
  2312. "with a table named after an SQL reserved word: %s" % e)
  2313. # The table is there
  2314. list(Thing.objects.all())
  2315. # Clean up that table
  2316. with connection.schema_editor() as editor:
  2317. editor.delete_model(Thing)
  2318. # The table is gone
  2319. with self.assertRaises(DatabaseError):
  2320. list(Thing.objects.all())
  2321. def test_remove_constraints_capital_letters(self):
  2322. """
  2323. #23065 - Constraint names must be quoted if they contain capital letters.
  2324. """
  2325. def get_field(*args, field_class=IntegerField, **kwargs):
  2326. kwargs['db_column'] = "CamelCase"
  2327. field = field_class(*args, **kwargs)
  2328. field.set_attributes_from_name("CamelCase")
  2329. return field
  2330. model = Author
  2331. field = get_field()
  2332. table = model._meta.db_table
  2333. column = field.column
  2334. identifier_converter = connection.introspection.identifier_converter
  2335. with connection.schema_editor() as editor:
  2336. editor.create_model(model)
  2337. editor.add_field(model, field)
  2338. constraint_name = 'CamelCaseIndex'
  2339. expected_constraint_name = identifier_converter(constraint_name)
  2340. editor.execute(
  2341. editor.sql_create_index % {
  2342. "table": editor.quote_name(table),
  2343. "name": editor.quote_name(constraint_name),
  2344. "using": "",
  2345. "columns": editor.quote_name(column),
  2346. "extra": "",
  2347. "condition": "",
  2348. "include": "",
  2349. }
  2350. )
  2351. self.assertIn(expected_constraint_name, self.get_constraints(model._meta.db_table))
  2352. editor.alter_field(model, get_field(db_index=True), field, strict=True)
  2353. self.assertNotIn(expected_constraint_name, self.get_constraints(model._meta.db_table))
  2354. constraint_name = 'CamelCaseUniqConstraint'
  2355. expected_constraint_name = identifier_converter(constraint_name)
  2356. editor.execute(editor._create_unique_sql(model, [field.column], constraint_name))
  2357. self.assertIn(expected_constraint_name, self.get_constraints(model._meta.db_table))
  2358. editor.alter_field(model, get_field(unique=True), field, strict=True)
  2359. self.assertNotIn(expected_constraint_name, self.get_constraints(model._meta.db_table))
  2360. if editor.sql_create_fk:
  2361. constraint_name = 'CamelCaseFKConstraint'
  2362. expected_constraint_name = identifier_converter(constraint_name)
  2363. editor.execute(
  2364. editor.sql_create_fk % {
  2365. "table": editor.quote_name(table),
  2366. "name": editor.quote_name(constraint_name),
  2367. "column": editor.quote_name(column),
  2368. "to_table": editor.quote_name(table),
  2369. "to_column": editor.quote_name(model._meta.auto_field.column),
  2370. "deferrable": connection.ops.deferrable_sql(),
  2371. }
  2372. )
  2373. self.assertIn(expected_constraint_name, self.get_constraints(model._meta.db_table))
  2374. editor.alter_field(model, get_field(Author, CASCADE, field_class=ForeignKey), field, strict=True)
  2375. self.assertNotIn(expected_constraint_name, self.get_constraints(model._meta.db_table))
  2376. def test_add_field_use_effective_default(self):
  2377. """
  2378. #23987 - effective_default() should be used as the field default when
  2379. adding a new field.
  2380. """
  2381. # Create the table
  2382. with connection.schema_editor() as editor:
  2383. editor.create_model(Author)
  2384. # Ensure there's no surname field
  2385. columns = self.column_classes(Author)
  2386. self.assertNotIn("surname", columns)
  2387. # Create a row
  2388. Author.objects.create(name='Anonymous1')
  2389. # Add new CharField to ensure default will be used from effective_default
  2390. new_field = CharField(max_length=15, blank=True)
  2391. new_field.set_attributes_from_name("surname")
  2392. with connection.schema_editor() as editor:
  2393. editor.add_field(Author, new_field)
  2394. # Ensure field was added with the right default
  2395. with connection.cursor() as cursor:
  2396. cursor.execute("SELECT surname FROM schema_author;")
  2397. item = cursor.fetchall()[0]
  2398. self.assertEqual(item[0], None if connection.features.interprets_empty_strings_as_nulls else '')
  2399. def test_add_field_default_dropped(self):
  2400. # Create the table
  2401. with connection.schema_editor() as editor:
  2402. editor.create_model(Author)
  2403. # Ensure there's no surname field
  2404. columns = self.column_classes(Author)
  2405. self.assertNotIn("surname", columns)
  2406. # Create a row
  2407. Author.objects.create(name='Anonymous1')
  2408. # Add new CharField with a default
  2409. new_field = CharField(max_length=15, blank=True, default='surname default')
  2410. new_field.set_attributes_from_name("surname")
  2411. with connection.schema_editor() as editor:
  2412. editor.add_field(Author, new_field)
  2413. # Ensure field was added with the right default
  2414. with connection.cursor() as cursor:
  2415. cursor.execute("SELECT surname FROM schema_author;")
  2416. item = cursor.fetchall()[0]
  2417. self.assertEqual(item[0], 'surname default')
  2418. # And that the default is no longer set in the database.
  2419. field = next(
  2420. f for f in connection.introspection.get_table_description(cursor, "schema_author")
  2421. if f.name == "surname"
  2422. )
  2423. if connection.features.can_introspect_default:
  2424. self.assertIsNone(field.default)
  2425. def test_alter_field_default_dropped(self):
  2426. # Create the table
  2427. with connection.schema_editor() as editor:
  2428. editor.create_model(Author)
  2429. # Create a row
  2430. Author.objects.create(name='Anonymous1')
  2431. self.assertIsNone(Author.objects.get().height)
  2432. old_field = Author._meta.get_field('height')
  2433. # The default from the new field is used in updating existing rows.
  2434. new_field = IntegerField(blank=True, default=42)
  2435. new_field.set_attributes_from_name('height')
  2436. with connection.schema_editor() as editor:
  2437. editor.alter_field(Author, old_field, new_field, strict=True)
  2438. self.assertEqual(Author.objects.get().height, 42)
  2439. # The database default should be removed.
  2440. with connection.cursor() as cursor:
  2441. field = next(
  2442. f for f in connection.introspection.get_table_description(cursor, "schema_author")
  2443. if f.name == "height"
  2444. )
  2445. if connection.features.can_introspect_default:
  2446. self.assertIsNone(field.default)
  2447. @unittest.skipIf(connection.vendor == 'sqlite', 'SQLite naively remakes the table on field alteration.')
  2448. def test_alter_field_default_doesnt_perform_queries(self):
  2449. """
  2450. No queries are performed if a field default changes and the field's
  2451. not changing from null to non-null.
  2452. """
  2453. with connection.schema_editor() as editor:
  2454. editor.create_model(AuthorWithDefaultHeight)
  2455. old_field = AuthorWithDefaultHeight._meta.get_field('height')
  2456. new_default = old_field.default * 2
  2457. new_field = PositiveIntegerField(null=True, blank=True, default=new_default)
  2458. new_field.set_attributes_from_name('height')
  2459. with connection.schema_editor() as editor, self.assertNumQueries(0):
  2460. editor.alter_field(AuthorWithDefaultHeight, old_field, new_field, strict=True)
  2461. def test_add_textfield_unhashable_default(self):
  2462. # Create the table
  2463. with connection.schema_editor() as editor:
  2464. editor.create_model(Author)
  2465. # Create a row
  2466. Author.objects.create(name='Anonymous1')
  2467. # Create a field that has an unhashable default
  2468. new_field = TextField(default={})
  2469. new_field.set_attributes_from_name("info")
  2470. with connection.schema_editor() as editor:
  2471. editor.add_field(Author, new_field)
  2472. @unittest.skipUnless(connection.vendor == 'postgresql', "PostgreSQL specific")
  2473. def test_add_indexed_charfield(self):
  2474. field = CharField(max_length=255, db_index=True)
  2475. field.set_attributes_from_name('nom_de_plume')
  2476. with connection.schema_editor() as editor:
  2477. editor.create_model(Author)
  2478. editor.add_field(Author, field)
  2479. # Should create two indexes; one for like operator.
  2480. self.assertEqual(
  2481. self.get_constraints_for_column(Author, 'nom_de_plume'),
  2482. ['schema_author_nom_de_plume_7570a851', 'schema_author_nom_de_plume_7570a851_like'],
  2483. )
  2484. @unittest.skipUnless(connection.vendor == 'postgresql', "PostgreSQL specific")
  2485. def test_add_unique_charfield(self):
  2486. field = CharField(max_length=255, unique=True)
  2487. field.set_attributes_from_name('nom_de_plume')
  2488. with connection.schema_editor() as editor:
  2489. editor.create_model(Author)
  2490. editor.add_field(Author, field)
  2491. # Should create two indexes; one for like operator.
  2492. self.assertEqual(
  2493. self.get_constraints_for_column(Author, 'nom_de_plume'),
  2494. ['schema_author_nom_de_plume_7570a851_like', 'schema_author_nom_de_plume_key']
  2495. )
  2496. @unittest.skipUnless(connection.vendor == 'postgresql', "PostgreSQL specific")
  2497. def test_alter_field_add_index_to_charfield(self):
  2498. # Create the table and verify no initial indexes.
  2499. with connection.schema_editor() as editor:
  2500. editor.create_model(Author)
  2501. self.assertEqual(self.get_constraints_for_column(Author, 'name'), [])
  2502. # Alter to add db_index=True and create 2 indexes.
  2503. old_field = Author._meta.get_field('name')
  2504. new_field = CharField(max_length=255, db_index=True)
  2505. new_field.set_attributes_from_name('name')
  2506. with connection.schema_editor() as editor:
  2507. editor.alter_field(Author, old_field, new_field, strict=True)
  2508. self.assertEqual(
  2509. self.get_constraints_for_column(Author, 'name'),
  2510. ['schema_author_name_1fbc5617', 'schema_author_name_1fbc5617_like']
  2511. )
  2512. # Remove db_index=True to drop both indexes.
  2513. with connection.schema_editor() as editor:
  2514. editor.alter_field(Author, new_field, old_field, strict=True)
  2515. self.assertEqual(self.get_constraints_for_column(Author, 'name'), [])
  2516. @unittest.skipUnless(connection.vendor == 'postgresql', "PostgreSQL specific")
  2517. def test_alter_field_add_unique_to_charfield(self):
  2518. # Create the table and verify no initial indexes.
  2519. with connection.schema_editor() as editor:
  2520. editor.create_model(Author)
  2521. self.assertEqual(self.get_constraints_for_column(Author, 'name'), [])
  2522. # Alter to add unique=True and create 2 indexes.
  2523. old_field = Author._meta.get_field('name')
  2524. new_field = CharField(max_length=255, unique=True)
  2525. new_field.set_attributes_from_name('name')
  2526. with connection.schema_editor() as editor:
  2527. editor.alter_field(Author, old_field, new_field, strict=True)
  2528. self.assertEqual(
  2529. self.get_constraints_for_column(Author, 'name'),
  2530. ['schema_author_name_1fbc5617_like', 'schema_author_name_1fbc5617_uniq']
  2531. )
  2532. # Remove unique=True to drop both indexes.
  2533. with connection.schema_editor() as editor:
  2534. editor.alter_field(Author, new_field, old_field, strict=True)
  2535. self.assertEqual(self.get_constraints_for_column(Author, 'name'), [])
  2536. @unittest.skipUnless(connection.vendor == 'postgresql', "PostgreSQL specific")
  2537. def test_alter_field_add_index_to_textfield(self):
  2538. # Create the table and verify no initial indexes.
  2539. with connection.schema_editor() as editor:
  2540. editor.create_model(Note)
  2541. self.assertEqual(self.get_constraints_for_column(Note, 'info'), [])
  2542. # Alter to add db_index=True and create 2 indexes.
  2543. old_field = Note._meta.get_field('info')
  2544. new_field = TextField(db_index=True)
  2545. new_field.set_attributes_from_name('info')
  2546. with connection.schema_editor() as editor:
  2547. editor.alter_field(Note, old_field, new_field, strict=True)
  2548. self.assertEqual(
  2549. self.get_constraints_for_column(Note, 'info'),
  2550. ['schema_note_info_4b0ea695', 'schema_note_info_4b0ea695_like']
  2551. )
  2552. # Remove db_index=True to drop both indexes.
  2553. with connection.schema_editor() as editor:
  2554. editor.alter_field(Note, new_field, old_field, strict=True)
  2555. self.assertEqual(self.get_constraints_for_column(Note, 'info'), [])
  2556. @unittest.skipUnless(connection.vendor == 'postgresql', "PostgreSQL specific")
  2557. def test_alter_field_add_unique_to_charfield_with_db_index(self):
  2558. # Create the table and verify initial indexes.
  2559. with connection.schema_editor() as editor:
  2560. editor.create_model(BookWithoutAuthor)
  2561. self.assertEqual(
  2562. self.get_constraints_for_column(BookWithoutAuthor, 'title'),
  2563. ['schema_book_title_2dfb2dff', 'schema_book_title_2dfb2dff_like']
  2564. )
  2565. # Alter to add unique=True (should replace the index)
  2566. old_field = BookWithoutAuthor._meta.get_field('title')
  2567. new_field = CharField(max_length=100, db_index=True, unique=True)
  2568. new_field.set_attributes_from_name('title')
  2569. with connection.schema_editor() as editor:
  2570. editor.alter_field(BookWithoutAuthor, old_field, new_field, strict=True)
  2571. self.assertEqual(
  2572. self.get_constraints_for_column(BookWithoutAuthor, 'title'),
  2573. ['schema_book_title_2dfb2dff_like', 'schema_book_title_2dfb2dff_uniq']
  2574. )
  2575. # Alter to remove unique=True (should drop unique index)
  2576. new_field2 = CharField(max_length=100, db_index=True)
  2577. new_field2.set_attributes_from_name('title')
  2578. with connection.schema_editor() as editor:
  2579. editor.alter_field(BookWithoutAuthor, new_field, new_field2, strict=True)
  2580. self.assertEqual(
  2581. self.get_constraints_for_column(BookWithoutAuthor, 'title'),
  2582. ['schema_book_title_2dfb2dff', 'schema_book_title_2dfb2dff_like']
  2583. )
  2584. @unittest.skipUnless(connection.vendor == 'postgresql', "PostgreSQL specific")
  2585. def test_alter_field_remove_unique_and_db_index_from_charfield(self):
  2586. # Create the table and verify initial indexes.
  2587. with connection.schema_editor() as editor:
  2588. editor.create_model(BookWithoutAuthor)
  2589. self.assertEqual(
  2590. self.get_constraints_for_column(BookWithoutAuthor, 'title'),
  2591. ['schema_book_title_2dfb2dff', 'schema_book_title_2dfb2dff_like']
  2592. )
  2593. # Alter to add unique=True (should replace the index)
  2594. old_field = BookWithoutAuthor._meta.get_field('title')
  2595. new_field = CharField(max_length=100, db_index=True, unique=True)
  2596. new_field.set_attributes_from_name('title')
  2597. with connection.schema_editor() as editor:
  2598. editor.alter_field(BookWithoutAuthor, old_field, new_field, strict=True)
  2599. self.assertEqual(
  2600. self.get_constraints_for_column(BookWithoutAuthor, 'title'),
  2601. ['schema_book_title_2dfb2dff_like', 'schema_book_title_2dfb2dff_uniq']
  2602. )
  2603. # Alter to remove both unique=True and db_index=True (should drop all indexes)
  2604. new_field2 = CharField(max_length=100)
  2605. new_field2.set_attributes_from_name('title')
  2606. with connection.schema_editor() as editor:
  2607. editor.alter_field(BookWithoutAuthor, new_field, new_field2, strict=True)
  2608. self.assertEqual(self.get_constraints_for_column(BookWithoutAuthor, 'title'), [])
  2609. @unittest.skipUnless(connection.vendor == 'postgresql', "PostgreSQL specific")
  2610. def test_alter_field_swap_unique_and_db_index_with_charfield(self):
  2611. # Create the table and verify initial indexes.
  2612. with connection.schema_editor() as editor:
  2613. editor.create_model(BookWithoutAuthor)
  2614. self.assertEqual(
  2615. self.get_constraints_for_column(BookWithoutAuthor, 'title'),
  2616. ['schema_book_title_2dfb2dff', 'schema_book_title_2dfb2dff_like']
  2617. )
  2618. # Alter to set unique=True and remove db_index=True (should replace the index)
  2619. old_field = BookWithoutAuthor._meta.get_field('title')
  2620. new_field = CharField(max_length=100, unique=True)
  2621. new_field.set_attributes_from_name('title')
  2622. with connection.schema_editor() as editor:
  2623. editor.alter_field(BookWithoutAuthor, old_field, new_field, strict=True)
  2624. self.assertEqual(
  2625. self.get_constraints_for_column(BookWithoutAuthor, 'title'),
  2626. ['schema_book_title_2dfb2dff_like', 'schema_book_title_2dfb2dff_uniq']
  2627. )
  2628. # Alter to set db_index=True and remove unique=True (should restore index)
  2629. new_field2 = CharField(max_length=100, db_index=True)
  2630. new_field2.set_attributes_from_name('title')
  2631. with connection.schema_editor() as editor:
  2632. editor.alter_field(BookWithoutAuthor, new_field, new_field2, strict=True)
  2633. self.assertEqual(
  2634. self.get_constraints_for_column(BookWithoutAuthor, 'title'),
  2635. ['schema_book_title_2dfb2dff', 'schema_book_title_2dfb2dff_like']
  2636. )
  2637. @unittest.skipUnless(connection.vendor == 'postgresql', "PostgreSQL specific")
  2638. def test_alter_field_add_db_index_to_charfield_with_unique(self):
  2639. # Create the table and verify initial indexes.
  2640. with connection.schema_editor() as editor:
  2641. editor.create_model(Tag)
  2642. self.assertEqual(
  2643. self.get_constraints_for_column(Tag, 'slug'),
  2644. ['schema_tag_slug_2c418ba3_like', 'schema_tag_slug_key']
  2645. )
  2646. # Alter to add db_index=True
  2647. old_field = Tag._meta.get_field('slug')
  2648. new_field = SlugField(db_index=True, unique=True)
  2649. new_field.set_attributes_from_name('slug')
  2650. with connection.schema_editor() as editor:
  2651. editor.alter_field(Tag, old_field, new_field, strict=True)
  2652. self.assertEqual(
  2653. self.get_constraints_for_column(Tag, 'slug'),
  2654. ['schema_tag_slug_2c418ba3_like', 'schema_tag_slug_key']
  2655. )
  2656. # Alter to remove db_index=True
  2657. new_field2 = SlugField(unique=True)
  2658. new_field2.set_attributes_from_name('slug')
  2659. with connection.schema_editor() as editor:
  2660. editor.alter_field(Tag, new_field, new_field2, strict=True)
  2661. self.assertEqual(
  2662. self.get_constraints_for_column(Tag, 'slug'),
  2663. ['schema_tag_slug_2c418ba3_like', 'schema_tag_slug_key']
  2664. )
  2665. def test_alter_field_add_index_to_integerfield(self):
  2666. # Create the table and verify no initial indexes.
  2667. with connection.schema_editor() as editor:
  2668. editor.create_model(Author)
  2669. self.assertEqual(self.get_constraints_for_column(Author, 'weight'), [])
  2670. # Alter to add db_index=True and create index.
  2671. old_field = Author._meta.get_field('weight')
  2672. new_field = IntegerField(null=True, db_index=True)
  2673. new_field.set_attributes_from_name('weight')
  2674. with connection.schema_editor() as editor:
  2675. editor.alter_field(Author, old_field, new_field, strict=True)
  2676. self.assertEqual(self.get_constraints_for_column(Author, 'weight'), ['schema_author_weight_587740f9'])
  2677. # Remove db_index=True to drop index.
  2678. with connection.schema_editor() as editor:
  2679. editor.alter_field(Author, new_field, old_field, strict=True)
  2680. self.assertEqual(self.get_constraints_for_column(Author, 'weight'), [])
  2681. def test_alter_pk_with_self_referential_field(self):
  2682. """
  2683. Changing the primary key field name of a model with a self-referential
  2684. foreign key (#26384).
  2685. """
  2686. with connection.schema_editor() as editor:
  2687. editor.create_model(Node)
  2688. old_field = Node._meta.get_field('node_id')
  2689. new_field = AutoField(primary_key=True)
  2690. new_field.set_attributes_from_name('id')
  2691. with connection.schema_editor() as editor:
  2692. editor.alter_field(Node, old_field, new_field, strict=True)
  2693. self.assertForeignKeyExists(Node, 'parent_id', Node._meta.db_table)
  2694. @mock.patch('django.db.backends.base.schema.datetime')
  2695. @mock.patch('django.db.backends.base.schema.timezone')
  2696. def test_add_datefield_and_datetimefield_use_effective_default(self, mocked_datetime, mocked_tz):
  2697. """
  2698. effective_default() should be used for DateField, DateTimeField, and
  2699. TimeField if auto_now or auto_now_add is set (#25005).
  2700. """
  2701. now = datetime.datetime(month=1, day=1, year=2000, hour=1, minute=1)
  2702. now_tz = datetime.datetime(month=1, day=1, year=2000, hour=1, minute=1, tzinfo=timezone.utc)
  2703. mocked_datetime.now = mock.MagicMock(return_value=now)
  2704. mocked_tz.now = mock.MagicMock(return_value=now_tz)
  2705. # Create the table
  2706. with connection.schema_editor() as editor:
  2707. editor.create_model(Author)
  2708. # Check auto_now/auto_now_add attributes are not defined
  2709. columns = self.column_classes(Author)
  2710. self.assertNotIn("dob_auto_now", columns)
  2711. self.assertNotIn("dob_auto_now_add", columns)
  2712. self.assertNotIn("dtob_auto_now", columns)
  2713. self.assertNotIn("dtob_auto_now_add", columns)
  2714. self.assertNotIn("tob_auto_now", columns)
  2715. self.assertNotIn("tob_auto_now_add", columns)
  2716. # Create a row
  2717. Author.objects.create(name='Anonymous1')
  2718. # Ensure fields were added with the correct defaults
  2719. dob_auto_now = DateField(auto_now=True)
  2720. dob_auto_now.set_attributes_from_name('dob_auto_now')
  2721. self.check_added_field_default(
  2722. editor, Author, dob_auto_now, 'dob_auto_now', now.date(),
  2723. cast_function=lambda x: x.date(),
  2724. )
  2725. dob_auto_now_add = DateField(auto_now_add=True)
  2726. dob_auto_now_add.set_attributes_from_name('dob_auto_now_add')
  2727. self.check_added_field_default(
  2728. editor, Author, dob_auto_now_add, 'dob_auto_now_add', now.date(),
  2729. cast_function=lambda x: x.date(),
  2730. )
  2731. dtob_auto_now = DateTimeField(auto_now=True)
  2732. dtob_auto_now.set_attributes_from_name('dtob_auto_now')
  2733. self.check_added_field_default(
  2734. editor, Author, dtob_auto_now, 'dtob_auto_now', now,
  2735. )
  2736. dt_tm_of_birth_auto_now_add = DateTimeField(auto_now_add=True)
  2737. dt_tm_of_birth_auto_now_add.set_attributes_from_name('dtob_auto_now_add')
  2738. self.check_added_field_default(
  2739. editor, Author, dt_tm_of_birth_auto_now_add, 'dtob_auto_now_add', now,
  2740. )
  2741. tob_auto_now = TimeField(auto_now=True)
  2742. tob_auto_now.set_attributes_from_name('tob_auto_now')
  2743. self.check_added_field_default(
  2744. editor, Author, tob_auto_now, 'tob_auto_now', now.time(),
  2745. cast_function=lambda x: x.time(),
  2746. )
  2747. tob_auto_now_add = TimeField(auto_now_add=True)
  2748. tob_auto_now_add.set_attributes_from_name('tob_auto_now_add')
  2749. self.check_added_field_default(
  2750. editor, Author, tob_auto_now_add, 'tob_auto_now_add', now.time(),
  2751. cast_function=lambda x: x.time(),
  2752. )
  2753. def test_namespaced_db_table_create_index_name(self):
  2754. """
  2755. Table names are stripped of their namespace/schema before being used to
  2756. generate index names.
  2757. """
  2758. with connection.schema_editor() as editor:
  2759. max_name_length = connection.ops.max_name_length() or 200
  2760. namespace = 'n' * max_name_length
  2761. table_name = 't' * max_name_length
  2762. namespaced_table_name = '"%s"."%s"' % (namespace, table_name)
  2763. self.assertEqual(
  2764. editor._create_index_name(table_name, []),
  2765. editor._create_index_name(namespaced_table_name, []),
  2766. )
  2767. @unittest.skipUnless(connection.vendor == 'oracle', 'Oracle specific db_table syntax')
  2768. def test_creation_with_db_table_double_quotes(self):
  2769. oracle_user = connection.creation._test_database_user()
  2770. class Student(Model):
  2771. name = CharField(max_length=30)
  2772. class Meta:
  2773. app_label = 'schema'
  2774. apps = new_apps
  2775. db_table = '"%s"."DJANGO_STUDENT_TABLE"' % oracle_user
  2776. class Document(Model):
  2777. name = CharField(max_length=30)
  2778. students = ManyToManyField(Student)
  2779. class Meta:
  2780. app_label = 'schema'
  2781. apps = new_apps
  2782. db_table = '"%s"."DJANGO_DOCUMENT_TABLE"' % oracle_user
  2783. self.local_models = [Student, Document]
  2784. with connection.schema_editor() as editor:
  2785. editor.create_model(Student)
  2786. editor.create_model(Document)
  2787. doc = Document.objects.create(name='Test Name')
  2788. student = Student.objects.create(name='Some man')
  2789. doc.students.add(student)
  2790. def test_rename_table_renames_deferred_sql_references(self):
  2791. atomic_rename = connection.features.supports_atomic_references_rename
  2792. with connection.schema_editor(atomic=atomic_rename) as editor:
  2793. editor.create_model(Author)
  2794. editor.create_model(Book)
  2795. editor.alter_db_table(Author, 'schema_author', 'schema_renamed_author')
  2796. editor.alter_db_table(Author, 'schema_book', 'schema_renamed_book')
  2797. self.assertGreater(len(editor.deferred_sql), 0)
  2798. for statement in editor.deferred_sql:
  2799. self.assertIs(statement.references_table('schema_author'), False)
  2800. self.assertIs(statement.references_table('schema_book'), False)
  2801. @unittest.skipIf(connection.vendor == 'sqlite', 'SQLite naively remakes the table on field alteration.')
  2802. def test_rename_column_renames_deferred_sql_references(self):
  2803. with connection.schema_editor() as editor:
  2804. editor.create_model(Author)
  2805. editor.create_model(Book)
  2806. old_title = Book._meta.get_field('title')
  2807. new_title = CharField(max_length=100, db_index=True)
  2808. new_title.set_attributes_from_name('renamed_title')
  2809. editor.alter_field(Book, old_title, new_title)
  2810. old_author = Book._meta.get_field('author')
  2811. new_author = ForeignKey(Author, CASCADE)
  2812. new_author.set_attributes_from_name('renamed_author')
  2813. editor.alter_field(Book, old_author, new_author)
  2814. self.assertGreater(len(editor.deferred_sql), 0)
  2815. for statement in editor.deferred_sql:
  2816. self.assertIs(statement.references_column('book', 'title'), False)
  2817. self.assertIs(statement.references_column('book', 'author_id'), False)
  2818. @isolate_apps('schema')
  2819. def test_referenced_field_without_constraint_rename_inside_atomic_block(self):
  2820. """
  2821. Foreign keys without database level constraint don't prevent the field
  2822. they reference from being renamed in an atomic block.
  2823. """
  2824. class Foo(Model):
  2825. field = CharField(max_length=255, unique=True)
  2826. class Meta:
  2827. app_label = 'schema'
  2828. class Bar(Model):
  2829. foo = ForeignKey(Foo, CASCADE, to_field='field', db_constraint=False)
  2830. class Meta:
  2831. app_label = 'schema'
  2832. self.isolated_local_models = [Foo, Bar]
  2833. with connection.schema_editor() as editor:
  2834. editor.create_model(Foo)
  2835. editor.create_model(Bar)
  2836. new_field = CharField(max_length=255, unique=True)
  2837. new_field.set_attributes_from_name('renamed')
  2838. with connection.schema_editor(atomic=True) as editor:
  2839. editor.alter_field(Foo, Foo._meta.get_field('field'), new_field)
  2840. @isolate_apps('schema')
  2841. def test_referenced_table_without_constraint_rename_inside_atomic_block(self):
  2842. """
  2843. Foreign keys without database level constraint don't prevent the table
  2844. they reference from being renamed in an atomic block.
  2845. """
  2846. class Foo(Model):
  2847. field = CharField(max_length=255, unique=True)
  2848. class Meta:
  2849. app_label = 'schema'
  2850. class Bar(Model):
  2851. foo = ForeignKey(Foo, CASCADE, to_field='field', db_constraint=False)
  2852. class Meta:
  2853. app_label = 'schema'
  2854. self.isolated_local_models = [Foo, Bar]
  2855. with connection.schema_editor() as editor:
  2856. editor.create_model(Foo)
  2857. editor.create_model(Bar)
  2858. new_field = CharField(max_length=255, unique=True)
  2859. new_field.set_attributes_from_name('renamed')
  2860. with connection.schema_editor(atomic=True) as editor:
  2861. editor.alter_db_table(Foo, Foo._meta.db_table, 'renamed_table')
  2862. Foo._meta.db_table = 'renamed_table'