tests.py 141 KB

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