2
0

tests.py 178 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054
  1. import datetime
  2. import itertools
  3. import unittest
  4. from copy import copy
  5. from unittest import mock
  6. from django.core.exceptions import FieldError
  7. from django.core.management.color import no_style
  8. from django.db import (
  9. DatabaseError, DataError, IntegrityError, OperationalError, connection,
  10. )
  11. from django.db.models import (
  12. CASCADE, PROTECT, AutoField, BigAutoField, BigIntegerField, BinaryField,
  13. BooleanField, CharField, CheckConstraint, DateField, DateTimeField,
  14. DecimalField, F, FloatField, ForeignKey, ForeignObject, Index,
  15. IntegerField, JSONField, ManyToManyField, Model, OneToOneField, OrderBy,
  16. PositiveIntegerField, Q, SlugField, SmallAutoField, SmallIntegerField,
  17. TextField, TimeField, UniqueConstraint, UUIDField, Value,
  18. )
  19. from django.db.models.fields.json import KeyTextTransform
  20. from django.db.models.functions import Abs, Cast, Collate, Lower, Random, Upper
  21. from django.db.models.indexes import IndexExpression
  22. from django.db.transaction import TransactionManagementError, atomic
  23. from django.test import (
  24. TransactionTestCase, skipIfDBFeature, skipUnlessDBFeature,
  25. )
  26. from django.test.utils import (
  27. CaptureQueriesContext, isolate_apps, register_lookup,
  28. )
  29. from django.utils import timezone
  30. from .fields import (
  31. CustomManyToManyField, InheritedManyToManyField, MediumBlobField,
  32. )
  33. from .models import (
  34. Author, AuthorCharFieldWithIndex, AuthorTextFieldWithIndex,
  35. AuthorWithDefaultHeight, AuthorWithEvenLongerName, AuthorWithIndexedName,
  36. AuthorWithIndexedNameAndBirthday, AuthorWithUniqueName,
  37. AuthorWithUniqueNameAndBirthday, Book, BookForeignObj, BookWeak,
  38. BookWithLongName, BookWithO2O, BookWithoutAuthor, BookWithSlug, IntegerPK,
  39. Node, Note, NoteRename, Tag, TagIndexed, TagM2MTest, TagUniqueRename,
  40. Thing, UniqueTest, new_apps,
  41. )
  42. class SchemaTests(TransactionTestCase):
  43. """
  44. Tests for the schema-alteration code.
  45. Be aware that these tests are more liable than most to false results,
  46. as sometimes the code to check if a test has worked is almost as complex
  47. as the code it is testing.
  48. """
  49. available_apps = []
  50. models = [
  51. Author, AuthorCharFieldWithIndex, AuthorTextFieldWithIndex,
  52. AuthorWithDefaultHeight, AuthorWithEvenLongerName, Book, BookWeak,
  53. BookWithLongName, BookWithO2O, BookWithSlug, IntegerPK, Node, Note,
  54. Tag, TagIndexed, TagM2MTest, TagUniqueRename, Thing, UniqueTest,
  55. ]
  56. # Utility functions
  57. def setUp(self):
  58. # local_models should contain test dependent model classes that will be
  59. # automatically removed from the app cache on test tear down.
  60. self.local_models = []
  61. # isolated_local_models contains models that are in test methods
  62. # decorated with @isolate_apps.
  63. self.isolated_local_models = []
  64. def tearDown(self):
  65. # Delete any tables made for our models
  66. self.delete_tables()
  67. new_apps.clear_cache()
  68. for model in new_apps.get_models():
  69. model._meta._expire_cache()
  70. if 'schema' in new_apps.all_models:
  71. for model in self.local_models:
  72. for many_to_many in model._meta.many_to_many:
  73. through = many_to_many.remote_field.through
  74. if through and through._meta.auto_created:
  75. del new_apps.all_models['schema'][through._meta.model_name]
  76. del new_apps.all_models['schema'][model._meta.model_name]
  77. if self.isolated_local_models:
  78. with connection.schema_editor() as editor:
  79. for model in self.isolated_local_models:
  80. editor.delete_model(model)
  81. def delete_tables(self):
  82. "Deletes all model tables for our models for a clean test environment"
  83. converter = connection.introspection.identifier_converter
  84. with connection.schema_editor() as editor:
  85. connection.disable_constraint_checking()
  86. table_names = connection.introspection.table_names()
  87. if connection.features.ignores_table_name_case:
  88. table_names = [table_name.lower() for table_name in table_names]
  89. for model in itertools.chain(SchemaTests.models, self.local_models):
  90. tbl = converter(model._meta.db_table)
  91. if connection.features.ignores_table_name_case:
  92. tbl = tbl.lower()
  93. if tbl in table_names:
  94. editor.delete_model(model)
  95. table_names.remove(tbl)
  96. connection.enable_constraint_checking()
  97. def column_classes(self, model):
  98. with connection.cursor() as cursor:
  99. columns = {
  100. d[0]: (connection.introspection.get_field_type(d[1], d), d)
  101. for d in connection.introspection.get_table_description(
  102. cursor,
  103. model._meta.db_table,
  104. )
  105. }
  106. # SQLite has a different format for field_type
  107. for name, (type, desc) in columns.items():
  108. if isinstance(type, tuple):
  109. columns[name] = (type[0], desc)
  110. # SQLite also doesn't error properly
  111. if not columns:
  112. raise DatabaseError("Table does not exist (empty pragma)")
  113. return columns
  114. def get_primary_key(self, table):
  115. with connection.cursor() as cursor:
  116. return connection.introspection.get_primary_key_column(cursor, table)
  117. def get_indexes(self, table):
  118. """
  119. Get the indexes on the table using a new cursor.
  120. """
  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['index'] and len(c['columns']) == 1
  126. ]
  127. def get_uniques(self, table):
  128. with connection.cursor() as cursor:
  129. return [
  130. c['columns'][0]
  131. for c in connection.introspection.get_constraints(cursor, table).values()
  132. if c['unique'] and len(c['columns']) == 1
  133. ]
  134. def get_constraints(self, table):
  135. """
  136. Get the constraints on a table using a new cursor.
  137. """
  138. with connection.cursor() as cursor:
  139. return connection.introspection.get_constraints(cursor, table)
  140. def get_constraints_for_column(self, model, column_name):
  141. constraints = self.get_constraints(model._meta.db_table)
  142. constraints_for_column = []
  143. for name, details in constraints.items():
  144. if details['columns'] == [column_name]:
  145. constraints_for_column.append(name)
  146. return sorted(constraints_for_column)
  147. def check_added_field_default(self, schema_editor, model, field, field_name, expected_default,
  148. cast_function=None):
  149. with connection.cursor() as cursor:
  150. schema_editor.add_field(model, field)
  151. cursor.execute("SELECT {} FROM {};".format(field_name, model._meta.db_table))
  152. database_default = cursor.fetchall()[0][0]
  153. if cast_function and type(database_default) != type(expected_default):
  154. database_default = cast_function(database_default)
  155. self.assertEqual(database_default, expected_default)
  156. def get_constraints_count(self, table, column, fk_to):
  157. """
  158. Return a dict with keys 'fks', 'uniques, and 'indexes' indicating the
  159. number of foreign keys, unique constraints, and indexes on
  160. `table`.`column`. The `fk_to` argument is a 2-tuple specifying the
  161. expected foreign key relationship's (table, column).
  162. """
  163. with connection.cursor() as cursor:
  164. constraints = connection.introspection.get_constraints(cursor, table)
  165. counts = {'fks': 0, 'uniques': 0, 'indexes': 0}
  166. for c in constraints.values():
  167. if c['columns'] == [column]:
  168. if c['foreign_key'] == fk_to:
  169. counts['fks'] += 1
  170. if c['unique']:
  171. counts['uniques'] += 1
  172. elif c['index']:
  173. counts['indexes'] += 1
  174. return counts
  175. def get_column_collation(self, table, column):
  176. with connection.cursor() as cursor:
  177. return next(
  178. f.collation
  179. for f in connection.introspection.get_table_description(cursor, table)
  180. if f.name == column
  181. )
  182. def assertIndexOrder(self, table, index, order):
  183. constraints = self.get_constraints(table)
  184. self.assertIn(index, constraints)
  185. index_orders = constraints[index]['orders']
  186. self.assertTrue(all(val == expected for val, expected in zip(index_orders, order)))
  187. def assertForeignKeyExists(self, model, column, expected_fk_table, field='id'):
  188. """
  189. Fail if the FK constraint on `model.Meta.db_table`.`column` to
  190. `expected_fk_table`.id doesn't exist.
  191. """
  192. constraints = self.get_constraints(model._meta.db_table)
  193. constraint_fk = None
  194. for details in constraints.values():
  195. if details['columns'] == [column] and details['foreign_key']:
  196. constraint_fk = details['foreign_key']
  197. break
  198. self.assertEqual(constraint_fk, (expected_fk_table, field))
  199. def assertForeignKeyNotExists(self, model, column, expected_fk_table):
  200. with self.assertRaises(AssertionError):
  201. self.assertForeignKeyExists(model, column, expected_fk_table)
  202. # Tests
  203. def test_creation_deletion(self):
  204. """
  205. Tries creating a model's table, and then deleting it.
  206. """
  207. with connection.schema_editor() as editor:
  208. # Create the table
  209. editor.create_model(Author)
  210. # The table is there
  211. list(Author.objects.all())
  212. # Clean up that table
  213. editor.delete_model(Author)
  214. # No deferred SQL should be left over.
  215. self.assertEqual(editor.deferred_sql, [])
  216. # The table is gone
  217. with self.assertRaises(DatabaseError):
  218. list(Author.objects.all())
  219. @skipUnlessDBFeature('supports_foreign_keys')
  220. def test_fk(self):
  221. "Creating tables out of FK order, then repointing, works"
  222. # Create the table
  223. with connection.schema_editor() as editor:
  224. editor.create_model(Book)
  225. editor.create_model(Author)
  226. editor.create_model(Tag)
  227. # Initial tables are there
  228. list(Author.objects.all())
  229. list(Book.objects.all())
  230. # Make sure the FK constraint is present
  231. with self.assertRaises(IntegrityError):
  232. Book.objects.create(
  233. author_id=1,
  234. title="Much Ado About Foreign Keys",
  235. pub_date=datetime.datetime.now(),
  236. )
  237. # Repoint the FK constraint
  238. old_field = Book._meta.get_field("author")
  239. new_field = ForeignKey(Tag, CASCADE)
  240. new_field.set_attributes_from_name("author")
  241. with connection.schema_editor() as editor:
  242. editor.alter_field(Book, old_field, new_field, strict=True)
  243. self.assertForeignKeyExists(Book, 'author_id', 'schema_tag')
  244. @skipUnlessDBFeature('can_create_inline_fk')
  245. def test_inline_fk(self):
  246. # Create some tables.
  247. with connection.schema_editor() as editor:
  248. editor.create_model(Author)
  249. editor.create_model(Book)
  250. editor.create_model(Note)
  251. self.assertForeignKeyNotExists(Note, 'book_id', 'schema_book')
  252. # Add a foreign key from one to the other.
  253. with connection.schema_editor() as editor:
  254. new_field = ForeignKey(Book, CASCADE)
  255. new_field.set_attributes_from_name('book')
  256. editor.add_field(Note, new_field)
  257. self.assertForeignKeyExists(Note, 'book_id', 'schema_book')
  258. # Creating a FK field with a constraint uses a single statement without
  259. # a deferred ALTER TABLE.
  260. self.assertFalse([
  261. sql for sql in (str(statement) for statement in editor.deferred_sql)
  262. if sql.startswith('ALTER TABLE') and 'ADD CONSTRAINT' in sql
  263. ])
  264. @skipUnlessDBFeature('can_create_inline_fk')
  265. def test_add_inline_fk_update_data(self):
  266. with connection.schema_editor() as editor:
  267. editor.create_model(Node)
  268. # Add an inline foreign key and update data in the same transaction.
  269. new_field = ForeignKey(Node, CASCADE, related_name='new_fk', null=True)
  270. new_field.set_attributes_from_name('new_parent_fk')
  271. parent = Node.objects.create()
  272. with connection.schema_editor() as editor:
  273. editor.add_field(Node, new_field)
  274. editor.execute('UPDATE schema_node SET new_parent_fk_id = %s;', [parent.pk])
  275. assertIndex = (
  276. self.assertIn
  277. if connection.features.indexes_foreign_keys
  278. else self.assertNotIn
  279. )
  280. assertIndex('new_parent_fk_id', self.get_indexes(Node._meta.db_table))
  281. @skipUnlessDBFeature(
  282. 'can_create_inline_fk',
  283. 'allows_multiple_constraints_on_same_fields',
  284. )
  285. @isolate_apps('schema')
  286. def test_add_inline_fk_index_update_data(self):
  287. class Node(Model):
  288. class Meta:
  289. app_label = 'schema'
  290. with connection.schema_editor() as editor:
  291. editor.create_model(Node)
  292. # Add an inline foreign key, update data, and an index in the same
  293. # transaction.
  294. new_field = ForeignKey(Node, CASCADE, related_name='new_fk', null=True)
  295. new_field.set_attributes_from_name('new_parent_fk')
  296. parent = Node.objects.create()
  297. with connection.schema_editor() as editor:
  298. editor.add_field(Node, new_field)
  299. Node._meta.add_field(new_field)
  300. editor.execute('UPDATE schema_node SET new_parent_fk_id = %s;', [parent.pk])
  301. editor.add_index(Node, Index(fields=['new_parent_fk'], name='new_parent_inline_fk_idx'))
  302. self.assertIn('new_parent_fk_id', self.get_indexes(Node._meta.db_table))
  303. @skipUnlessDBFeature('supports_foreign_keys')
  304. def test_char_field_with_db_index_to_fk(self):
  305. # Create the table
  306. with connection.schema_editor() as editor:
  307. editor.create_model(Author)
  308. editor.create_model(AuthorCharFieldWithIndex)
  309. # Change CharField to FK
  310. old_field = AuthorCharFieldWithIndex._meta.get_field('char_field')
  311. new_field = ForeignKey(Author, CASCADE, blank=True)
  312. new_field.set_attributes_from_name('char_field')
  313. with connection.schema_editor() as editor:
  314. editor.alter_field(AuthorCharFieldWithIndex, old_field, new_field, strict=True)
  315. self.assertForeignKeyExists(AuthorCharFieldWithIndex, 'char_field_id', 'schema_author')
  316. @skipUnlessDBFeature('supports_foreign_keys')
  317. @skipUnlessDBFeature('supports_index_on_text_field')
  318. def test_text_field_with_db_index_to_fk(self):
  319. # Create the table
  320. with connection.schema_editor() as editor:
  321. editor.create_model(Author)
  322. editor.create_model(AuthorTextFieldWithIndex)
  323. # Change TextField to FK
  324. old_field = AuthorTextFieldWithIndex._meta.get_field('text_field')
  325. new_field = ForeignKey(Author, CASCADE, blank=True)
  326. new_field.set_attributes_from_name('text_field')
  327. with connection.schema_editor() as editor:
  328. editor.alter_field(AuthorTextFieldWithIndex, old_field, new_field, strict=True)
  329. self.assertForeignKeyExists(AuthorTextFieldWithIndex, 'text_field_id', 'schema_author')
  330. @isolate_apps('schema')
  331. def test_char_field_pk_to_auto_field(self):
  332. class Foo(Model):
  333. id = CharField(max_length=255, primary_key=True)
  334. class Meta:
  335. app_label = 'schema'
  336. with connection.schema_editor() as editor:
  337. editor.create_model(Foo)
  338. self.isolated_local_models = [Foo]
  339. old_field = Foo._meta.get_field('id')
  340. new_field = AutoField(primary_key=True)
  341. new_field.set_attributes_from_name('id')
  342. new_field.model = Foo
  343. with connection.schema_editor() as editor:
  344. editor.alter_field(Foo, old_field, new_field, strict=True)
  345. @skipUnlessDBFeature('supports_foreign_keys')
  346. def test_fk_to_proxy(self):
  347. "Creating a FK to a proxy model creates database constraints."
  348. class AuthorProxy(Author):
  349. class Meta:
  350. app_label = 'schema'
  351. apps = new_apps
  352. proxy = True
  353. class AuthorRef(Model):
  354. author = ForeignKey(AuthorProxy, on_delete=CASCADE)
  355. class Meta:
  356. app_label = 'schema'
  357. apps = new_apps
  358. self.local_models = [AuthorProxy, AuthorRef]
  359. # Create the table
  360. with connection.schema_editor() as editor:
  361. editor.create_model(Author)
  362. editor.create_model(AuthorRef)
  363. self.assertForeignKeyExists(AuthorRef, 'author_id', 'schema_author')
  364. @skipUnlessDBFeature('supports_foreign_keys')
  365. def test_fk_db_constraint(self):
  366. "The db_constraint parameter is respected"
  367. # Create the table
  368. with connection.schema_editor() as editor:
  369. editor.create_model(Tag)
  370. editor.create_model(Author)
  371. editor.create_model(BookWeak)
  372. # Initial tables are there
  373. list(Author.objects.all())
  374. list(Tag.objects.all())
  375. list(BookWeak.objects.all())
  376. self.assertForeignKeyNotExists(BookWeak, 'author_id', 'schema_author')
  377. # Make a db_constraint=False FK
  378. new_field = ForeignKey(Tag, CASCADE, db_constraint=False)
  379. new_field.set_attributes_from_name("tag")
  380. with connection.schema_editor() as editor:
  381. editor.add_field(Author, new_field)
  382. self.assertForeignKeyNotExists(Author, 'tag_id', 'schema_tag')
  383. # Alter to one with a constraint
  384. new_field2 = ForeignKey(Tag, CASCADE)
  385. new_field2.set_attributes_from_name("tag")
  386. with connection.schema_editor() as editor:
  387. editor.alter_field(Author, new_field, new_field2, strict=True)
  388. self.assertForeignKeyExists(Author, 'tag_id', 'schema_tag')
  389. # Alter to one without a constraint again
  390. new_field2 = ForeignKey(Tag, CASCADE)
  391. new_field2.set_attributes_from_name("tag")
  392. with connection.schema_editor() as editor:
  393. editor.alter_field(Author, new_field2, new_field, strict=True)
  394. self.assertForeignKeyNotExists(Author, 'tag_id', 'schema_tag')
  395. @isolate_apps('schema')
  396. def test_no_db_constraint_added_during_primary_key_change(self):
  397. """
  398. When a primary key that's pointed to by a ForeignKey with
  399. db_constraint=False is altered, a foreign key constraint isn't added.
  400. """
  401. class Author(Model):
  402. class Meta:
  403. app_label = 'schema'
  404. class BookWeak(Model):
  405. author = ForeignKey(Author, CASCADE, db_constraint=False)
  406. class Meta:
  407. app_label = 'schema'
  408. with connection.schema_editor() as editor:
  409. editor.create_model(Author)
  410. editor.create_model(BookWeak)
  411. self.assertForeignKeyNotExists(BookWeak, 'author_id', 'schema_author')
  412. old_field = Author._meta.get_field('id')
  413. new_field = BigAutoField(primary_key=True)
  414. new_field.model = Author
  415. new_field.set_attributes_from_name('id')
  416. # @isolate_apps() and inner models are needed to have the model
  417. # relations populated, otherwise this doesn't act as a regression test.
  418. self.assertEqual(len(new_field.model._meta.related_objects), 1)
  419. with connection.schema_editor() as editor:
  420. editor.alter_field(Author, old_field, new_field, strict=True)
  421. self.assertForeignKeyNotExists(BookWeak, 'author_id', 'schema_author')
  422. def _test_m2m_db_constraint(self, M2MFieldClass):
  423. class LocalAuthorWithM2M(Model):
  424. name = CharField(max_length=255)
  425. class Meta:
  426. app_label = 'schema'
  427. apps = new_apps
  428. self.local_models = [LocalAuthorWithM2M]
  429. # Create the table
  430. with connection.schema_editor() as editor:
  431. editor.create_model(Tag)
  432. editor.create_model(LocalAuthorWithM2M)
  433. # Initial tables are there
  434. list(LocalAuthorWithM2M.objects.all())
  435. list(Tag.objects.all())
  436. # Make a db_constraint=False FK
  437. new_field = M2MFieldClass(Tag, related_name="authors", db_constraint=False)
  438. new_field.contribute_to_class(LocalAuthorWithM2M, "tags")
  439. # Add the field
  440. with connection.schema_editor() as editor:
  441. editor.add_field(LocalAuthorWithM2M, new_field)
  442. self.assertForeignKeyNotExists(new_field.remote_field.through, 'tag_id', 'schema_tag')
  443. @skipUnlessDBFeature('supports_foreign_keys')
  444. def test_m2m_db_constraint(self):
  445. self._test_m2m_db_constraint(ManyToManyField)
  446. @skipUnlessDBFeature('supports_foreign_keys')
  447. def test_m2m_db_constraint_custom(self):
  448. self._test_m2m_db_constraint(CustomManyToManyField)
  449. @skipUnlessDBFeature('supports_foreign_keys')
  450. def test_m2m_db_constraint_inherited(self):
  451. self._test_m2m_db_constraint(InheritedManyToManyField)
  452. def test_add_field(self):
  453. """
  454. Tests adding fields to models
  455. """
  456. # Create the table
  457. with connection.schema_editor() as editor:
  458. editor.create_model(Author)
  459. # Ensure there's no age field
  460. columns = self.column_classes(Author)
  461. self.assertNotIn("age", columns)
  462. # Add the new field
  463. new_field = IntegerField(null=True)
  464. new_field.set_attributes_from_name("age")
  465. with CaptureQueriesContext(connection) as ctx, connection.schema_editor() as editor:
  466. editor.add_field(Author, new_field)
  467. drop_default_sql = editor.sql_alter_column_no_default % {
  468. 'column': editor.quote_name(new_field.name),
  469. }
  470. self.assertFalse(any(drop_default_sql in query['sql'] for query in ctx.captured_queries))
  471. # Ensure the field is right afterwards
  472. columns = self.column_classes(Author)
  473. self.assertEqual(columns['age'][0], connection.features.introspected_field_types['IntegerField'])
  474. self.assertTrue(columns['age'][1][6])
  475. def test_add_field_remove_field(self):
  476. """
  477. Adding a field and removing it removes all deferred sql referring to it.
  478. """
  479. with connection.schema_editor() as editor:
  480. # Create a table with a unique constraint on the slug field.
  481. editor.create_model(Tag)
  482. # Remove the slug column.
  483. editor.remove_field(Tag, Tag._meta.get_field('slug'))
  484. self.assertEqual(editor.deferred_sql, [])
  485. def test_add_field_temp_default(self):
  486. """
  487. Tests adding fields to models with a temporary default
  488. """
  489. # Create the table
  490. with connection.schema_editor() as editor:
  491. editor.create_model(Author)
  492. # Ensure there's no age field
  493. columns = self.column_classes(Author)
  494. self.assertNotIn("age", columns)
  495. # Add some rows of data
  496. Author.objects.create(name="Andrew", height=30)
  497. Author.objects.create(name="Andrea")
  498. # Add a not-null field
  499. new_field = CharField(max_length=30, default="Godwin")
  500. new_field.set_attributes_from_name("surname")
  501. with connection.schema_editor() as editor:
  502. editor.add_field(Author, new_field)
  503. # Ensure the field is right afterwards
  504. columns = self.column_classes(Author)
  505. self.assertEqual(columns['surname'][0], connection.features.introspected_field_types['CharField'])
  506. self.assertEqual(columns['surname'][1][6],
  507. connection.features.interprets_empty_strings_as_nulls)
  508. def test_add_field_temp_default_boolean(self):
  509. """
  510. Tests adding fields to models with a temporary default where
  511. the default is False. (#21783)
  512. """
  513. # Create the table
  514. with connection.schema_editor() as editor:
  515. editor.create_model(Author)
  516. # Ensure there's no age field
  517. columns = self.column_classes(Author)
  518. self.assertNotIn("age", columns)
  519. # Add some rows of data
  520. Author.objects.create(name="Andrew", height=30)
  521. Author.objects.create(name="Andrea")
  522. # Add a not-null field
  523. new_field = BooleanField(default=False)
  524. new_field.set_attributes_from_name("awesome")
  525. with connection.schema_editor() as editor:
  526. editor.add_field(Author, new_field)
  527. # Ensure the field is right afterwards
  528. columns = self.column_classes(Author)
  529. # BooleanField are stored as TINYINT(1) on MySQL.
  530. field_type = columns['awesome'][0]
  531. self.assertEqual(field_type, connection.features.introspected_field_types['BooleanField'])
  532. def test_add_field_default_transform(self):
  533. """
  534. Tests adding fields to models with a default that is not directly
  535. valid in the database (#22581)
  536. """
  537. class TestTransformField(IntegerField):
  538. # Weird field that saves the count of items in its value
  539. def get_default(self):
  540. return self.default
  541. def get_prep_value(self, value):
  542. if value is None:
  543. return 0
  544. return len(value)
  545. # Create the table
  546. with connection.schema_editor() as editor:
  547. editor.create_model(Author)
  548. # Add some rows of data
  549. Author.objects.create(name="Andrew", height=30)
  550. Author.objects.create(name="Andrea")
  551. # Add the field with a default it needs to cast (to string in this case)
  552. new_field = TestTransformField(default={1: 2})
  553. new_field.set_attributes_from_name("thing")
  554. with connection.schema_editor() as editor:
  555. editor.add_field(Author, new_field)
  556. # Ensure the field is there
  557. columns = self.column_classes(Author)
  558. field_type, field_info = columns['thing']
  559. self.assertEqual(field_type, connection.features.introspected_field_types['IntegerField'])
  560. # Make sure the values were transformed correctly
  561. self.assertEqual(Author.objects.extra(where=["thing = 1"]).count(), 2)
  562. def test_add_field_binary(self):
  563. """
  564. Tests binary fields get a sane default (#22851)
  565. """
  566. # Create the table
  567. with connection.schema_editor() as editor:
  568. editor.create_model(Author)
  569. # Add the new field
  570. new_field = BinaryField(blank=True)
  571. new_field.set_attributes_from_name("bits")
  572. with connection.schema_editor() as editor:
  573. editor.add_field(Author, new_field)
  574. # Ensure the field is right afterwards
  575. columns = self.column_classes(Author)
  576. # MySQL annoyingly uses the same backend, so it'll come back as one of
  577. # these two types.
  578. self.assertIn(columns['bits'][0], ("BinaryField", "TextField"))
  579. @unittest.skipUnless(connection.vendor == 'mysql', "MySQL specific")
  580. def test_add_binaryfield_mediumblob(self):
  581. """
  582. Test adding a custom-sized binary field on MySQL (#24846).
  583. """
  584. # Create the table
  585. with connection.schema_editor() as editor:
  586. editor.create_model(Author)
  587. # Add the new field with default
  588. new_field = MediumBlobField(blank=True, default=b'123')
  589. new_field.set_attributes_from_name('bits')
  590. with connection.schema_editor() as editor:
  591. editor.add_field(Author, new_field)
  592. columns = self.column_classes(Author)
  593. # Introspection treats BLOBs as TextFields
  594. self.assertEqual(columns['bits'][0], "TextField")
  595. def test_alter(self):
  596. """
  597. Tests simple altering of fields
  598. """
  599. # Create the table
  600. with connection.schema_editor() as editor:
  601. editor.create_model(Author)
  602. # Ensure the field is right to begin with
  603. columns = self.column_classes(Author)
  604. self.assertEqual(columns['name'][0], connection.features.introspected_field_types['CharField'])
  605. self.assertEqual(bool(columns['name'][1][6]), bool(connection.features.interprets_empty_strings_as_nulls))
  606. # Alter the name field to a TextField
  607. old_field = Author._meta.get_field("name")
  608. new_field = TextField(null=True)
  609. new_field.set_attributes_from_name("name")
  610. with connection.schema_editor() as editor:
  611. editor.alter_field(Author, old_field, new_field, strict=True)
  612. # Ensure the field is right afterwards
  613. columns = self.column_classes(Author)
  614. self.assertEqual(columns['name'][0], "TextField")
  615. self.assertTrue(columns['name'][1][6])
  616. # Change nullability again
  617. new_field2 = TextField(null=False)
  618. new_field2.set_attributes_from_name("name")
  619. with connection.schema_editor() as editor:
  620. editor.alter_field(Author, new_field, new_field2, strict=True)
  621. # Ensure the field is right afterwards
  622. columns = self.column_classes(Author)
  623. self.assertEqual(columns['name'][0], "TextField")
  624. self.assertEqual(bool(columns['name'][1][6]), bool(connection.features.interprets_empty_strings_as_nulls))
  625. def test_alter_auto_field_to_integer_field(self):
  626. # Create the table
  627. with connection.schema_editor() as editor:
  628. editor.create_model(Author)
  629. # Change AutoField to IntegerField
  630. old_field = Author._meta.get_field('id')
  631. new_field = IntegerField(primary_key=True)
  632. new_field.set_attributes_from_name('id')
  633. new_field.model = Author
  634. with connection.schema_editor() as editor:
  635. editor.alter_field(Author, old_field, new_field, strict=True)
  636. # Now that ID is an IntegerField, the database raises an error if it
  637. # isn't provided.
  638. if not connection.features.supports_unspecified_pk:
  639. with self.assertRaises(DatabaseError):
  640. Author.objects.create()
  641. def test_alter_auto_field_to_char_field(self):
  642. # Create the table
  643. with connection.schema_editor() as editor:
  644. editor.create_model(Author)
  645. # Change AutoField to CharField
  646. old_field = Author._meta.get_field('id')
  647. new_field = CharField(primary_key=True, max_length=50)
  648. new_field.set_attributes_from_name('id')
  649. new_field.model = Author
  650. with connection.schema_editor() as editor:
  651. editor.alter_field(Author, old_field, new_field, strict=True)
  652. @isolate_apps('schema')
  653. def test_alter_auto_field_quoted_db_column(self):
  654. class Foo(Model):
  655. id = AutoField(primary_key=True, db_column='"quoted_id"')
  656. class Meta:
  657. app_label = 'schema'
  658. with connection.schema_editor() as editor:
  659. editor.create_model(Foo)
  660. self.isolated_local_models = [Foo]
  661. old_field = Foo._meta.get_field('id')
  662. new_field = BigAutoField(primary_key=True)
  663. new_field.model = Foo
  664. new_field.db_column = '"quoted_id"'
  665. new_field.set_attributes_from_name('id')
  666. with connection.schema_editor() as editor:
  667. editor.alter_field(Foo, old_field, new_field, strict=True)
  668. Foo.objects.create()
  669. def test_alter_not_unique_field_to_primary_key(self):
  670. # Create the table.
  671. with connection.schema_editor() as editor:
  672. editor.create_model(Author)
  673. # Change UUIDField to primary key.
  674. old_field = Author._meta.get_field('uuid')
  675. new_field = UUIDField(primary_key=True)
  676. new_field.set_attributes_from_name('uuid')
  677. new_field.model = Author
  678. with connection.schema_editor() as editor:
  679. editor.remove_field(Author, Author._meta.get_field('id'))
  680. editor.alter_field(Author, old_field, new_field, strict=True)
  681. @isolate_apps('schema')
  682. def test_alter_primary_key_quoted_db_table(self):
  683. class Foo(Model):
  684. class Meta:
  685. app_label = 'schema'
  686. db_table = '"foo"'
  687. with connection.schema_editor() as editor:
  688. editor.create_model(Foo)
  689. self.isolated_local_models = [Foo]
  690. old_field = Foo._meta.get_field('id')
  691. new_field = BigAutoField(primary_key=True)
  692. new_field.model = Foo
  693. new_field.set_attributes_from_name('id')
  694. with connection.schema_editor() as editor:
  695. editor.alter_field(Foo, old_field, new_field, strict=True)
  696. Foo.objects.create()
  697. def test_alter_text_field(self):
  698. # Regression for "BLOB/TEXT column 'info' can't have a default value")
  699. # on MySQL.
  700. # Create the table
  701. with connection.schema_editor() as editor:
  702. editor.create_model(Note)
  703. old_field = Note._meta.get_field("info")
  704. new_field = TextField(blank=True)
  705. new_field.set_attributes_from_name("info")
  706. with connection.schema_editor() as editor:
  707. editor.alter_field(Note, old_field, new_field, strict=True)
  708. @skipUnlessDBFeature('can_defer_constraint_checks', 'can_rollback_ddl')
  709. def test_alter_fk_checks_deferred_constraints(self):
  710. """
  711. #25492 - Altering a foreign key's structure and data in the same
  712. transaction.
  713. """
  714. with connection.schema_editor() as editor:
  715. editor.create_model(Node)
  716. old_field = Node._meta.get_field('parent')
  717. new_field = ForeignKey(Node, CASCADE)
  718. new_field.set_attributes_from_name('parent')
  719. parent = Node.objects.create()
  720. with connection.schema_editor() as editor:
  721. # Update the parent FK to create a deferred constraint check.
  722. Node.objects.update(parent=parent)
  723. editor.alter_field(Node, old_field, new_field, strict=True)
  724. def test_alter_text_field_to_date_field(self):
  725. """
  726. #25002 - Test conversion of text field to date field.
  727. """
  728. with connection.schema_editor() as editor:
  729. editor.create_model(Note)
  730. Note.objects.create(info='1988-05-05')
  731. old_field = Note._meta.get_field('info')
  732. new_field = DateField(blank=True)
  733. new_field.set_attributes_from_name('info')
  734. with connection.schema_editor() as editor:
  735. editor.alter_field(Note, old_field, new_field, strict=True)
  736. # Make sure the field isn't nullable
  737. columns = self.column_classes(Note)
  738. self.assertFalse(columns['info'][1][6])
  739. def test_alter_text_field_to_datetime_field(self):
  740. """
  741. #25002 - Test conversion of text field to datetime field.
  742. """
  743. with connection.schema_editor() as editor:
  744. editor.create_model(Note)
  745. Note.objects.create(info='1988-05-05 3:16:17.4567')
  746. old_field = Note._meta.get_field('info')
  747. new_field = DateTimeField(blank=True)
  748. new_field.set_attributes_from_name('info')
  749. with connection.schema_editor() as editor:
  750. editor.alter_field(Note, old_field, new_field, strict=True)
  751. # Make sure the field isn't nullable
  752. columns = self.column_classes(Note)
  753. self.assertFalse(columns['info'][1][6])
  754. def test_alter_text_field_to_time_field(self):
  755. """
  756. #25002 - Test conversion of text field to time field.
  757. """
  758. with connection.schema_editor() as editor:
  759. editor.create_model(Note)
  760. Note.objects.create(info='3:16:17.4567')
  761. old_field = Note._meta.get_field('info')
  762. new_field = TimeField(blank=True)
  763. new_field.set_attributes_from_name('info')
  764. with connection.schema_editor() as editor:
  765. editor.alter_field(Note, old_field, new_field, strict=True)
  766. # Make sure the field isn't nullable
  767. columns = self.column_classes(Note)
  768. self.assertFalse(columns['info'][1][6])
  769. @skipIfDBFeature('interprets_empty_strings_as_nulls')
  770. def test_alter_textual_field_keep_null_status(self):
  771. """
  772. Changing a field type shouldn't affect the not null status.
  773. """
  774. with connection.schema_editor() as editor:
  775. editor.create_model(Note)
  776. with self.assertRaises(IntegrityError):
  777. Note.objects.create(info=None)
  778. old_field = Note._meta.get_field("info")
  779. new_field = CharField(max_length=50)
  780. new_field.set_attributes_from_name("info")
  781. with connection.schema_editor() as editor:
  782. editor.alter_field(Note, old_field, new_field, strict=True)
  783. with self.assertRaises(IntegrityError):
  784. Note.objects.create(info=None)
  785. def test_alter_numeric_field_keep_null_status(self):
  786. """
  787. Changing a field type shouldn't affect the not null status.
  788. """
  789. with connection.schema_editor() as editor:
  790. editor.create_model(UniqueTest)
  791. with self.assertRaises(IntegrityError):
  792. UniqueTest.objects.create(year=None, slug='aaa')
  793. old_field = UniqueTest._meta.get_field("year")
  794. new_field = BigIntegerField()
  795. new_field.set_attributes_from_name("year")
  796. with connection.schema_editor() as editor:
  797. editor.alter_field(UniqueTest, old_field, new_field, strict=True)
  798. with self.assertRaises(IntegrityError):
  799. UniqueTest.objects.create(year=None, slug='bbb')
  800. def test_alter_null_to_not_null(self):
  801. """
  802. #23609 - Tests handling of default values when altering from NULL to NOT NULL.
  803. """
  804. # Create the table
  805. with connection.schema_editor() as editor:
  806. editor.create_model(Author)
  807. # Ensure the field is right to begin with
  808. columns = self.column_classes(Author)
  809. self.assertTrue(columns['height'][1][6])
  810. # Create some test data
  811. Author.objects.create(name='Not null author', height=12)
  812. Author.objects.create(name='Null author')
  813. # Verify null value
  814. self.assertEqual(Author.objects.get(name='Not null author').height, 12)
  815. self.assertIsNone(Author.objects.get(name='Null author').height)
  816. # Alter the height field to NOT NULL with default
  817. old_field = Author._meta.get_field("height")
  818. new_field = PositiveIntegerField(default=42)
  819. new_field.set_attributes_from_name("height")
  820. with connection.schema_editor() as editor:
  821. editor.alter_field(Author, old_field, new_field, strict=True)
  822. # Ensure the field is right afterwards
  823. columns = self.column_classes(Author)
  824. self.assertFalse(columns['height'][1][6])
  825. # Verify default value
  826. self.assertEqual(Author.objects.get(name='Not null author').height, 12)
  827. self.assertEqual(Author.objects.get(name='Null author').height, 42)
  828. def test_alter_charfield_to_null(self):
  829. """
  830. #24307 - Should skip an alter statement on databases with
  831. interprets_empty_strings_as_null when changing a CharField to null.
  832. """
  833. # Create the table
  834. with connection.schema_editor() as editor:
  835. editor.create_model(Author)
  836. # Change the CharField to null
  837. old_field = Author._meta.get_field('name')
  838. new_field = copy(old_field)
  839. new_field.null = True
  840. with connection.schema_editor() as editor:
  841. editor.alter_field(Author, old_field, new_field, strict=True)
  842. @unittest.skipUnless(connection.vendor == 'postgresql', 'PostgreSQL specific')
  843. def test_alter_char_field_decrease_length(self):
  844. # Create the table.
  845. with connection.schema_editor() as editor:
  846. editor.create_model(Author)
  847. Author.objects.create(name='x' * 255)
  848. # Change max_length of CharField.
  849. old_field = Author._meta.get_field('name')
  850. new_field = CharField(max_length=254)
  851. new_field.set_attributes_from_name('name')
  852. with connection.schema_editor() as editor:
  853. msg = 'value too long for type character varying(254)'
  854. with self.assertRaisesMessage(DataError, msg):
  855. editor.alter_field(Author, old_field, new_field, strict=True)
  856. @unittest.skipUnless(connection.vendor == 'postgresql', 'PostgreSQL specific')
  857. def test_alter_field_with_custom_db_type(self):
  858. from django.contrib.postgres.fields import ArrayField
  859. class Foo(Model):
  860. field = ArrayField(CharField(max_length=255))
  861. class Meta:
  862. app_label = 'schema'
  863. with connection.schema_editor() as editor:
  864. editor.create_model(Foo)
  865. self.isolated_local_models = [Foo]
  866. old_field = Foo._meta.get_field('field')
  867. new_field = ArrayField(CharField(max_length=16))
  868. new_field.set_attributes_from_name('field')
  869. new_field.model = Foo
  870. with connection.schema_editor() as editor:
  871. editor.alter_field(Foo, old_field, new_field, strict=True)
  872. @isolate_apps('schema')
  873. @unittest.skipUnless(connection.vendor == 'postgresql', 'PostgreSQL specific')
  874. def test_alter_array_field_decrease_base_field_length(self):
  875. from django.contrib.postgres.fields import ArrayField
  876. class ArrayModel(Model):
  877. field = ArrayField(CharField(max_length=16))
  878. class Meta:
  879. app_label = 'schema'
  880. with connection.schema_editor() as editor:
  881. editor.create_model(ArrayModel)
  882. self.isolated_local_models = [ArrayModel]
  883. ArrayModel.objects.create(field=['x' * 16])
  884. old_field = ArrayModel._meta.get_field('field')
  885. new_field = ArrayField(CharField(max_length=15))
  886. new_field.set_attributes_from_name('field')
  887. new_field.model = ArrayModel
  888. with connection.schema_editor() as editor:
  889. msg = 'value too long for type character varying(15)'
  890. with self.assertRaisesMessage(DataError, msg):
  891. editor.alter_field(ArrayModel, old_field, new_field, strict=True)
  892. @isolate_apps('schema')
  893. @unittest.skipUnless(connection.vendor == 'postgresql', 'PostgreSQL specific')
  894. def test_alter_array_field_decrease_nested_base_field_length(self):
  895. from django.contrib.postgres.fields import ArrayField
  896. class ArrayModel(Model):
  897. field = ArrayField(ArrayField(CharField(max_length=16)))
  898. class Meta:
  899. app_label = 'schema'
  900. with connection.schema_editor() as editor:
  901. editor.create_model(ArrayModel)
  902. self.isolated_local_models = [ArrayModel]
  903. ArrayModel.objects.create(field=[['x' * 16]])
  904. old_field = ArrayModel._meta.get_field('field')
  905. new_field = ArrayField(ArrayField(CharField(max_length=15)))
  906. new_field.set_attributes_from_name('field')
  907. new_field.model = ArrayModel
  908. with connection.schema_editor() as editor:
  909. msg = 'value too long for type character varying(15)'
  910. with self.assertRaisesMessage(DataError, msg):
  911. editor.alter_field(ArrayModel, old_field, new_field, strict=True)
  912. def test_alter_textfield_to_null(self):
  913. """
  914. #24307 - Should skip an alter statement on databases with
  915. interprets_empty_strings_as_null when changing a TextField to null.
  916. """
  917. # Create the table
  918. with connection.schema_editor() as editor:
  919. editor.create_model(Note)
  920. # Change the TextField to null
  921. old_field = Note._meta.get_field('info')
  922. new_field = copy(old_field)
  923. new_field.null = True
  924. with connection.schema_editor() as editor:
  925. editor.alter_field(Note, old_field, new_field, strict=True)
  926. @skipUnlessDBFeature('supports_combined_alters')
  927. def test_alter_null_to_not_null_keeping_default(self):
  928. """
  929. #23738 - Can change a nullable field with default to non-nullable
  930. with the same default.
  931. """
  932. # Create the table
  933. with connection.schema_editor() as editor:
  934. editor.create_model(AuthorWithDefaultHeight)
  935. # Ensure the field is right to begin with
  936. columns = self.column_classes(AuthorWithDefaultHeight)
  937. self.assertTrue(columns['height'][1][6])
  938. # Alter the height field to NOT NULL keeping the previous default
  939. old_field = AuthorWithDefaultHeight._meta.get_field("height")
  940. new_field = PositiveIntegerField(default=42)
  941. new_field.set_attributes_from_name("height")
  942. with connection.schema_editor() as editor:
  943. editor.alter_field(AuthorWithDefaultHeight, old_field, new_field, strict=True)
  944. # Ensure the field is right afterwards
  945. columns = self.column_classes(AuthorWithDefaultHeight)
  946. self.assertFalse(columns['height'][1][6])
  947. @skipUnlessDBFeature('supports_foreign_keys')
  948. def test_alter_fk(self):
  949. """
  950. Tests altering of FKs
  951. """
  952. # Create the table
  953. with connection.schema_editor() as editor:
  954. editor.create_model(Author)
  955. editor.create_model(Book)
  956. # Ensure the field is right to begin with
  957. columns = self.column_classes(Book)
  958. self.assertEqual(columns['author_id'][0], connection.features.introspected_field_types['IntegerField'])
  959. self.assertForeignKeyExists(Book, 'author_id', 'schema_author')
  960. # Alter the FK
  961. old_field = Book._meta.get_field("author")
  962. new_field = ForeignKey(Author, CASCADE, editable=False)
  963. new_field.set_attributes_from_name("author")
  964. with connection.schema_editor() as editor:
  965. editor.alter_field(Book, old_field, new_field, strict=True)
  966. # Ensure the field is right afterwards
  967. columns = self.column_classes(Book)
  968. self.assertEqual(columns['author_id'][0], connection.features.introspected_field_types['IntegerField'])
  969. self.assertForeignKeyExists(Book, 'author_id', 'schema_author')
  970. @skipUnlessDBFeature('supports_foreign_keys')
  971. def test_alter_to_fk(self):
  972. """
  973. #24447 - Tests adding a FK constraint for an existing column
  974. """
  975. class LocalBook(Model):
  976. author = IntegerField()
  977. title = CharField(max_length=100, db_index=True)
  978. pub_date = DateTimeField()
  979. class Meta:
  980. app_label = 'schema'
  981. apps = new_apps
  982. self.local_models = [LocalBook]
  983. # Create the tables
  984. with connection.schema_editor() as editor:
  985. editor.create_model(Author)
  986. editor.create_model(LocalBook)
  987. # Ensure no FK constraint exists
  988. constraints = self.get_constraints(LocalBook._meta.db_table)
  989. for details in constraints.values():
  990. if details['foreign_key']:
  991. self.fail('Found an unexpected FK constraint to %s' % details['columns'])
  992. old_field = LocalBook._meta.get_field("author")
  993. new_field = ForeignKey(Author, CASCADE)
  994. new_field.set_attributes_from_name("author")
  995. with connection.schema_editor() as editor:
  996. editor.alter_field(LocalBook, old_field, new_field, strict=True)
  997. self.assertForeignKeyExists(LocalBook, 'author_id', 'schema_author')
  998. @skipUnlessDBFeature('supports_foreign_keys')
  999. def test_alter_o2o_to_fk(self):
  1000. """
  1001. #24163 - Tests altering of OneToOneField to ForeignKey
  1002. """
  1003. # Create the table
  1004. with connection.schema_editor() as editor:
  1005. editor.create_model(Author)
  1006. editor.create_model(BookWithO2O)
  1007. # Ensure the field is right to begin with
  1008. columns = self.column_classes(BookWithO2O)
  1009. self.assertEqual(columns['author_id'][0], connection.features.introspected_field_types['IntegerField'])
  1010. # Ensure the field is unique
  1011. author = Author.objects.create(name="Joe")
  1012. BookWithO2O.objects.create(author=author, title="Django 1", pub_date=datetime.datetime.now())
  1013. with self.assertRaises(IntegrityError):
  1014. BookWithO2O.objects.create(author=author, title="Django 2", pub_date=datetime.datetime.now())
  1015. BookWithO2O.objects.all().delete()
  1016. self.assertForeignKeyExists(BookWithO2O, 'author_id', 'schema_author')
  1017. # Alter the OneToOneField to ForeignKey
  1018. old_field = BookWithO2O._meta.get_field("author")
  1019. new_field = ForeignKey(Author, CASCADE)
  1020. new_field.set_attributes_from_name("author")
  1021. with connection.schema_editor() as editor:
  1022. editor.alter_field(BookWithO2O, old_field, new_field, strict=True)
  1023. # Ensure the field is right afterwards
  1024. columns = self.column_classes(Book)
  1025. self.assertEqual(columns['author_id'][0], connection.features.introspected_field_types['IntegerField'])
  1026. # Ensure the field is not unique anymore
  1027. Book.objects.create(author=author, title="Django 1", pub_date=datetime.datetime.now())
  1028. Book.objects.create(author=author, title="Django 2", pub_date=datetime.datetime.now())
  1029. self.assertForeignKeyExists(Book, 'author_id', 'schema_author')
  1030. @skipUnlessDBFeature('supports_foreign_keys')
  1031. def test_alter_fk_to_o2o(self):
  1032. """
  1033. #24163 - Tests altering of ForeignKey to OneToOneField
  1034. """
  1035. # Create the table
  1036. with connection.schema_editor() as editor:
  1037. editor.create_model(Author)
  1038. editor.create_model(Book)
  1039. # Ensure the field is right to begin with
  1040. columns = self.column_classes(Book)
  1041. self.assertEqual(columns['author_id'][0], connection.features.introspected_field_types['IntegerField'])
  1042. # Ensure the field is not unique
  1043. author = Author.objects.create(name="Joe")
  1044. Book.objects.create(author=author, title="Django 1", pub_date=datetime.datetime.now())
  1045. Book.objects.create(author=author, title="Django 2", pub_date=datetime.datetime.now())
  1046. Book.objects.all().delete()
  1047. self.assertForeignKeyExists(Book, 'author_id', 'schema_author')
  1048. # Alter the ForeignKey to OneToOneField
  1049. old_field = Book._meta.get_field("author")
  1050. new_field = OneToOneField(Author, CASCADE)
  1051. new_field.set_attributes_from_name("author")
  1052. with connection.schema_editor() as editor:
  1053. editor.alter_field(Book, old_field, new_field, strict=True)
  1054. # Ensure the field is right afterwards
  1055. columns = self.column_classes(BookWithO2O)
  1056. self.assertEqual(columns['author_id'][0], connection.features.introspected_field_types['IntegerField'])
  1057. # Ensure the field is unique now
  1058. BookWithO2O.objects.create(author=author, title="Django 1", pub_date=datetime.datetime.now())
  1059. with self.assertRaises(IntegrityError):
  1060. BookWithO2O.objects.create(author=author, title="Django 2", pub_date=datetime.datetime.now())
  1061. self.assertForeignKeyExists(BookWithO2O, 'author_id', 'schema_author')
  1062. def test_alter_field_fk_to_o2o(self):
  1063. with connection.schema_editor() as editor:
  1064. editor.create_model(Author)
  1065. editor.create_model(Book)
  1066. expected_fks = 1 if connection.features.supports_foreign_keys else 0
  1067. expected_indexes = 1 if connection.features.indexes_foreign_keys else 0
  1068. # Check the index is right to begin with.
  1069. counts = self.get_constraints_count(
  1070. Book._meta.db_table,
  1071. Book._meta.get_field('author').column,
  1072. (Author._meta.db_table, Author._meta.pk.column),
  1073. )
  1074. self.assertEqual(
  1075. counts,
  1076. {'fks': expected_fks, 'uniques': 0, 'indexes': expected_indexes},
  1077. )
  1078. old_field = Book._meta.get_field('author')
  1079. new_field = OneToOneField(Author, CASCADE)
  1080. new_field.set_attributes_from_name('author')
  1081. with connection.schema_editor() as editor:
  1082. editor.alter_field(Book, old_field, new_field, strict=True)
  1083. counts = self.get_constraints_count(
  1084. Book._meta.db_table,
  1085. Book._meta.get_field('author').column,
  1086. (Author._meta.db_table, Author._meta.pk.column),
  1087. )
  1088. # The index on ForeignKey is replaced with a unique constraint for OneToOneField.
  1089. self.assertEqual(counts, {'fks': expected_fks, 'uniques': 1, 'indexes': 0})
  1090. def test_alter_field_fk_keeps_index(self):
  1091. with connection.schema_editor() as editor:
  1092. editor.create_model(Author)
  1093. editor.create_model(Book)
  1094. expected_fks = 1 if connection.features.supports_foreign_keys else 0
  1095. expected_indexes = 1 if connection.features.indexes_foreign_keys else 0
  1096. # Check the index is right to begin with.
  1097. counts = self.get_constraints_count(
  1098. Book._meta.db_table,
  1099. Book._meta.get_field('author').column,
  1100. (Author._meta.db_table, Author._meta.pk.column),
  1101. )
  1102. self.assertEqual(
  1103. counts,
  1104. {'fks': expected_fks, 'uniques': 0, 'indexes': expected_indexes},
  1105. )
  1106. old_field = Book._meta.get_field('author')
  1107. # on_delete changed from CASCADE.
  1108. new_field = ForeignKey(Author, PROTECT)
  1109. new_field.set_attributes_from_name('author')
  1110. with connection.schema_editor() as editor:
  1111. editor.alter_field(Book, old_field, new_field, strict=True)
  1112. counts = self.get_constraints_count(
  1113. Book._meta.db_table,
  1114. Book._meta.get_field('author').column,
  1115. (Author._meta.db_table, Author._meta.pk.column),
  1116. )
  1117. # The index remains.
  1118. self.assertEqual(
  1119. counts,
  1120. {'fks': expected_fks, 'uniques': 0, 'indexes': expected_indexes},
  1121. )
  1122. def test_alter_field_o2o_to_fk(self):
  1123. with connection.schema_editor() as editor:
  1124. editor.create_model(Author)
  1125. editor.create_model(BookWithO2O)
  1126. expected_fks = 1 if connection.features.supports_foreign_keys else 0
  1127. # Check the unique constraint is right to begin with.
  1128. counts = self.get_constraints_count(
  1129. BookWithO2O._meta.db_table,
  1130. BookWithO2O._meta.get_field('author').column,
  1131. (Author._meta.db_table, Author._meta.pk.column),
  1132. )
  1133. self.assertEqual(counts, {'fks': expected_fks, 'uniques': 1, 'indexes': 0})
  1134. old_field = BookWithO2O._meta.get_field('author')
  1135. new_field = ForeignKey(Author, CASCADE)
  1136. new_field.set_attributes_from_name('author')
  1137. with connection.schema_editor() as editor:
  1138. editor.alter_field(BookWithO2O, old_field, new_field, strict=True)
  1139. counts = self.get_constraints_count(
  1140. BookWithO2O._meta.db_table,
  1141. BookWithO2O._meta.get_field('author').column,
  1142. (Author._meta.db_table, Author._meta.pk.column),
  1143. )
  1144. # The unique constraint on OneToOneField is replaced with an index for ForeignKey.
  1145. self.assertEqual(counts, {'fks': expected_fks, 'uniques': 0, 'indexes': 1})
  1146. def test_alter_field_o2o_keeps_unique(self):
  1147. with connection.schema_editor() as editor:
  1148. editor.create_model(Author)
  1149. editor.create_model(BookWithO2O)
  1150. expected_fks = 1 if connection.features.supports_foreign_keys else 0
  1151. # Check the unique constraint is right to begin with.
  1152. counts = self.get_constraints_count(
  1153. BookWithO2O._meta.db_table,
  1154. BookWithO2O._meta.get_field('author').column,
  1155. (Author._meta.db_table, Author._meta.pk.column),
  1156. )
  1157. self.assertEqual(counts, {'fks': expected_fks, 'uniques': 1, 'indexes': 0})
  1158. old_field = BookWithO2O._meta.get_field('author')
  1159. # on_delete changed from CASCADE.
  1160. new_field = OneToOneField(Author, PROTECT)
  1161. new_field.set_attributes_from_name('author')
  1162. with connection.schema_editor() as editor:
  1163. editor.alter_field(BookWithO2O, old_field, new_field, strict=True)
  1164. counts = self.get_constraints_count(
  1165. BookWithO2O._meta.db_table,
  1166. BookWithO2O._meta.get_field('author').column,
  1167. (Author._meta.db_table, Author._meta.pk.column),
  1168. )
  1169. # The unique constraint remains.
  1170. self.assertEqual(counts, {'fks': expected_fks, 'uniques': 1, 'indexes': 0})
  1171. @skipUnlessDBFeature('ignores_table_name_case')
  1172. def test_alter_db_table_case(self):
  1173. # Create the table
  1174. with connection.schema_editor() as editor:
  1175. editor.create_model(Author)
  1176. # Alter the case of the table
  1177. old_table_name = Author._meta.db_table
  1178. with connection.schema_editor() as editor:
  1179. editor.alter_db_table(Author, old_table_name, old_table_name.upper())
  1180. def test_alter_implicit_id_to_explicit(self):
  1181. """
  1182. Should be able to convert an implicit "id" field to an explicit "id"
  1183. primary key field.
  1184. """
  1185. with connection.schema_editor() as editor:
  1186. editor.create_model(Author)
  1187. old_field = Author._meta.get_field("id")
  1188. new_field = AutoField(primary_key=True)
  1189. new_field.set_attributes_from_name("id")
  1190. new_field.model = Author
  1191. with connection.schema_editor() as editor:
  1192. editor.alter_field(Author, old_field, new_field, strict=True)
  1193. # This will fail if DROP DEFAULT is inadvertently executed on this
  1194. # field which drops the id sequence, at least on PostgreSQL.
  1195. Author.objects.create(name='Foo')
  1196. Author.objects.create(name='Bar')
  1197. def test_alter_autofield_pk_to_bigautofield_pk_sequence_owner(self):
  1198. """
  1199. Converting an implicit PK to BigAutoField(primary_key=True) should keep
  1200. a sequence owner on PostgreSQL.
  1201. """
  1202. with connection.schema_editor() as editor:
  1203. editor.create_model(Author)
  1204. old_field = Author._meta.get_field('id')
  1205. new_field = BigAutoField(primary_key=True)
  1206. new_field.set_attributes_from_name('id')
  1207. new_field.model = Author
  1208. with connection.schema_editor() as editor:
  1209. editor.alter_field(Author, old_field, new_field, strict=True)
  1210. Author.objects.create(name='Foo', pk=1)
  1211. with connection.cursor() as cursor:
  1212. sequence_reset_sqls = connection.ops.sequence_reset_sql(no_style(), [Author])
  1213. if sequence_reset_sqls:
  1214. cursor.execute(sequence_reset_sqls[0])
  1215. # Fail on PostgreSQL if sequence is missing an owner.
  1216. self.assertIsNotNone(Author.objects.create(name='Bar'))
  1217. def test_alter_autofield_pk_to_smallautofield_pk_sequence_owner(self):
  1218. """
  1219. Converting an implicit PK to SmallAutoField(primary_key=True) should
  1220. keep a sequence owner on PostgreSQL.
  1221. """
  1222. with connection.schema_editor() as editor:
  1223. editor.create_model(Author)
  1224. old_field = Author._meta.get_field('id')
  1225. new_field = SmallAutoField(primary_key=True)
  1226. new_field.set_attributes_from_name('id')
  1227. new_field.model = Author
  1228. with connection.schema_editor() as editor:
  1229. editor.alter_field(Author, old_field, new_field, strict=True)
  1230. Author.objects.create(name='Foo', pk=1)
  1231. with connection.cursor() as cursor:
  1232. sequence_reset_sqls = connection.ops.sequence_reset_sql(no_style(), [Author])
  1233. if sequence_reset_sqls:
  1234. cursor.execute(sequence_reset_sqls[0])
  1235. # Fail on PostgreSQL if sequence is missing an owner.
  1236. self.assertIsNotNone(Author.objects.create(name='Bar'))
  1237. def test_alter_int_pk_to_autofield_pk(self):
  1238. """
  1239. Should be able to rename an IntegerField(primary_key=True) to
  1240. AutoField(primary_key=True).
  1241. """
  1242. with connection.schema_editor() as editor:
  1243. editor.create_model(IntegerPK)
  1244. old_field = IntegerPK._meta.get_field('i')
  1245. new_field = AutoField(primary_key=True)
  1246. new_field.model = IntegerPK
  1247. new_field.set_attributes_from_name('i')
  1248. with connection.schema_editor() as editor:
  1249. editor.alter_field(IntegerPK, old_field, new_field, strict=True)
  1250. # A model representing the updated model.
  1251. class IntegerPKToAutoField(Model):
  1252. i = AutoField(primary_key=True)
  1253. j = IntegerField(unique=True)
  1254. class Meta:
  1255. app_label = 'schema'
  1256. apps = new_apps
  1257. db_table = IntegerPK._meta.db_table
  1258. # An id (i) is generated by the database.
  1259. obj = IntegerPKToAutoField.objects.create(j=1)
  1260. self.assertIsNotNone(obj.i)
  1261. def test_alter_int_pk_to_bigautofield_pk(self):
  1262. """
  1263. Should be able to rename an IntegerField(primary_key=True) to
  1264. BigAutoField(primary_key=True).
  1265. """
  1266. with connection.schema_editor() as editor:
  1267. editor.create_model(IntegerPK)
  1268. old_field = IntegerPK._meta.get_field('i')
  1269. new_field = BigAutoField(primary_key=True)
  1270. new_field.model = IntegerPK
  1271. new_field.set_attributes_from_name('i')
  1272. with connection.schema_editor() as editor:
  1273. editor.alter_field(IntegerPK, old_field, new_field, strict=True)
  1274. # A model representing the updated model.
  1275. class IntegerPKToBigAutoField(Model):
  1276. i = BigAutoField(primary_key=True)
  1277. j = IntegerField(unique=True)
  1278. class Meta:
  1279. app_label = 'schema'
  1280. apps = new_apps
  1281. db_table = IntegerPK._meta.db_table
  1282. # An id (i) is generated by the database.
  1283. obj = IntegerPKToBigAutoField.objects.create(j=1)
  1284. self.assertIsNotNone(obj.i)
  1285. @isolate_apps('schema')
  1286. def test_alter_smallint_pk_to_smallautofield_pk(self):
  1287. """
  1288. Should be able to rename an SmallIntegerField(primary_key=True) to
  1289. SmallAutoField(primary_key=True).
  1290. """
  1291. class SmallIntegerPK(Model):
  1292. i = SmallIntegerField(primary_key=True)
  1293. class Meta:
  1294. app_label = 'schema'
  1295. with connection.schema_editor() as editor:
  1296. editor.create_model(SmallIntegerPK)
  1297. self.isolated_local_models = [SmallIntegerPK]
  1298. old_field = SmallIntegerPK._meta.get_field('i')
  1299. new_field = SmallAutoField(primary_key=True)
  1300. new_field.model = SmallIntegerPK
  1301. new_field.set_attributes_from_name('i')
  1302. with connection.schema_editor() as editor:
  1303. editor.alter_field(SmallIntegerPK, old_field, new_field, strict=True)
  1304. def test_alter_int_pk_to_int_unique(self):
  1305. """
  1306. Should be able to rename an IntegerField(primary_key=True) to
  1307. IntegerField(unique=True).
  1308. """
  1309. with connection.schema_editor() as editor:
  1310. editor.create_model(IntegerPK)
  1311. # Delete the old PK
  1312. old_field = IntegerPK._meta.get_field('i')
  1313. new_field = IntegerField(unique=True)
  1314. new_field.model = IntegerPK
  1315. new_field.set_attributes_from_name('i')
  1316. with connection.schema_editor() as editor:
  1317. editor.alter_field(IntegerPK, old_field, new_field, strict=True)
  1318. # The primary key constraint is gone. Result depends on database:
  1319. # 'id' for SQLite, None for others (must not be 'i').
  1320. self.assertIn(self.get_primary_key(IntegerPK._meta.db_table), ('id', None))
  1321. # Set up a model class as it currently stands. The original IntegerPK
  1322. # class is now out of date and some backends make use of the whole
  1323. # model class when modifying a field (such as sqlite3 when remaking a
  1324. # table) so an outdated model class leads to incorrect results.
  1325. class Transitional(Model):
  1326. i = IntegerField(unique=True)
  1327. j = IntegerField(unique=True)
  1328. class Meta:
  1329. app_label = 'schema'
  1330. apps = new_apps
  1331. db_table = 'INTEGERPK'
  1332. # model requires a new PK
  1333. old_field = Transitional._meta.get_field('j')
  1334. new_field = IntegerField(primary_key=True)
  1335. new_field.model = Transitional
  1336. new_field.set_attributes_from_name('j')
  1337. with connection.schema_editor() as editor:
  1338. editor.alter_field(Transitional, old_field, new_field, strict=True)
  1339. # Create a model class representing the updated model.
  1340. class IntegerUnique(Model):
  1341. i = IntegerField(unique=True)
  1342. j = IntegerField(primary_key=True)
  1343. class Meta:
  1344. app_label = 'schema'
  1345. apps = new_apps
  1346. db_table = 'INTEGERPK'
  1347. # Ensure unique constraint works.
  1348. IntegerUnique.objects.create(i=1, j=1)
  1349. with self.assertRaises(IntegrityError):
  1350. IntegerUnique.objects.create(i=1, j=2)
  1351. def test_rename(self):
  1352. """
  1353. Tests simple altering of fields
  1354. """
  1355. # Create the table
  1356. with connection.schema_editor() as editor:
  1357. editor.create_model(Author)
  1358. # Ensure the field is right to begin with
  1359. columns = self.column_classes(Author)
  1360. self.assertEqual(columns['name'][0], connection.features.introspected_field_types['CharField'])
  1361. self.assertNotIn("display_name", columns)
  1362. # Alter the name field's name
  1363. old_field = Author._meta.get_field("name")
  1364. new_field = CharField(max_length=254)
  1365. new_field.set_attributes_from_name("display_name")
  1366. with connection.schema_editor() as editor:
  1367. editor.alter_field(Author, old_field, new_field, strict=True)
  1368. # Ensure the field is right afterwards
  1369. columns = self.column_classes(Author)
  1370. self.assertEqual(columns['display_name'][0], connection.features.introspected_field_types['CharField'])
  1371. self.assertNotIn("name", columns)
  1372. @isolate_apps('schema')
  1373. def test_rename_referenced_field(self):
  1374. class Author(Model):
  1375. name = CharField(max_length=255, unique=True)
  1376. class Meta:
  1377. app_label = 'schema'
  1378. class Book(Model):
  1379. author = ForeignKey(Author, CASCADE, to_field='name')
  1380. class Meta:
  1381. app_label = 'schema'
  1382. with connection.schema_editor() as editor:
  1383. editor.create_model(Author)
  1384. editor.create_model(Book)
  1385. new_field = CharField(max_length=255, unique=True)
  1386. new_field.set_attributes_from_name('renamed')
  1387. with connection.schema_editor(atomic=connection.features.supports_atomic_references_rename) as editor:
  1388. editor.alter_field(Author, Author._meta.get_field('name'), new_field)
  1389. # Ensure the foreign key reference was updated.
  1390. self.assertForeignKeyExists(Book, 'author_id', 'schema_author', 'renamed')
  1391. @skipIfDBFeature('interprets_empty_strings_as_nulls')
  1392. def test_rename_keep_null_status(self):
  1393. """
  1394. Renaming a field shouldn't affect the not null status.
  1395. """
  1396. with connection.schema_editor() as editor:
  1397. editor.create_model(Note)
  1398. with self.assertRaises(IntegrityError):
  1399. Note.objects.create(info=None)
  1400. old_field = Note._meta.get_field("info")
  1401. new_field = TextField()
  1402. new_field.set_attributes_from_name("detail_info")
  1403. with connection.schema_editor() as editor:
  1404. editor.alter_field(Note, old_field, new_field, strict=True)
  1405. columns = self.column_classes(Note)
  1406. self.assertEqual(columns['detail_info'][0], "TextField")
  1407. self.assertNotIn("info", columns)
  1408. with self.assertRaises(IntegrityError):
  1409. NoteRename.objects.create(detail_info=None)
  1410. def _test_m2m_create(self, M2MFieldClass):
  1411. """
  1412. Tests M2M fields on models during creation
  1413. """
  1414. class LocalBookWithM2M(Model):
  1415. author = ForeignKey(Author, CASCADE)
  1416. title = CharField(max_length=100, db_index=True)
  1417. pub_date = DateTimeField()
  1418. tags = M2MFieldClass("TagM2MTest", related_name="books")
  1419. class Meta:
  1420. app_label = 'schema'
  1421. apps = new_apps
  1422. self.local_models = [LocalBookWithM2M]
  1423. # Create the tables
  1424. with connection.schema_editor() as editor:
  1425. editor.create_model(Author)
  1426. editor.create_model(TagM2MTest)
  1427. editor.create_model(LocalBookWithM2M)
  1428. # Ensure there is now an m2m table there
  1429. columns = self.column_classes(LocalBookWithM2M._meta.get_field("tags").remote_field.through)
  1430. self.assertEqual(columns['tagm2mtest_id'][0], connection.features.introspected_field_types['IntegerField'])
  1431. def test_m2m_create(self):
  1432. self._test_m2m_create(ManyToManyField)
  1433. def test_m2m_create_custom(self):
  1434. self._test_m2m_create(CustomManyToManyField)
  1435. def test_m2m_create_inherited(self):
  1436. self._test_m2m_create(InheritedManyToManyField)
  1437. def _test_m2m_create_through(self, M2MFieldClass):
  1438. """
  1439. Tests M2M fields on models during creation with through models
  1440. """
  1441. class LocalTagThrough(Model):
  1442. book = ForeignKey("schema.LocalBookWithM2MThrough", CASCADE)
  1443. tag = ForeignKey("schema.TagM2MTest", CASCADE)
  1444. class Meta:
  1445. app_label = 'schema'
  1446. apps = new_apps
  1447. class LocalBookWithM2MThrough(Model):
  1448. tags = M2MFieldClass("TagM2MTest", related_name="books", through=LocalTagThrough)
  1449. class Meta:
  1450. app_label = 'schema'
  1451. apps = new_apps
  1452. self.local_models = [LocalTagThrough, LocalBookWithM2MThrough]
  1453. # Create the tables
  1454. with connection.schema_editor() as editor:
  1455. editor.create_model(LocalTagThrough)
  1456. editor.create_model(TagM2MTest)
  1457. editor.create_model(LocalBookWithM2MThrough)
  1458. # Ensure there is now an m2m table there
  1459. columns = self.column_classes(LocalTagThrough)
  1460. self.assertEqual(columns['book_id'][0], connection.features.introspected_field_types['IntegerField'])
  1461. self.assertEqual(columns['tag_id'][0], connection.features.introspected_field_types['IntegerField'])
  1462. def test_m2m_create_through(self):
  1463. self._test_m2m_create_through(ManyToManyField)
  1464. def test_m2m_create_through_custom(self):
  1465. self._test_m2m_create_through(CustomManyToManyField)
  1466. def test_m2m_create_through_inherited(self):
  1467. self._test_m2m_create_through(InheritedManyToManyField)
  1468. def _test_m2m(self, M2MFieldClass):
  1469. """
  1470. Tests adding/removing M2M fields on models
  1471. """
  1472. class LocalAuthorWithM2M(Model):
  1473. name = CharField(max_length=255)
  1474. class Meta:
  1475. app_label = 'schema'
  1476. apps = new_apps
  1477. self.local_models = [LocalAuthorWithM2M]
  1478. # Create the tables
  1479. with connection.schema_editor() as editor:
  1480. editor.create_model(LocalAuthorWithM2M)
  1481. editor.create_model(TagM2MTest)
  1482. # Create an M2M field
  1483. new_field = M2MFieldClass("schema.TagM2MTest", related_name="authors")
  1484. new_field.contribute_to_class(LocalAuthorWithM2M, "tags")
  1485. # Ensure there's no m2m table there
  1486. with self.assertRaises(DatabaseError):
  1487. self.column_classes(new_field.remote_field.through)
  1488. # Add the field
  1489. with connection.schema_editor() as editor:
  1490. editor.add_field(LocalAuthorWithM2M, new_field)
  1491. # Ensure there is now an m2m table there
  1492. columns = self.column_classes(new_field.remote_field.through)
  1493. self.assertEqual(columns['tagm2mtest_id'][0], connection.features.introspected_field_types['IntegerField'])
  1494. # "Alter" the field. This should not rename the DB table to itself.
  1495. with connection.schema_editor() as editor:
  1496. editor.alter_field(LocalAuthorWithM2M, new_field, new_field, strict=True)
  1497. # Remove the M2M table again
  1498. with connection.schema_editor() as editor:
  1499. editor.remove_field(LocalAuthorWithM2M, new_field)
  1500. # Ensure there's no m2m table there
  1501. with self.assertRaises(DatabaseError):
  1502. self.column_classes(new_field.remote_field.through)
  1503. # Make sure the model state is coherent with the table one now that
  1504. # we've removed the tags field.
  1505. opts = LocalAuthorWithM2M._meta
  1506. opts.local_many_to_many.remove(new_field)
  1507. del new_apps.all_models['schema'][new_field.remote_field.through._meta.model_name]
  1508. opts._expire_cache()
  1509. def test_m2m(self):
  1510. self._test_m2m(ManyToManyField)
  1511. def test_m2m_custom(self):
  1512. self._test_m2m(CustomManyToManyField)
  1513. def test_m2m_inherited(self):
  1514. self._test_m2m(InheritedManyToManyField)
  1515. def _test_m2m_through_alter(self, M2MFieldClass):
  1516. """
  1517. Tests altering M2Ms with explicit through models (should no-op)
  1518. """
  1519. class LocalAuthorTag(Model):
  1520. author = ForeignKey("schema.LocalAuthorWithM2MThrough", CASCADE)
  1521. tag = ForeignKey("schema.TagM2MTest", CASCADE)
  1522. class Meta:
  1523. app_label = 'schema'
  1524. apps = new_apps
  1525. class LocalAuthorWithM2MThrough(Model):
  1526. name = CharField(max_length=255)
  1527. tags = M2MFieldClass("schema.TagM2MTest", related_name="authors", through=LocalAuthorTag)
  1528. class Meta:
  1529. app_label = 'schema'
  1530. apps = new_apps
  1531. self.local_models = [LocalAuthorTag, LocalAuthorWithM2MThrough]
  1532. # Create the tables
  1533. with connection.schema_editor() as editor:
  1534. editor.create_model(LocalAuthorTag)
  1535. editor.create_model(LocalAuthorWithM2MThrough)
  1536. editor.create_model(TagM2MTest)
  1537. # Ensure the m2m table is there
  1538. self.assertEqual(len(self.column_classes(LocalAuthorTag)), 3)
  1539. # "Alter" the field's blankness. This should not actually do anything.
  1540. old_field = LocalAuthorWithM2MThrough._meta.get_field("tags")
  1541. new_field = M2MFieldClass("schema.TagM2MTest", related_name="authors", through=LocalAuthorTag)
  1542. new_field.contribute_to_class(LocalAuthorWithM2MThrough, "tags")
  1543. with connection.schema_editor() as editor:
  1544. editor.alter_field(LocalAuthorWithM2MThrough, old_field, new_field, strict=True)
  1545. # Ensure the m2m table is still there
  1546. self.assertEqual(len(self.column_classes(LocalAuthorTag)), 3)
  1547. def test_m2m_through_alter(self):
  1548. self._test_m2m_through_alter(ManyToManyField)
  1549. def test_m2m_through_alter_custom(self):
  1550. self._test_m2m_through_alter(CustomManyToManyField)
  1551. def test_m2m_through_alter_inherited(self):
  1552. self._test_m2m_through_alter(InheritedManyToManyField)
  1553. def _test_m2m_repoint(self, M2MFieldClass):
  1554. """
  1555. Tests repointing M2M fields
  1556. """
  1557. class LocalBookWithM2M(Model):
  1558. author = ForeignKey(Author, CASCADE)
  1559. title = CharField(max_length=100, db_index=True)
  1560. pub_date = DateTimeField()
  1561. tags = M2MFieldClass("TagM2MTest", related_name="books")
  1562. class Meta:
  1563. app_label = 'schema'
  1564. apps = new_apps
  1565. self.local_models = [LocalBookWithM2M]
  1566. # Create the tables
  1567. with connection.schema_editor() as editor:
  1568. editor.create_model(Author)
  1569. editor.create_model(LocalBookWithM2M)
  1570. editor.create_model(TagM2MTest)
  1571. editor.create_model(UniqueTest)
  1572. # Ensure the M2M exists and points to TagM2MTest
  1573. if connection.features.supports_foreign_keys:
  1574. self.assertForeignKeyExists(
  1575. LocalBookWithM2M._meta.get_field("tags").remote_field.through,
  1576. 'tagm2mtest_id',
  1577. 'schema_tagm2mtest',
  1578. )
  1579. # Repoint the M2M
  1580. old_field = LocalBookWithM2M._meta.get_field("tags")
  1581. new_field = M2MFieldClass(UniqueTest)
  1582. new_field.contribute_to_class(LocalBookWithM2M, "uniques")
  1583. with connection.schema_editor() as editor:
  1584. editor.alter_field(LocalBookWithM2M, old_field, new_field, strict=True)
  1585. # Ensure old M2M is gone
  1586. with self.assertRaises(DatabaseError):
  1587. self.column_classes(LocalBookWithM2M._meta.get_field("tags").remote_field.through)
  1588. # This model looks like the new model and is used for teardown.
  1589. opts = LocalBookWithM2M._meta
  1590. opts.local_many_to_many.remove(old_field)
  1591. # Ensure the new M2M exists and points to UniqueTest
  1592. if connection.features.supports_foreign_keys:
  1593. self.assertForeignKeyExists(new_field.remote_field.through, 'uniquetest_id', 'schema_uniquetest')
  1594. def test_m2m_repoint(self):
  1595. self._test_m2m_repoint(ManyToManyField)
  1596. def test_m2m_repoint_custom(self):
  1597. self._test_m2m_repoint(CustomManyToManyField)
  1598. def test_m2m_repoint_inherited(self):
  1599. self._test_m2m_repoint(InheritedManyToManyField)
  1600. @isolate_apps('schema')
  1601. def test_m2m_rename_field_in_target_model(self):
  1602. class LocalTagM2MTest(Model):
  1603. title = CharField(max_length=255)
  1604. class Meta:
  1605. app_label = 'schema'
  1606. class LocalM2M(Model):
  1607. tags = ManyToManyField(LocalTagM2MTest)
  1608. class Meta:
  1609. app_label = 'schema'
  1610. # Create the tables.
  1611. with connection.schema_editor() as editor:
  1612. editor.create_model(LocalM2M)
  1613. editor.create_model(LocalTagM2MTest)
  1614. self.isolated_local_models = [LocalM2M, LocalTagM2MTest]
  1615. # Ensure the m2m table is there.
  1616. self.assertEqual(len(self.column_classes(LocalM2M)), 1)
  1617. # Alter a field in LocalTagM2MTest.
  1618. old_field = LocalTagM2MTest._meta.get_field('title')
  1619. new_field = CharField(max_length=254)
  1620. new_field.contribute_to_class(LocalTagM2MTest, 'title1')
  1621. # @isolate_apps() and inner models are needed to have the model
  1622. # relations populated, otherwise this doesn't act as a regression test.
  1623. self.assertEqual(len(new_field.model._meta.related_objects), 1)
  1624. with connection.schema_editor() as editor:
  1625. editor.alter_field(LocalTagM2MTest, old_field, new_field, strict=True)
  1626. # Ensure the m2m table is still there.
  1627. self.assertEqual(len(self.column_classes(LocalM2M)), 1)
  1628. @skipUnlessDBFeature('supports_column_check_constraints', 'can_introspect_check_constraints')
  1629. def test_check_constraints(self):
  1630. """
  1631. Tests creating/deleting CHECK constraints
  1632. """
  1633. # Create the tables
  1634. with connection.schema_editor() as editor:
  1635. editor.create_model(Author)
  1636. # Ensure the constraint exists
  1637. constraints = self.get_constraints(Author._meta.db_table)
  1638. if not any(details['columns'] == ['height'] and details['check'] for details in constraints.values()):
  1639. self.fail("No check constraint for height found")
  1640. # Alter the column to remove it
  1641. old_field = Author._meta.get_field("height")
  1642. new_field = IntegerField(null=True, blank=True)
  1643. new_field.set_attributes_from_name("height")
  1644. with connection.schema_editor() as editor:
  1645. editor.alter_field(Author, old_field, new_field, strict=True)
  1646. constraints = self.get_constraints(Author._meta.db_table)
  1647. for details in constraints.values():
  1648. if details['columns'] == ["height"] and details['check']:
  1649. self.fail("Check constraint for height found")
  1650. # Alter the column to re-add it
  1651. new_field2 = Author._meta.get_field("height")
  1652. with connection.schema_editor() as editor:
  1653. editor.alter_field(Author, new_field, new_field2, strict=True)
  1654. constraints = self.get_constraints(Author._meta.db_table)
  1655. if not any(details['columns'] == ['height'] and details['check'] for details in constraints.values()):
  1656. self.fail("No check constraint for height found")
  1657. @skipUnlessDBFeature('supports_column_check_constraints', 'can_introspect_check_constraints')
  1658. def test_remove_field_check_does_not_remove_meta_constraints(self):
  1659. with connection.schema_editor() as editor:
  1660. editor.create_model(Author)
  1661. # Add the custom check constraint
  1662. constraint = CheckConstraint(check=Q(height__gte=0), name='author_height_gte_0_check')
  1663. custom_constraint_name = constraint.name
  1664. Author._meta.constraints = [constraint]
  1665. with connection.schema_editor() as editor:
  1666. editor.add_constraint(Author, constraint)
  1667. # Ensure the constraints exist
  1668. constraints = self.get_constraints(Author._meta.db_table)
  1669. self.assertIn(custom_constraint_name, constraints)
  1670. other_constraints = [
  1671. name for name, details in constraints.items()
  1672. if details['columns'] == ['height'] and details['check'] and name != custom_constraint_name
  1673. ]
  1674. self.assertEqual(len(other_constraints), 1)
  1675. # Alter the column to remove field check
  1676. old_field = Author._meta.get_field('height')
  1677. new_field = IntegerField(null=True, blank=True)
  1678. new_field.set_attributes_from_name('height')
  1679. with connection.schema_editor() as editor:
  1680. editor.alter_field(Author, old_field, new_field, strict=True)
  1681. constraints = self.get_constraints(Author._meta.db_table)
  1682. self.assertIn(custom_constraint_name, constraints)
  1683. other_constraints = [
  1684. name for name, details in constraints.items()
  1685. if details['columns'] == ['height'] and details['check'] and name != custom_constraint_name
  1686. ]
  1687. self.assertEqual(len(other_constraints), 0)
  1688. # Alter the column to re-add field check
  1689. new_field2 = Author._meta.get_field('height')
  1690. with connection.schema_editor() as editor:
  1691. editor.alter_field(Author, new_field, new_field2, strict=True)
  1692. constraints = self.get_constraints(Author._meta.db_table)
  1693. self.assertIn(custom_constraint_name, constraints)
  1694. other_constraints = [
  1695. name for name, details in constraints.items()
  1696. if details['columns'] == ['height'] and details['check'] and name != custom_constraint_name
  1697. ]
  1698. self.assertEqual(len(other_constraints), 1)
  1699. # Drop the check constraint
  1700. with connection.schema_editor() as editor:
  1701. Author._meta.constraints = []
  1702. editor.remove_constraint(Author, constraint)
  1703. def test_unique(self):
  1704. """
  1705. Tests removing and adding unique constraints to a single column.
  1706. """
  1707. # Create the table
  1708. with connection.schema_editor() as editor:
  1709. editor.create_model(Tag)
  1710. # Ensure the field is unique to begin with
  1711. Tag.objects.create(title="foo", slug="foo")
  1712. with self.assertRaises(IntegrityError):
  1713. Tag.objects.create(title="bar", slug="foo")
  1714. Tag.objects.all().delete()
  1715. # Alter the slug field to be non-unique
  1716. old_field = Tag._meta.get_field("slug")
  1717. new_field = SlugField(unique=False)
  1718. new_field.set_attributes_from_name("slug")
  1719. with connection.schema_editor() as editor:
  1720. editor.alter_field(Tag, old_field, new_field, strict=True)
  1721. # Ensure the field is no longer unique
  1722. Tag.objects.create(title="foo", slug="foo")
  1723. Tag.objects.create(title="bar", slug="foo")
  1724. Tag.objects.all().delete()
  1725. # Alter the slug field to be unique
  1726. new_field2 = SlugField(unique=True)
  1727. new_field2.set_attributes_from_name("slug")
  1728. with connection.schema_editor() as editor:
  1729. editor.alter_field(Tag, new_field, new_field2, strict=True)
  1730. # Ensure the field is unique again
  1731. Tag.objects.create(title="foo", slug="foo")
  1732. with self.assertRaises(IntegrityError):
  1733. Tag.objects.create(title="bar", slug="foo")
  1734. Tag.objects.all().delete()
  1735. # Rename the field
  1736. new_field3 = SlugField(unique=True)
  1737. new_field3.set_attributes_from_name("slug2")
  1738. with connection.schema_editor() as editor:
  1739. editor.alter_field(Tag, new_field2, new_field3, strict=True)
  1740. # Ensure the field is still unique
  1741. TagUniqueRename.objects.create(title="foo", slug2="foo")
  1742. with self.assertRaises(IntegrityError):
  1743. TagUniqueRename.objects.create(title="bar", slug2="foo")
  1744. Tag.objects.all().delete()
  1745. def test_unique_name_quoting(self):
  1746. old_table_name = TagUniqueRename._meta.db_table
  1747. try:
  1748. with connection.schema_editor() as editor:
  1749. editor.create_model(TagUniqueRename)
  1750. editor.alter_db_table(TagUniqueRename, old_table_name, 'unique-table')
  1751. TagUniqueRename._meta.db_table = 'unique-table'
  1752. # This fails if the unique index name isn't quoted.
  1753. editor.alter_unique_together(TagUniqueRename, [], (('title', 'slug2'),))
  1754. finally:
  1755. TagUniqueRename._meta.db_table = old_table_name
  1756. @isolate_apps('schema')
  1757. @skipUnlessDBFeature('supports_foreign_keys')
  1758. def test_unique_no_unnecessary_fk_drops(self):
  1759. """
  1760. If AlterField isn't selective about dropping foreign key constraints
  1761. when modifying a field with a unique constraint, the AlterField
  1762. incorrectly drops and recreates the Book.author foreign key even though
  1763. it doesn't restrict the field being changed (#29193).
  1764. """
  1765. class Author(Model):
  1766. name = CharField(max_length=254, unique=True)
  1767. class Meta:
  1768. app_label = 'schema'
  1769. class Book(Model):
  1770. author = ForeignKey(Author, CASCADE)
  1771. class Meta:
  1772. app_label = 'schema'
  1773. with connection.schema_editor() as editor:
  1774. editor.create_model(Author)
  1775. editor.create_model(Book)
  1776. new_field = CharField(max_length=255, unique=True)
  1777. new_field.model = Author
  1778. new_field.set_attributes_from_name('name')
  1779. with self.assertLogs('django.db.backends.schema', 'DEBUG') as cm:
  1780. with connection.schema_editor() as editor:
  1781. editor.alter_field(Author, Author._meta.get_field('name'), new_field)
  1782. # One SQL statement is executed to alter the field.
  1783. self.assertEqual(len(cm.records), 1)
  1784. @isolate_apps('schema')
  1785. def test_unique_and_reverse_m2m(self):
  1786. """
  1787. AlterField can modify a unique field when there's a reverse M2M
  1788. relation on the model.
  1789. """
  1790. class Tag(Model):
  1791. title = CharField(max_length=255)
  1792. slug = SlugField(unique=True)
  1793. class Meta:
  1794. app_label = 'schema'
  1795. class Book(Model):
  1796. tags = ManyToManyField(Tag, related_name='books')
  1797. class Meta:
  1798. app_label = 'schema'
  1799. self.isolated_local_models = [Book._meta.get_field('tags').remote_field.through]
  1800. with connection.schema_editor() as editor:
  1801. editor.create_model(Tag)
  1802. editor.create_model(Book)
  1803. new_field = SlugField(max_length=75, unique=True)
  1804. new_field.model = Tag
  1805. new_field.set_attributes_from_name('slug')
  1806. with self.assertLogs('django.db.backends.schema', 'DEBUG') as cm:
  1807. with connection.schema_editor() as editor:
  1808. editor.alter_field(Tag, Tag._meta.get_field('slug'), new_field)
  1809. # One SQL statement is executed to alter the field.
  1810. self.assertEqual(len(cm.records), 1)
  1811. # Ensure that the field is still unique.
  1812. Tag.objects.create(title='foo', slug='foo')
  1813. with self.assertRaises(IntegrityError):
  1814. Tag.objects.create(title='bar', slug='foo')
  1815. @skipUnlessDBFeature('allows_multiple_constraints_on_same_fields')
  1816. def test_remove_field_unique_does_not_remove_meta_constraints(self):
  1817. with connection.schema_editor() as editor:
  1818. editor.create_model(AuthorWithUniqueName)
  1819. # Add the custom unique constraint
  1820. constraint = UniqueConstraint(fields=['name'], name='author_name_uniq')
  1821. custom_constraint_name = constraint.name
  1822. AuthorWithUniqueName._meta.constraints = [constraint]
  1823. with connection.schema_editor() as editor:
  1824. editor.add_constraint(AuthorWithUniqueName, constraint)
  1825. # Ensure the constraints exist
  1826. constraints = self.get_constraints(AuthorWithUniqueName._meta.db_table)
  1827. self.assertIn(custom_constraint_name, constraints)
  1828. other_constraints = [
  1829. name for name, details in constraints.items()
  1830. if details['columns'] == ['name'] and details['unique'] and name != custom_constraint_name
  1831. ]
  1832. self.assertEqual(len(other_constraints), 1)
  1833. # Alter the column to remove field uniqueness
  1834. old_field = AuthorWithUniqueName._meta.get_field('name')
  1835. new_field = CharField(max_length=255)
  1836. new_field.set_attributes_from_name('name')
  1837. with connection.schema_editor() as editor:
  1838. editor.alter_field(AuthorWithUniqueName, old_field, new_field, strict=True)
  1839. constraints = self.get_constraints(AuthorWithUniqueName._meta.db_table)
  1840. self.assertIn(custom_constraint_name, constraints)
  1841. other_constraints = [
  1842. name for name, details in constraints.items()
  1843. if details['columns'] == ['name'] and details['unique'] and name != custom_constraint_name
  1844. ]
  1845. self.assertEqual(len(other_constraints), 0)
  1846. # Alter the column to re-add field uniqueness
  1847. new_field2 = AuthorWithUniqueName._meta.get_field('name')
  1848. with connection.schema_editor() as editor:
  1849. editor.alter_field(AuthorWithUniqueName, new_field, new_field2, strict=True)
  1850. constraints = self.get_constraints(AuthorWithUniqueName._meta.db_table)
  1851. self.assertIn(custom_constraint_name, constraints)
  1852. other_constraints = [
  1853. name for name, details in constraints.items()
  1854. if details['columns'] == ['name'] and details['unique'] and name != custom_constraint_name
  1855. ]
  1856. self.assertEqual(len(other_constraints), 1)
  1857. # Drop the unique constraint
  1858. with connection.schema_editor() as editor:
  1859. AuthorWithUniqueName._meta.constraints = []
  1860. editor.remove_constraint(AuthorWithUniqueName, constraint)
  1861. def test_unique_together(self):
  1862. """
  1863. Tests removing and adding unique_together constraints on a model.
  1864. """
  1865. # Create the table
  1866. with connection.schema_editor() as editor:
  1867. editor.create_model(UniqueTest)
  1868. # Ensure the fields are unique to begin with
  1869. UniqueTest.objects.create(year=2012, slug="foo")
  1870. UniqueTest.objects.create(year=2011, slug="foo")
  1871. UniqueTest.objects.create(year=2011, slug="bar")
  1872. with self.assertRaises(IntegrityError):
  1873. UniqueTest.objects.create(year=2012, slug="foo")
  1874. UniqueTest.objects.all().delete()
  1875. # Alter the model to its non-unique-together companion
  1876. with connection.schema_editor() as editor:
  1877. editor.alter_unique_together(UniqueTest, UniqueTest._meta.unique_together, [])
  1878. # Ensure the fields are no longer unique
  1879. UniqueTest.objects.create(year=2012, slug="foo")
  1880. UniqueTest.objects.create(year=2012, slug="foo")
  1881. UniqueTest.objects.all().delete()
  1882. # Alter it back
  1883. new_field2 = SlugField(unique=True)
  1884. new_field2.set_attributes_from_name("slug")
  1885. with connection.schema_editor() as editor:
  1886. editor.alter_unique_together(UniqueTest, [], UniqueTest._meta.unique_together)
  1887. # Ensure the fields are unique again
  1888. UniqueTest.objects.create(year=2012, slug="foo")
  1889. with self.assertRaises(IntegrityError):
  1890. UniqueTest.objects.create(year=2012, slug="foo")
  1891. UniqueTest.objects.all().delete()
  1892. def test_unique_together_with_fk(self):
  1893. """
  1894. Tests removing and adding unique_together constraints that include
  1895. a foreign key.
  1896. """
  1897. # Create the table
  1898. with connection.schema_editor() as editor:
  1899. editor.create_model(Author)
  1900. editor.create_model(Book)
  1901. # Ensure the fields are unique to begin with
  1902. self.assertEqual(Book._meta.unique_together, ())
  1903. # Add the unique_together constraint
  1904. with connection.schema_editor() as editor:
  1905. editor.alter_unique_together(Book, [], [['author', 'title']])
  1906. # Alter it back
  1907. with connection.schema_editor() as editor:
  1908. editor.alter_unique_together(Book, [['author', 'title']], [])
  1909. def test_unique_together_with_fk_with_existing_index(self):
  1910. """
  1911. Tests removing and adding unique_together constraints that include
  1912. a foreign key, where the foreign key is added after the model is
  1913. created.
  1914. """
  1915. # Create the tables
  1916. with connection.schema_editor() as editor:
  1917. editor.create_model(Author)
  1918. editor.create_model(BookWithoutAuthor)
  1919. new_field = ForeignKey(Author, CASCADE)
  1920. new_field.set_attributes_from_name('author')
  1921. editor.add_field(BookWithoutAuthor, new_field)
  1922. # Ensure the fields aren't unique to begin with
  1923. self.assertEqual(Book._meta.unique_together, ())
  1924. # Add the unique_together constraint
  1925. with connection.schema_editor() as editor:
  1926. editor.alter_unique_together(Book, [], [['author', 'title']])
  1927. # Alter it back
  1928. with connection.schema_editor() as editor:
  1929. editor.alter_unique_together(Book, [['author', 'title']], [])
  1930. @skipUnlessDBFeature('allows_multiple_constraints_on_same_fields')
  1931. def test_remove_unique_together_does_not_remove_meta_constraints(self):
  1932. with connection.schema_editor() as editor:
  1933. editor.create_model(AuthorWithUniqueNameAndBirthday)
  1934. # Add the custom unique constraint
  1935. constraint = UniqueConstraint(fields=['name', 'birthday'], name='author_name_birthday_uniq')
  1936. custom_constraint_name = constraint.name
  1937. AuthorWithUniqueNameAndBirthday._meta.constraints = [constraint]
  1938. with connection.schema_editor() as editor:
  1939. editor.add_constraint(AuthorWithUniqueNameAndBirthday, constraint)
  1940. # Ensure the constraints exist
  1941. constraints = self.get_constraints(AuthorWithUniqueNameAndBirthday._meta.db_table)
  1942. self.assertIn(custom_constraint_name, constraints)
  1943. other_constraints = [
  1944. name for name, details in constraints.items()
  1945. if details['columns'] == ['name', 'birthday'] and details['unique'] and name != custom_constraint_name
  1946. ]
  1947. self.assertEqual(len(other_constraints), 1)
  1948. # Remove unique together
  1949. unique_together = AuthorWithUniqueNameAndBirthday._meta.unique_together
  1950. with connection.schema_editor() as editor:
  1951. editor.alter_unique_together(AuthorWithUniqueNameAndBirthday, unique_together, [])
  1952. constraints = self.get_constraints(AuthorWithUniqueNameAndBirthday._meta.db_table)
  1953. self.assertIn(custom_constraint_name, constraints)
  1954. other_constraints = [
  1955. name for name, details in constraints.items()
  1956. if details['columns'] == ['name', 'birthday'] and details['unique'] and name != custom_constraint_name
  1957. ]
  1958. self.assertEqual(len(other_constraints), 0)
  1959. # Re-add unique together
  1960. with connection.schema_editor() as editor:
  1961. editor.alter_unique_together(AuthorWithUniqueNameAndBirthday, [], unique_together)
  1962. constraints = self.get_constraints(AuthorWithUniqueNameAndBirthday._meta.db_table)
  1963. self.assertIn(custom_constraint_name, constraints)
  1964. other_constraints = [
  1965. name for name, details in constraints.items()
  1966. if details['columns'] == ['name', 'birthday'] and details['unique'] and name != custom_constraint_name
  1967. ]
  1968. self.assertEqual(len(other_constraints), 1)
  1969. # Drop the unique constraint
  1970. with connection.schema_editor() as editor:
  1971. AuthorWithUniqueNameAndBirthday._meta.constraints = []
  1972. editor.remove_constraint(AuthorWithUniqueNameAndBirthday, constraint)
  1973. @skipUnlessDBFeature('supports_expression_indexes')
  1974. def test_func_unique_constraint(self):
  1975. with connection.schema_editor() as editor:
  1976. editor.create_model(Author)
  1977. constraint = UniqueConstraint(Upper('name').desc(), name='func_upper_uq')
  1978. # Add constraint.
  1979. with connection.schema_editor() as editor:
  1980. editor.add_constraint(Author, constraint)
  1981. sql = constraint.create_sql(Author, editor)
  1982. table = Author._meta.db_table
  1983. constraints = self.get_constraints(table)
  1984. if connection.features.supports_index_column_ordering:
  1985. self.assertIndexOrder(table, constraint.name, ['DESC'])
  1986. self.assertIn(constraint.name, constraints)
  1987. self.assertIs(constraints[constraint.name]['unique'], True)
  1988. # SQL contains a database function.
  1989. self.assertIs(sql.references_column(table, 'name'), True)
  1990. self.assertIn('UPPER(%s)' % editor.quote_name('name'), str(sql))
  1991. # Remove constraint.
  1992. with connection.schema_editor() as editor:
  1993. editor.remove_constraint(Author, constraint)
  1994. self.assertNotIn(constraint.name, self.get_constraints(table))
  1995. @skipUnlessDBFeature('supports_expression_indexes')
  1996. def test_composite_func_unique_constraint(self):
  1997. with connection.schema_editor() as editor:
  1998. editor.create_model(Author)
  1999. editor.create_model(BookWithSlug)
  2000. constraint = UniqueConstraint(
  2001. Upper('title'),
  2002. Lower('slug'),
  2003. name='func_upper_lower_unq',
  2004. )
  2005. # Add constraint.
  2006. with connection.schema_editor() as editor:
  2007. editor.add_constraint(BookWithSlug, constraint)
  2008. sql = constraint.create_sql(BookWithSlug, editor)
  2009. table = BookWithSlug._meta.db_table
  2010. constraints = self.get_constraints(table)
  2011. self.assertIn(constraint.name, constraints)
  2012. self.assertIs(constraints[constraint.name]['unique'], True)
  2013. # SQL contains database functions.
  2014. self.assertIs(sql.references_column(table, 'title'), True)
  2015. self.assertIs(sql.references_column(table, 'slug'), True)
  2016. sql = str(sql)
  2017. self.assertIn('UPPER(%s)' % editor.quote_name('title'), sql)
  2018. self.assertIn('LOWER(%s)' % editor.quote_name('slug'), sql)
  2019. self.assertLess(sql.index('UPPER'), sql.index('LOWER'))
  2020. # Remove constraint.
  2021. with connection.schema_editor() as editor:
  2022. editor.remove_constraint(BookWithSlug, constraint)
  2023. self.assertNotIn(constraint.name, self.get_constraints(table))
  2024. @skipUnlessDBFeature('supports_expression_indexes')
  2025. def test_unique_constraint_field_and_expression(self):
  2026. with connection.schema_editor() as editor:
  2027. editor.create_model(Author)
  2028. constraint = UniqueConstraint(
  2029. F('height').desc(),
  2030. 'uuid',
  2031. Lower('name').asc(),
  2032. name='func_f_lower_field_unq',
  2033. )
  2034. # Add constraint.
  2035. with connection.schema_editor() as editor:
  2036. editor.add_constraint(Author, constraint)
  2037. sql = constraint.create_sql(Author, editor)
  2038. table = Author._meta.db_table
  2039. if connection.features.supports_index_column_ordering:
  2040. self.assertIndexOrder(table, constraint.name, ['DESC', 'ASC', 'ASC'])
  2041. constraints = self.get_constraints(table)
  2042. self.assertIs(constraints[constraint.name]['unique'], True)
  2043. self.assertEqual(len(constraints[constraint.name]['columns']), 3)
  2044. self.assertEqual(constraints[constraint.name]['columns'][1], 'uuid')
  2045. # SQL contains database functions and columns.
  2046. self.assertIs(sql.references_column(table, 'height'), True)
  2047. self.assertIs(sql.references_column(table, 'name'), True)
  2048. self.assertIs(sql.references_column(table, 'uuid'), True)
  2049. self.assertIn('LOWER(%s)' % editor.quote_name('name'), str(sql))
  2050. # Remove constraint.
  2051. with connection.schema_editor() as editor:
  2052. editor.remove_constraint(Author, constraint)
  2053. self.assertNotIn(constraint.name, self.get_constraints(table))
  2054. @skipUnlessDBFeature('supports_expression_indexes', 'supports_partial_indexes')
  2055. def test_func_unique_constraint_partial(self):
  2056. with connection.schema_editor() as editor:
  2057. editor.create_model(Author)
  2058. constraint = UniqueConstraint(
  2059. Upper('name'),
  2060. name='func_upper_cond_weight_uq',
  2061. condition=Q(weight__isnull=False),
  2062. )
  2063. # Add constraint.
  2064. with connection.schema_editor() as editor:
  2065. editor.add_constraint(Author, constraint)
  2066. sql = constraint.create_sql(Author, editor)
  2067. table = Author._meta.db_table
  2068. constraints = self.get_constraints(table)
  2069. self.assertIn(constraint.name, constraints)
  2070. self.assertIs(constraints[constraint.name]['unique'], True)
  2071. self.assertIs(sql.references_column(table, 'name'), True)
  2072. self.assertIn('UPPER(%s)' % editor.quote_name('name'), str(sql))
  2073. self.assertIn(
  2074. 'WHERE %s IS NOT NULL' % editor.quote_name('weight'),
  2075. str(sql),
  2076. )
  2077. # Remove constraint.
  2078. with connection.schema_editor() as editor:
  2079. editor.remove_constraint(Author, constraint)
  2080. self.assertNotIn(constraint.name, self.get_constraints(table))
  2081. @skipUnlessDBFeature('supports_expression_indexes', 'supports_covering_indexes')
  2082. def test_func_unique_constraint_covering(self):
  2083. with connection.schema_editor() as editor:
  2084. editor.create_model(Author)
  2085. constraint = UniqueConstraint(
  2086. Upper('name'),
  2087. name='func_upper_covering_uq',
  2088. include=['weight', 'height'],
  2089. )
  2090. # Add constraint.
  2091. with connection.schema_editor() as editor:
  2092. editor.add_constraint(Author, constraint)
  2093. sql = constraint.create_sql(Author, editor)
  2094. table = Author._meta.db_table
  2095. constraints = self.get_constraints(table)
  2096. self.assertIn(constraint.name, constraints)
  2097. self.assertIs(constraints[constraint.name]['unique'], True)
  2098. self.assertEqual(
  2099. constraints[constraint.name]['columns'],
  2100. [None, 'weight', 'height'],
  2101. )
  2102. self.assertIs(sql.references_column(table, 'name'), True)
  2103. self.assertIs(sql.references_column(table, 'weight'), True)
  2104. self.assertIs(sql.references_column(table, 'height'), True)
  2105. self.assertIn('UPPER(%s)' % editor.quote_name('name'), str(sql))
  2106. self.assertIn(
  2107. 'INCLUDE (%s, %s)' % (
  2108. editor.quote_name('weight'),
  2109. editor.quote_name('height'),
  2110. ),
  2111. str(sql),
  2112. )
  2113. # Remove constraint.
  2114. with connection.schema_editor() as editor:
  2115. editor.remove_constraint(Author, constraint)
  2116. self.assertNotIn(constraint.name, self.get_constraints(table))
  2117. @skipUnlessDBFeature('supports_expression_indexes')
  2118. def test_func_unique_constraint_lookups(self):
  2119. with connection.schema_editor() as editor:
  2120. editor.create_model(Author)
  2121. with register_lookup(CharField, Lower), register_lookup(IntegerField, Abs):
  2122. constraint = UniqueConstraint(
  2123. F('name__lower'),
  2124. F('weight__abs'),
  2125. name='func_lower_abs_lookup_uq',
  2126. )
  2127. # Add constraint.
  2128. with connection.schema_editor() as editor:
  2129. editor.add_constraint(Author, constraint)
  2130. sql = constraint.create_sql(Author, editor)
  2131. table = Author._meta.db_table
  2132. constraints = self.get_constraints(table)
  2133. self.assertIn(constraint.name, constraints)
  2134. self.assertIs(constraints[constraint.name]['unique'], True)
  2135. # SQL contains columns.
  2136. self.assertIs(sql.references_column(table, 'name'), True)
  2137. self.assertIs(sql.references_column(table, 'weight'), True)
  2138. # Remove constraint.
  2139. with connection.schema_editor() as editor:
  2140. editor.remove_constraint(Author, constraint)
  2141. self.assertNotIn(constraint.name, self.get_constraints(table))
  2142. @skipUnlessDBFeature('supports_expression_indexes')
  2143. def test_func_unique_constraint_collate(self):
  2144. collation = connection.features.test_collations.get('non_default')
  2145. if not collation:
  2146. self.skipTest(
  2147. 'This backend does not support case-insensitive collations.'
  2148. )
  2149. with connection.schema_editor() as editor:
  2150. editor.create_model(Author)
  2151. editor.create_model(BookWithSlug)
  2152. constraint = UniqueConstraint(
  2153. Collate(F('title'), collation=collation).desc(),
  2154. Collate('slug', collation=collation),
  2155. name='func_collate_uq',
  2156. )
  2157. # Add constraint.
  2158. with connection.schema_editor() as editor:
  2159. editor.add_constraint(BookWithSlug, constraint)
  2160. sql = constraint.create_sql(BookWithSlug, editor)
  2161. table = BookWithSlug._meta.db_table
  2162. constraints = self.get_constraints(table)
  2163. self.assertIn(constraint.name, constraints)
  2164. self.assertIs(constraints[constraint.name]['unique'], True)
  2165. if connection.features.supports_index_column_ordering:
  2166. self.assertIndexOrder(table, constraint.name, ['DESC', 'ASC'])
  2167. # SQL contains columns and a collation.
  2168. self.assertIs(sql.references_column(table, 'title'), True)
  2169. self.assertIs(sql.references_column(table, 'slug'), True)
  2170. self.assertIn('COLLATE %s' % editor.quote_name(collation), str(sql))
  2171. # Remove constraint.
  2172. with connection.schema_editor() as editor:
  2173. editor.remove_constraint(BookWithSlug, constraint)
  2174. self.assertNotIn(constraint.name, self.get_constraints(table))
  2175. @skipIfDBFeature('supports_expression_indexes')
  2176. def test_func_unique_constraint_unsupported(self):
  2177. # UniqueConstraint is ignored on databases that don't support indexes on
  2178. # expressions.
  2179. with connection.schema_editor() as editor:
  2180. editor.create_model(Author)
  2181. constraint = UniqueConstraint(F('name'), name='func_name_uq')
  2182. with connection.schema_editor() as editor, self.assertNumQueries(0):
  2183. self.assertIsNone(editor.add_constraint(Author, constraint))
  2184. self.assertIsNone(editor.remove_constraint(Author, constraint))
  2185. @skipUnlessDBFeature('supports_expression_indexes')
  2186. def test_func_unique_constraint_nonexistent_field(self):
  2187. constraint = UniqueConstraint(Lower('nonexistent'), name='func_nonexistent_uq')
  2188. msg = (
  2189. "Cannot resolve keyword 'nonexistent' into field. Choices are: "
  2190. "height, id, name, uuid, weight"
  2191. )
  2192. with self.assertRaisesMessage(FieldError, msg):
  2193. with connection.schema_editor() as editor:
  2194. editor.add_constraint(Author, constraint)
  2195. @skipUnlessDBFeature('supports_expression_indexes')
  2196. def test_func_unique_constraint_nondeterministic(self):
  2197. with connection.schema_editor() as editor:
  2198. editor.create_model(Author)
  2199. constraint = UniqueConstraint(Random(), name='func_random_uq')
  2200. with connection.schema_editor() as editor:
  2201. with self.assertRaises(DatabaseError):
  2202. editor.add_constraint(Author, constraint)
  2203. def test_index_together(self):
  2204. """
  2205. Tests removing and adding index_together constraints on a model.
  2206. """
  2207. # Create the table
  2208. with connection.schema_editor() as editor:
  2209. editor.create_model(Tag)
  2210. # Ensure there's no index on the year/slug columns first
  2211. self.assertIs(
  2212. any(
  2213. c["index"]
  2214. for c in self.get_constraints("schema_tag").values()
  2215. if c['columns'] == ["slug", "title"]
  2216. ),
  2217. False,
  2218. )
  2219. # Alter the model to add an index
  2220. with connection.schema_editor() as editor:
  2221. editor.alter_index_together(Tag, [], [("slug", "title")])
  2222. # Ensure there is now an index
  2223. self.assertIs(
  2224. any(
  2225. c["index"]
  2226. for c in self.get_constraints("schema_tag").values()
  2227. if c['columns'] == ["slug", "title"]
  2228. ),
  2229. True,
  2230. )
  2231. # Alter it back
  2232. new_field2 = SlugField(unique=True)
  2233. new_field2.set_attributes_from_name("slug")
  2234. with connection.schema_editor() as editor:
  2235. editor.alter_index_together(Tag, [("slug", "title")], [])
  2236. # Ensure there's no index
  2237. self.assertIs(
  2238. any(
  2239. c["index"]
  2240. for c in self.get_constraints("schema_tag").values()
  2241. if c['columns'] == ["slug", "title"]
  2242. ),
  2243. False,
  2244. )
  2245. def test_index_together_with_fk(self):
  2246. """
  2247. Tests removing and adding index_together constraints that include
  2248. a foreign key.
  2249. """
  2250. # Create the table
  2251. with connection.schema_editor() as editor:
  2252. editor.create_model(Author)
  2253. editor.create_model(Book)
  2254. # Ensure the fields are unique to begin with
  2255. self.assertEqual(Book._meta.index_together, ())
  2256. # Add the unique_together constraint
  2257. with connection.schema_editor() as editor:
  2258. editor.alter_index_together(Book, [], [['author', 'title']])
  2259. # Alter it back
  2260. with connection.schema_editor() as editor:
  2261. editor.alter_index_together(Book, [['author', 'title']], [])
  2262. def test_create_index_together(self):
  2263. """
  2264. Tests creating models with index_together already defined
  2265. """
  2266. # Create the table
  2267. with connection.schema_editor() as editor:
  2268. editor.create_model(TagIndexed)
  2269. # Ensure there is an index
  2270. self.assertIs(
  2271. any(
  2272. c["index"]
  2273. for c in self.get_constraints("schema_tagindexed").values()
  2274. if c['columns'] == ["slug", "title"]
  2275. ),
  2276. True,
  2277. )
  2278. @skipUnlessDBFeature('allows_multiple_constraints_on_same_fields')
  2279. def test_remove_index_together_does_not_remove_meta_indexes(self):
  2280. with connection.schema_editor() as editor:
  2281. editor.create_model(AuthorWithIndexedNameAndBirthday)
  2282. # Add the custom index
  2283. index = Index(fields=['name', 'birthday'], name='author_name_birthday_idx')
  2284. custom_index_name = index.name
  2285. AuthorWithIndexedNameAndBirthday._meta.indexes = [index]
  2286. with connection.schema_editor() as editor:
  2287. editor.add_index(AuthorWithIndexedNameAndBirthday, index)
  2288. # Ensure the indexes exist
  2289. constraints = self.get_constraints(AuthorWithIndexedNameAndBirthday._meta.db_table)
  2290. self.assertIn(custom_index_name, constraints)
  2291. other_constraints = [
  2292. name for name, details in constraints.items()
  2293. if details['columns'] == ['name', 'birthday'] and details['index'] and name != custom_index_name
  2294. ]
  2295. self.assertEqual(len(other_constraints), 1)
  2296. # Remove index together
  2297. index_together = AuthorWithIndexedNameAndBirthday._meta.index_together
  2298. with connection.schema_editor() as editor:
  2299. editor.alter_index_together(AuthorWithIndexedNameAndBirthday, index_together, [])
  2300. constraints = self.get_constraints(AuthorWithIndexedNameAndBirthday._meta.db_table)
  2301. self.assertIn(custom_index_name, constraints)
  2302. other_constraints = [
  2303. name for name, details in constraints.items()
  2304. if details['columns'] == ['name', 'birthday'] and details['index'] and name != custom_index_name
  2305. ]
  2306. self.assertEqual(len(other_constraints), 0)
  2307. # Re-add index together
  2308. with connection.schema_editor() as editor:
  2309. editor.alter_index_together(AuthorWithIndexedNameAndBirthday, [], index_together)
  2310. constraints = self.get_constraints(AuthorWithIndexedNameAndBirthday._meta.db_table)
  2311. self.assertIn(custom_index_name, constraints)
  2312. other_constraints = [
  2313. name for name, details in constraints.items()
  2314. if details['columns'] == ['name', 'birthday'] and details['index'] and name != custom_index_name
  2315. ]
  2316. self.assertEqual(len(other_constraints), 1)
  2317. # Drop the index
  2318. with connection.schema_editor() as editor:
  2319. AuthorWithIndexedNameAndBirthday._meta.indexes = []
  2320. editor.remove_index(AuthorWithIndexedNameAndBirthday, index)
  2321. @isolate_apps('schema')
  2322. def test_db_table(self):
  2323. """
  2324. Tests renaming of the table
  2325. """
  2326. class Author(Model):
  2327. name = CharField(max_length=255)
  2328. class Meta:
  2329. app_label = 'schema'
  2330. class Book(Model):
  2331. author = ForeignKey(Author, CASCADE)
  2332. class Meta:
  2333. app_label = 'schema'
  2334. # Create the table and one referring it.
  2335. with connection.schema_editor() as editor:
  2336. editor.create_model(Author)
  2337. editor.create_model(Book)
  2338. # Ensure the table is there to begin with
  2339. columns = self.column_classes(Author)
  2340. self.assertEqual(columns['name'][0], connection.features.introspected_field_types['CharField'])
  2341. # Alter the table
  2342. with connection.schema_editor(atomic=connection.features.supports_atomic_references_rename) as editor:
  2343. editor.alter_db_table(Author, "schema_author", "schema_otherauthor")
  2344. # Ensure the table is there afterwards
  2345. Author._meta.db_table = "schema_otherauthor"
  2346. columns = self.column_classes(Author)
  2347. self.assertEqual(columns['name'][0], connection.features.introspected_field_types['CharField'])
  2348. # Ensure the foreign key reference was updated
  2349. self.assertForeignKeyExists(Book, "author_id", "schema_otherauthor")
  2350. # Alter the table again
  2351. with connection.schema_editor(atomic=connection.features.supports_atomic_references_rename) as editor:
  2352. editor.alter_db_table(Author, "schema_otherauthor", "schema_author")
  2353. # Ensure the table is still there
  2354. Author._meta.db_table = "schema_author"
  2355. columns = self.column_classes(Author)
  2356. self.assertEqual(columns['name'][0], connection.features.introspected_field_types['CharField'])
  2357. def test_add_remove_index(self):
  2358. """
  2359. Tests index addition and removal
  2360. """
  2361. # Create the table
  2362. with connection.schema_editor() as editor:
  2363. editor.create_model(Author)
  2364. # Ensure the table is there and has no index
  2365. self.assertNotIn('title', self.get_indexes(Author._meta.db_table))
  2366. # Add the index
  2367. index = Index(fields=['name'], name='author_title_idx')
  2368. with connection.schema_editor() as editor:
  2369. editor.add_index(Author, index)
  2370. self.assertIn('name', self.get_indexes(Author._meta.db_table))
  2371. # Drop the index
  2372. with connection.schema_editor() as editor:
  2373. editor.remove_index(Author, index)
  2374. self.assertNotIn('name', self.get_indexes(Author._meta.db_table))
  2375. def test_remove_db_index_doesnt_remove_custom_indexes(self):
  2376. """
  2377. Changing db_index to False doesn't remove indexes from Meta.indexes.
  2378. """
  2379. with connection.schema_editor() as editor:
  2380. editor.create_model(AuthorWithIndexedName)
  2381. # Ensure the table has its index
  2382. self.assertIn('name', self.get_indexes(AuthorWithIndexedName._meta.db_table))
  2383. # Add the custom index
  2384. index = Index(fields=['-name'], name='author_name_idx')
  2385. author_index_name = index.name
  2386. with connection.schema_editor() as editor:
  2387. db_index_name = editor._create_index_name(
  2388. table_name=AuthorWithIndexedName._meta.db_table,
  2389. column_names=('name',),
  2390. )
  2391. try:
  2392. AuthorWithIndexedName._meta.indexes = [index]
  2393. with connection.schema_editor() as editor:
  2394. editor.add_index(AuthorWithIndexedName, index)
  2395. old_constraints = self.get_constraints(AuthorWithIndexedName._meta.db_table)
  2396. self.assertIn(author_index_name, old_constraints)
  2397. self.assertIn(db_index_name, old_constraints)
  2398. # Change name field to db_index=False
  2399. old_field = AuthorWithIndexedName._meta.get_field('name')
  2400. new_field = CharField(max_length=255)
  2401. new_field.set_attributes_from_name('name')
  2402. with connection.schema_editor() as editor:
  2403. editor.alter_field(AuthorWithIndexedName, old_field, new_field, strict=True)
  2404. new_constraints = self.get_constraints(AuthorWithIndexedName._meta.db_table)
  2405. self.assertNotIn(db_index_name, new_constraints)
  2406. # The index from Meta.indexes is still in the database.
  2407. self.assertIn(author_index_name, new_constraints)
  2408. # Drop the index
  2409. with connection.schema_editor() as editor:
  2410. editor.remove_index(AuthorWithIndexedName, index)
  2411. finally:
  2412. AuthorWithIndexedName._meta.indexes = []
  2413. def test_order_index(self):
  2414. """
  2415. Indexes defined with ordering (ASC/DESC) defined on column
  2416. """
  2417. with connection.schema_editor() as editor:
  2418. editor.create_model(Author)
  2419. # The table doesn't have an index
  2420. self.assertNotIn('title', self.get_indexes(Author._meta.db_table))
  2421. index_name = 'author_name_idx'
  2422. # Add the index
  2423. index = Index(fields=['name', '-weight'], name=index_name)
  2424. with connection.schema_editor() as editor:
  2425. editor.add_index(Author, index)
  2426. if connection.features.supports_index_column_ordering:
  2427. self.assertIndexOrder(Author._meta.db_table, index_name, ['ASC', 'DESC'])
  2428. # Drop the index
  2429. with connection.schema_editor() as editor:
  2430. editor.remove_index(Author, index)
  2431. def test_indexes(self):
  2432. """
  2433. Tests creation/altering of indexes
  2434. """
  2435. # Create the table
  2436. with connection.schema_editor() as editor:
  2437. editor.create_model(Author)
  2438. editor.create_model(Book)
  2439. # Ensure the table is there and has the right index
  2440. self.assertIn(
  2441. "title",
  2442. self.get_indexes(Book._meta.db_table),
  2443. )
  2444. # Alter to remove the index
  2445. old_field = Book._meta.get_field("title")
  2446. new_field = CharField(max_length=100, db_index=False)
  2447. new_field.set_attributes_from_name("title")
  2448. with connection.schema_editor() as editor:
  2449. editor.alter_field(Book, old_field, new_field, strict=True)
  2450. # Ensure the table is there and has no index
  2451. self.assertNotIn(
  2452. "title",
  2453. self.get_indexes(Book._meta.db_table),
  2454. )
  2455. # Alter to re-add the index
  2456. new_field2 = Book._meta.get_field("title")
  2457. with connection.schema_editor() as editor:
  2458. editor.alter_field(Book, new_field, new_field2, strict=True)
  2459. # Ensure the table is there and has the index again
  2460. self.assertIn(
  2461. "title",
  2462. self.get_indexes(Book._meta.db_table),
  2463. )
  2464. # Add a unique column, verify that creates an implicit index
  2465. new_field3 = BookWithSlug._meta.get_field("slug")
  2466. with connection.schema_editor() as editor:
  2467. editor.add_field(Book, new_field3)
  2468. self.assertIn(
  2469. "slug",
  2470. self.get_uniques(Book._meta.db_table),
  2471. )
  2472. # Remove the unique, check the index goes with it
  2473. new_field4 = CharField(max_length=20, unique=False)
  2474. new_field4.set_attributes_from_name("slug")
  2475. with connection.schema_editor() as editor:
  2476. editor.alter_field(BookWithSlug, new_field3, new_field4, strict=True)
  2477. self.assertNotIn(
  2478. "slug",
  2479. self.get_uniques(Book._meta.db_table),
  2480. )
  2481. def test_text_field_with_db_index(self):
  2482. with connection.schema_editor() as editor:
  2483. editor.create_model(AuthorTextFieldWithIndex)
  2484. # The text_field index is present if the database supports it.
  2485. assertion = self.assertIn if connection.features.supports_index_on_text_field else self.assertNotIn
  2486. assertion('text_field', self.get_indexes(AuthorTextFieldWithIndex._meta.db_table))
  2487. def _index_expressions_wrappers(self):
  2488. index_expression = IndexExpression()
  2489. index_expression.set_wrapper_classes(connection)
  2490. return ', '.join([
  2491. wrapper_cls.__qualname__ for wrapper_cls in index_expression.wrapper_classes
  2492. ])
  2493. @skipUnlessDBFeature('supports_expression_indexes')
  2494. def test_func_index_multiple_wrapper_references(self):
  2495. index = Index(OrderBy(F('name').desc(), descending=True), name='name')
  2496. msg = (
  2497. "Multiple references to %s can't be used in an indexed expression."
  2498. % self._index_expressions_wrappers()
  2499. )
  2500. with connection.schema_editor() as editor:
  2501. with self.assertRaisesMessage(ValueError, msg):
  2502. editor.add_index(Author, index)
  2503. @skipUnlessDBFeature('supports_expression_indexes')
  2504. def test_func_index_invalid_topmost_expressions(self):
  2505. index = Index(Upper(F('name').desc()), name='name')
  2506. msg = (
  2507. '%s must be topmost expressions in an indexed expression.'
  2508. % self._index_expressions_wrappers()
  2509. )
  2510. with connection.schema_editor() as editor:
  2511. with self.assertRaisesMessage(ValueError, msg):
  2512. editor.add_index(Author, index)
  2513. @skipUnlessDBFeature('supports_expression_indexes')
  2514. def test_func_index(self):
  2515. with connection.schema_editor() as editor:
  2516. editor.create_model(Author)
  2517. index = Index(Lower('name').desc(), name='func_lower_idx')
  2518. # Add index.
  2519. with connection.schema_editor() as editor:
  2520. editor.add_index(Author, index)
  2521. sql = index.create_sql(Author, editor)
  2522. table = Author._meta.db_table
  2523. if connection.features.supports_index_column_ordering:
  2524. self.assertIndexOrder(table, index.name, ['DESC'])
  2525. # SQL contains a database function.
  2526. self.assertIs(sql.references_column(table, 'name'), True)
  2527. self.assertIn('LOWER(%s)' % editor.quote_name('name'), str(sql))
  2528. # Remove index.
  2529. with connection.schema_editor() as editor:
  2530. editor.remove_index(Author, index)
  2531. self.assertNotIn(index.name, self.get_constraints(table))
  2532. @skipUnlessDBFeature('supports_expression_indexes')
  2533. def test_func_index_f(self):
  2534. with connection.schema_editor() as editor:
  2535. editor.create_model(Tag)
  2536. index = Index('slug', F('title').desc(), name='func_f_idx')
  2537. # Add index.
  2538. with connection.schema_editor() as editor:
  2539. editor.add_index(Tag, index)
  2540. sql = index.create_sql(Tag, editor)
  2541. table = Tag._meta.db_table
  2542. self.assertIn(index.name, self.get_constraints(table))
  2543. if connection.features.supports_index_column_ordering:
  2544. self.assertIndexOrder(Tag._meta.db_table, index.name, ['ASC', 'DESC'])
  2545. # SQL contains columns.
  2546. self.assertIs(sql.references_column(table, 'slug'), True)
  2547. self.assertIs(sql.references_column(table, 'title'), True)
  2548. # Remove index.
  2549. with connection.schema_editor() as editor:
  2550. editor.remove_index(Tag, index)
  2551. self.assertNotIn(index.name, self.get_constraints(table))
  2552. @skipUnlessDBFeature('supports_expression_indexes')
  2553. def test_func_index_lookups(self):
  2554. with connection.schema_editor() as editor:
  2555. editor.create_model(Author)
  2556. with register_lookup(CharField, Lower), register_lookup(IntegerField, Abs):
  2557. index = Index(
  2558. F('name__lower'),
  2559. F('weight__abs'),
  2560. name='func_lower_abs_lookup_idx',
  2561. )
  2562. # Add index.
  2563. with connection.schema_editor() as editor:
  2564. editor.add_index(Author, index)
  2565. sql = index.create_sql(Author, editor)
  2566. table = Author._meta.db_table
  2567. self.assertIn(index.name, self.get_constraints(table))
  2568. # SQL contains columns.
  2569. self.assertIs(sql.references_column(table, 'name'), True)
  2570. self.assertIs(sql.references_column(table, 'weight'), True)
  2571. # Remove index.
  2572. with connection.schema_editor() as editor:
  2573. editor.remove_index(Author, index)
  2574. self.assertNotIn(index.name, self.get_constraints(table))
  2575. @skipUnlessDBFeature('supports_expression_indexes')
  2576. def test_composite_func_index(self):
  2577. with connection.schema_editor() as editor:
  2578. editor.create_model(Author)
  2579. index = Index(Lower('name'), Upper('name'), name='func_lower_upper_idx')
  2580. # Add index.
  2581. with connection.schema_editor() as editor:
  2582. editor.add_index(Author, index)
  2583. sql = index.create_sql(Author, editor)
  2584. table = Author._meta.db_table
  2585. self.assertIn(index.name, self.get_constraints(table))
  2586. # SQL contains database functions.
  2587. self.assertIs(sql.references_column(table, 'name'), True)
  2588. sql = str(sql)
  2589. self.assertIn('LOWER(%s)' % editor.quote_name('name'), sql)
  2590. self.assertIn('UPPER(%s)' % editor.quote_name('name'), sql)
  2591. self.assertLess(sql.index('LOWER'), sql.index('UPPER'))
  2592. # Remove index.
  2593. with connection.schema_editor() as editor:
  2594. editor.remove_index(Author, index)
  2595. self.assertNotIn(index.name, self.get_constraints(table))
  2596. @skipUnlessDBFeature('supports_expression_indexes')
  2597. def test_composite_func_index_field_and_expression(self):
  2598. with connection.schema_editor() as editor:
  2599. editor.create_model(Author)
  2600. editor.create_model(Book)
  2601. index = Index(
  2602. F('author').desc(),
  2603. Lower('title').asc(),
  2604. 'pub_date',
  2605. name='func_f_lower_field_idx',
  2606. )
  2607. # Add index.
  2608. with connection.schema_editor() as editor:
  2609. editor.add_index(Book, index)
  2610. sql = index.create_sql(Book, editor)
  2611. table = Book._meta.db_table
  2612. constraints = self.get_constraints(table)
  2613. if connection.features.supports_index_column_ordering:
  2614. self.assertIndexOrder(table, index.name, ['DESC', 'ASC', 'ASC'])
  2615. self.assertEqual(len(constraints[index.name]['columns']), 3)
  2616. self.assertEqual(constraints[index.name]['columns'][2], 'pub_date')
  2617. # SQL contains database functions and columns.
  2618. self.assertIs(sql.references_column(table, 'author_id'), True)
  2619. self.assertIs(sql.references_column(table, 'title'), True)
  2620. self.assertIs(sql.references_column(table, 'pub_date'), True)
  2621. self.assertIn('LOWER(%s)' % editor.quote_name('title'), str(sql))
  2622. # Remove index.
  2623. with connection.schema_editor() as editor:
  2624. editor.remove_index(Book, index)
  2625. self.assertNotIn(index.name, self.get_constraints(table))
  2626. @skipUnlessDBFeature('supports_expression_indexes')
  2627. @isolate_apps('schema')
  2628. def test_func_index_f_decimalfield(self):
  2629. class Node(Model):
  2630. value = DecimalField(max_digits=5, decimal_places=2)
  2631. class Meta:
  2632. app_label = 'schema'
  2633. with connection.schema_editor() as editor:
  2634. editor.create_model(Node)
  2635. index = Index(F('value'), name='func_f_decimalfield_idx')
  2636. # Add index.
  2637. with connection.schema_editor() as editor:
  2638. editor.add_index(Node, index)
  2639. sql = index.create_sql(Node, editor)
  2640. table = Node._meta.db_table
  2641. self.assertIn(index.name, self.get_constraints(table))
  2642. self.assertIs(sql.references_column(table, 'value'), True)
  2643. # SQL doesn't contain casting.
  2644. self.assertNotIn('CAST', str(sql))
  2645. # Remove index.
  2646. with connection.schema_editor() as editor:
  2647. editor.remove_index(Node, index)
  2648. self.assertNotIn(index.name, self.get_constraints(table))
  2649. @skipUnlessDBFeature('supports_expression_indexes')
  2650. def test_func_index_cast(self):
  2651. with connection.schema_editor() as editor:
  2652. editor.create_model(Author)
  2653. index = Index(Cast('weight', FloatField()), name='func_cast_idx')
  2654. # Add index.
  2655. with connection.schema_editor() as editor:
  2656. editor.add_index(Author, index)
  2657. sql = index.create_sql(Author, editor)
  2658. table = Author._meta.db_table
  2659. self.assertIn(index.name, self.get_constraints(table))
  2660. self.assertIs(sql.references_column(table, 'weight'), True)
  2661. # Remove index.
  2662. with connection.schema_editor() as editor:
  2663. editor.remove_index(Author, index)
  2664. self.assertNotIn(index.name, self.get_constraints(table))
  2665. @skipUnlessDBFeature('supports_expression_indexes')
  2666. def test_func_index_collate(self):
  2667. collation = connection.features.test_collations.get('non_default')
  2668. if not collation:
  2669. self.skipTest(
  2670. 'This backend does not support case-insensitive collations.'
  2671. )
  2672. with connection.schema_editor() as editor:
  2673. editor.create_model(Author)
  2674. editor.create_model(BookWithSlug)
  2675. index = Index(
  2676. Collate(F('title'), collation=collation).desc(),
  2677. Collate('slug', collation=collation),
  2678. name='func_collate_idx',
  2679. )
  2680. # Add index.
  2681. with connection.schema_editor() as editor:
  2682. editor.add_index(BookWithSlug, index)
  2683. sql = index.create_sql(BookWithSlug, editor)
  2684. table = Book._meta.db_table
  2685. self.assertIn(index.name, self.get_constraints(table))
  2686. if connection.features.supports_index_column_ordering:
  2687. self.assertIndexOrder(table, index.name, ['DESC', 'ASC'])
  2688. # SQL contains columns and a collation.
  2689. self.assertIs(sql.references_column(table, 'title'), True)
  2690. self.assertIs(sql.references_column(table, 'slug'), True)
  2691. self.assertIn('COLLATE %s' % editor.quote_name(collation), str(sql))
  2692. # Remove index.
  2693. with connection.schema_editor() as editor:
  2694. editor.remove_index(Book, index)
  2695. self.assertNotIn(index.name, self.get_constraints(table))
  2696. @skipUnlessDBFeature('supports_expression_indexes')
  2697. @skipIfDBFeature('collate_as_index_expression')
  2698. def test_func_index_collate_f_ordered(self):
  2699. collation = connection.features.test_collations.get('non_default')
  2700. if not collation:
  2701. self.skipTest(
  2702. 'This backend does not support case-insensitive collations.'
  2703. )
  2704. with connection.schema_editor() as editor:
  2705. editor.create_model(Author)
  2706. index = Index(
  2707. Collate(F('name').desc(), collation=collation),
  2708. name='func_collate_f_desc_idx',
  2709. )
  2710. # Add index.
  2711. with connection.schema_editor() as editor:
  2712. editor.add_index(Author, index)
  2713. sql = index.create_sql(Author, editor)
  2714. table = Author._meta.db_table
  2715. self.assertIn(index.name, self.get_constraints(table))
  2716. if connection.features.supports_index_column_ordering:
  2717. self.assertIndexOrder(table, index.name, ['DESC'])
  2718. # SQL contains columns and a collation.
  2719. self.assertIs(sql.references_column(table, 'name'), True)
  2720. self.assertIn('COLLATE %s' % editor.quote_name(collation), str(sql))
  2721. # Remove index.
  2722. with connection.schema_editor() as editor:
  2723. editor.remove_index(Author, index)
  2724. self.assertNotIn(index.name, self.get_constraints(table))
  2725. @skipUnlessDBFeature('supports_expression_indexes')
  2726. def test_func_index_calc(self):
  2727. with connection.schema_editor() as editor:
  2728. editor.create_model(Author)
  2729. index = Index(F('height') / (F('weight') + Value(5)), name='func_calc_idx')
  2730. # Add index.
  2731. with connection.schema_editor() as editor:
  2732. editor.add_index(Author, index)
  2733. sql = index.create_sql(Author, editor)
  2734. table = Author._meta.db_table
  2735. self.assertIn(index.name, self.get_constraints(table))
  2736. # SQL contains columns and expressions.
  2737. self.assertIs(sql.references_column(table, 'height'), True)
  2738. self.assertIs(sql.references_column(table, 'weight'), True)
  2739. sql = str(sql)
  2740. self.assertIs(
  2741. sql.index(editor.quote_name('height')) <
  2742. sql.index('/') <
  2743. sql.index(editor.quote_name('weight')) <
  2744. sql.index('+') <
  2745. sql.index('5'),
  2746. True,
  2747. )
  2748. # Remove index.
  2749. with connection.schema_editor() as editor:
  2750. editor.remove_index(Author, index)
  2751. self.assertNotIn(index.name, self.get_constraints(table))
  2752. @skipUnlessDBFeature('supports_expression_indexes', 'supports_json_field')
  2753. @isolate_apps('schema')
  2754. def test_func_index_json_key_transform(self):
  2755. class JSONModel(Model):
  2756. field = JSONField()
  2757. class Meta:
  2758. app_label = 'schema'
  2759. with connection.schema_editor() as editor:
  2760. editor.create_model(JSONModel)
  2761. self.isolated_local_models = [JSONModel]
  2762. index = Index('field__some_key', name='func_json_key_idx')
  2763. with connection.schema_editor() as editor:
  2764. editor.add_index(JSONModel, index)
  2765. sql = index.create_sql(JSONModel, editor)
  2766. table = JSONModel._meta.db_table
  2767. self.assertIn(index.name, self.get_constraints(table))
  2768. self.assertIs(sql.references_column(table, 'field'), True)
  2769. with connection.schema_editor() as editor:
  2770. editor.remove_index(JSONModel, index)
  2771. self.assertNotIn(index.name, self.get_constraints(table))
  2772. @skipUnlessDBFeature('supports_expression_indexes', 'supports_json_field')
  2773. @isolate_apps('schema')
  2774. def test_func_index_json_key_transform_cast(self):
  2775. class JSONModel(Model):
  2776. field = JSONField()
  2777. class Meta:
  2778. app_label = 'schema'
  2779. with connection.schema_editor() as editor:
  2780. editor.create_model(JSONModel)
  2781. self.isolated_local_models = [JSONModel]
  2782. index = Index(
  2783. Cast(KeyTextTransform('some_key', 'field'), IntegerField()),
  2784. name='func_json_key_cast_idx',
  2785. )
  2786. with connection.schema_editor() as editor:
  2787. editor.add_index(JSONModel, index)
  2788. sql = index.create_sql(JSONModel, editor)
  2789. table = JSONModel._meta.db_table
  2790. self.assertIn(index.name, self.get_constraints(table))
  2791. self.assertIs(sql.references_column(table, 'field'), True)
  2792. with connection.schema_editor() as editor:
  2793. editor.remove_index(JSONModel, index)
  2794. self.assertNotIn(index.name, self.get_constraints(table))
  2795. @skipIfDBFeature('supports_expression_indexes')
  2796. def test_func_index_unsupported(self):
  2797. # Index is ignored on databases that don't support indexes on
  2798. # expressions.
  2799. with connection.schema_editor() as editor:
  2800. editor.create_model(Author)
  2801. index = Index(F('name'), name='random_idx')
  2802. with connection.schema_editor() as editor, self.assertNumQueries(0):
  2803. self.assertIsNone(editor.add_index(Author, index))
  2804. self.assertIsNone(editor.remove_index(Author, index))
  2805. @skipUnlessDBFeature('supports_expression_indexes')
  2806. def test_func_index_nonexistent_field(self):
  2807. index = Index(Lower('nonexistent'), name='func_nonexistent_idx')
  2808. msg = (
  2809. "Cannot resolve keyword 'nonexistent' into field. Choices are: "
  2810. "height, id, name, uuid, weight"
  2811. )
  2812. with self.assertRaisesMessage(FieldError, msg):
  2813. with connection.schema_editor() as editor:
  2814. editor.add_index(Author, index)
  2815. @skipUnlessDBFeature('supports_expression_indexes')
  2816. def test_func_index_nondeterministic(self):
  2817. with connection.schema_editor() as editor:
  2818. editor.create_model(Author)
  2819. index = Index(Random(), name='func_random_idx')
  2820. with connection.schema_editor() as editor:
  2821. with self.assertRaises(DatabaseError):
  2822. editor.add_index(Author, index)
  2823. def test_primary_key(self):
  2824. """
  2825. Tests altering of the primary key
  2826. """
  2827. # Create the table
  2828. with connection.schema_editor() as editor:
  2829. editor.create_model(Tag)
  2830. # Ensure the table is there and has the right PK
  2831. self.assertEqual(self.get_primary_key(Tag._meta.db_table), 'id')
  2832. # Alter to change the PK
  2833. id_field = Tag._meta.get_field("id")
  2834. old_field = Tag._meta.get_field("slug")
  2835. new_field = SlugField(primary_key=True)
  2836. new_field.set_attributes_from_name("slug")
  2837. new_field.model = Tag
  2838. with connection.schema_editor() as editor:
  2839. editor.remove_field(Tag, id_field)
  2840. editor.alter_field(Tag, old_field, new_field)
  2841. # Ensure the PK changed
  2842. self.assertNotIn(
  2843. 'id',
  2844. self.get_indexes(Tag._meta.db_table),
  2845. )
  2846. self.assertEqual(self.get_primary_key(Tag._meta.db_table), 'slug')
  2847. def test_context_manager_exit(self):
  2848. """
  2849. Ensures transaction is correctly closed when an error occurs
  2850. inside a SchemaEditor context.
  2851. """
  2852. class SomeError(Exception):
  2853. pass
  2854. try:
  2855. with connection.schema_editor():
  2856. raise SomeError
  2857. except SomeError:
  2858. self.assertFalse(connection.in_atomic_block)
  2859. @skipIfDBFeature('can_rollback_ddl')
  2860. def test_unsupported_transactional_ddl_disallowed(self):
  2861. message = (
  2862. "Executing DDL statements while in a transaction on databases "
  2863. "that can't perform a rollback is prohibited."
  2864. )
  2865. with atomic(), connection.schema_editor() as editor:
  2866. with self.assertRaisesMessage(TransactionManagementError, message):
  2867. editor.execute(editor.sql_create_table % {'table': 'foo', 'definition': ''})
  2868. @skipUnlessDBFeature('supports_foreign_keys', 'indexes_foreign_keys')
  2869. def test_foreign_key_index_long_names_regression(self):
  2870. """
  2871. Regression test for #21497.
  2872. Only affects databases that supports foreign keys.
  2873. """
  2874. # Create the table
  2875. with connection.schema_editor() as editor:
  2876. editor.create_model(AuthorWithEvenLongerName)
  2877. editor.create_model(BookWithLongName)
  2878. # Find the properly shortened column name
  2879. column_name = connection.ops.quote_name("author_foreign_key_with_really_long_field_name_id")
  2880. column_name = column_name[1:-1].lower() # unquote, and, for Oracle, un-upcase
  2881. # Ensure the table is there and has an index on the column
  2882. self.assertIn(
  2883. column_name,
  2884. self.get_indexes(BookWithLongName._meta.db_table),
  2885. )
  2886. @skipUnlessDBFeature('supports_foreign_keys')
  2887. def test_add_foreign_key_long_names(self):
  2888. """
  2889. Regression test for #23009.
  2890. Only affects databases that supports foreign keys.
  2891. """
  2892. # Create the initial tables
  2893. with connection.schema_editor() as editor:
  2894. editor.create_model(AuthorWithEvenLongerName)
  2895. editor.create_model(BookWithLongName)
  2896. # Add a second FK, this would fail due to long ref name before the fix
  2897. new_field = ForeignKey(AuthorWithEvenLongerName, CASCADE, related_name="something")
  2898. new_field.set_attributes_from_name("author_other_really_long_named_i_mean_so_long_fk")
  2899. with connection.schema_editor() as editor:
  2900. editor.add_field(BookWithLongName, new_field)
  2901. @isolate_apps('schema')
  2902. @skipUnlessDBFeature('supports_foreign_keys')
  2903. def test_add_foreign_key_quoted_db_table(self):
  2904. class Author(Model):
  2905. class Meta:
  2906. db_table = '"table_author_double_quoted"'
  2907. app_label = 'schema'
  2908. class Book(Model):
  2909. author = ForeignKey(Author, CASCADE)
  2910. class Meta:
  2911. app_label = 'schema'
  2912. with connection.schema_editor() as editor:
  2913. editor.create_model(Author)
  2914. editor.create_model(Book)
  2915. if connection.vendor == 'mysql':
  2916. self.assertForeignKeyExists(Book, 'author_id', '"table_author_double_quoted"')
  2917. else:
  2918. self.assertForeignKeyExists(Book, 'author_id', 'table_author_double_quoted')
  2919. def test_add_foreign_object(self):
  2920. with connection.schema_editor() as editor:
  2921. editor.create_model(BookForeignObj)
  2922. new_field = ForeignObject(Author, on_delete=CASCADE, from_fields=['author_id'], to_fields=['id'])
  2923. new_field.set_attributes_from_name('author')
  2924. with connection.schema_editor() as editor:
  2925. editor.add_field(BookForeignObj, new_field)
  2926. def test_creation_deletion_reserved_names(self):
  2927. """
  2928. Tries creating a model's table, and then deleting it when it has a
  2929. SQL reserved name.
  2930. """
  2931. # Create the table
  2932. with connection.schema_editor() as editor:
  2933. try:
  2934. editor.create_model(Thing)
  2935. except OperationalError as e:
  2936. self.fail("Errors when applying initial migration for a model "
  2937. "with a table named after an SQL reserved word: %s" % e)
  2938. # The table is there
  2939. list(Thing.objects.all())
  2940. # Clean up that table
  2941. with connection.schema_editor() as editor:
  2942. editor.delete_model(Thing)
  2943. # The table is gone
  2944. with self.assertRaises(DatabaseError):
  2945. list(Thing.objects.all())
  2946. def test_remove_constraints_capital_letters(self):
  2947. """
  2948. #23065 - Constraint names must be quoted if they contain capital letters.
  2949. """
  2950. def get_field(*args, field_class=IntegerField, **kwargs):
  2951. kwargs['db_column'] = "CamelCase"
  2952. field = field_class(*args, **kwargs)
  2953. field.set_attributes_from_name("CamelCase")
  2954. return field
  2955. model = Author
  2956. field = get_field()
  2957. table = model._meta.db_table
  2958. column = field.column
  2959. identifier_converter = connection.introspection.identifier_converter
  2960. with connection.schema_editor() as editor:
  2961. editor.create_model(model)
  2962. editor.add_field(model, field)
  2963. constraint_name = 'CamelCaseIndex'
  2964. expected_constraint_name = identifier_converter(constraint_name)
  2965. editor.execute(
  2966. editor.sql_create_index % {
  2967. "table": editor.quote_name(table),
  2968. "name": editor.quote_name(constraint_name),
  2969. "using": "",
  2970. "columns": editor.quote_name(column),
  2971. "extra": "",
  2972. "condition": "",
  2973. "include": "",
  2974. }
  2975. )
  2976. self.assertIn(expected_constraint_name, self.get_constraints(model._meta.db_table))
  2977. editor.alter_field(model, get_field(db_index=True), field, strict=True)
  2978. self.assertNotIn(expected_constraint_name, self.get_constraints(model._meta.db_table))
  2979. constraint_name = 'CamelCaseUniqConstraint'
  2980. expected_constraint_name = identifier_converter(constraint_name)
  2981. editor.execute(editor._create_unique_sql(model, [field.column], constraint_name))
  2982. self.assertIn(expected_constraint_name, self.get_constraints(model._meta.db_table))
  2983. editor.alter_field(model, get_field(unique=True), field, strict=True)
  2984. self.assertNotIn(expected_constraint_name, self.get_constraints(model._meta.db_table))
  2985. if editor.sql_create_fk:
  2986. constraint_name = 'CamelCaseFKConstraint'
  2987. expected_constraint_name = identifier_converter(constraint_name)
  2988. editor.execute(
  2989. editor.sql_create_fk % {
  2990. "table": editor.quote_name(table),
  2991. "name": editor.quote_name(constraint_name),
  2992. "column": editor.quote_name(column),
  2993. "to_table": editor.quote_name(table),
  2994. "to_column": editor.quote_name(model._meta.auto_field.column),
  2995. "deferrable": connection.ops.deferrable_sql(),
  2996. }
  2997. )
  2998. self.assertIn(expected_constraint_name, self.get_constraints(model._meta.db_table))
  2999. editor.alter_field(model, get_field(Author, CASCADE, field_class=ForeignKey), field, strict=True)
  3000. self.assertNotIn(expected_constraint_name, self.get_constraints(model._meta.db_table))
  3001. def test_add_field_use_effective_default(self):
  3002. """
  3003. #23987 - effective_default() should be used as the field default when
  3004. adding a new field.
  3005. """
  3006. # Create the table
  3007. with connection.schema_editor() as editor:
  3008. editor.create_model(Author)
  3009. # Ensure there's no surname field
  3010. columns = self.column_classes(Author)
  3011. self.assertNotIn("surname", columns)
  3012. # Create a row
  3013. Author.objects.create(name='Anonymous1')
  3014. # Add new CharField to ensure default will be used from effective_default
  3015. new_field = CharField(max_length=15, blank=True)
  3016. new_field.set_attributes_from_name("surname")
  3017. with connection.schema_editor() as editor:
  3018. editor.add_field(Author, new_field)
  3019. # Ensure field was added with the right default
  3020. with connection.cursor() as cursor:
  3021. cursor.execute("SELECT surname FROM schema_author;")
  3022. item = cursor.fetchall()[0]
  3023. self.assertEqual(item[0], None if connection.features.interprets_empty_strings_as_nulls else '')
  3024. def test_add_field_default_dropped(self):
  3025. # Create the table
  3026. with connection.schema_editor() as editor:
  3027. editor.create_model(Author)
  3028. # Ensure there's no surname field
  3029. columns = self.column_classes(Author)
  3030. self.assertNotIn("surname", columns)
  3031. # Create a row
  3032. Author.objects.create(name='Anonymous1')
  3033. # Add new CharField with a default
  3034. new_field = CharField(max_length=15, blank=True, default='surname default')
  3035. new_field.set_attributes_from_name("surname")
  3036. with connection.schema_editor() as editor:
  3037. editor.add_field(Author, new_field)
  3038. # Ensure field was added with the right default
  3039. with connection.cursor() as cursor:
  3040. cursor.execute("SELECT surname FROM schema_author;")
  3041. item = cursor.fetchall()[0]
  3042. self.assertEqual(item[0], 'surname default')
  3043. # And that the default is no longer set in the database.
  3044. field = next(
  3045. f for f in connection.introspection.get_table_description(cursor, "schema_author")
  3046. if f.name == "surname"
  3047. )
  3048. if connection.features.can_introspect_default:
  3049. self.assertIsNone(field.default)
  3050. def test_add_field_default_nullable(self):
  3051. with connection.schema_editor() as editor:
  3052. editor.create_model(Author)
  3053. # Add new nullable CharField with a default.
  3054. new_field = CharField(max_length=15, blank=True, null=True, default='surname')
  3055. new_field.set_attributes_from_name('surname')
  3056. with connection.schema_editor() as editor:
  3057. editor.add_field(Author, new_field)
  3058. Author.objects.create(name='Anonymous1')
  3059. with connection.cursor() as cursor:
  3060. cursor.execute('SELECT surname FROM schema_author;')
  3061. item = cursor.fetchall()[0]
  3062. self.assertIsNone(item[0])
  3063. field = next(
  3064. f
  3065. for f in connection.introspection.get_table_description(
  3066. cursor,
  3067. 'schema_author',
  3068. )
  3069. if f.name == 'surname'
  3070. )
  3071. # Field is still nullable.
  3072. self.assertTrue(field.null_ok)
  3073. # The database default is no longer set.
  3074. if connection.features.can_introspect_default:
  3075. self.assertIn(field.default, ['NULL', None])
  3076. def test_alter_field_default_dropped(self):
  3077. # Create the table
  3078. with connection.schema_editor() as editor:
  3079. editor.create_model(Author)
  3080. # Create a row
  3081. Author.objects.create(name='Anonymous1')
  3082. self.assertIsNone(Author.objects.get().height)
  3083. old_field = Author._meta.get_field('height')
  3084. # The default from the new field is used in updating existing rows.
  3085. new_field = IntegerField(blank=True, default=42)
  3086. new_field.set_attributes_from_name('height')
  3087. with connection.schema_editor() as editor:
  3088. editor.alter_field(Author, old_field, new_field, strict=True)
  3089. self.assertEqual(Author.objects.get().height, 42)
  3090. # The database default should be removed.
  3091. with connection.cursor() as cursor:
  3092. field = next(
  3093. f for f in connection.introspection.get_table_description(cursor, "schema_author")
  3094. if f.name == "height"
  3095. )
  3096. if connection.features.can_introspect_default:
  3097. self.assertIsNone(field.default)
  3098. def test_alter_field_default_doesnt_perform_queries(self):
  3099. """
  3100. No queries are performed if a field default changes and the field's
  3101. not changing from null to non-null.
  3102. """
  3103. with connection.schema_editor() as editor:
  3104. editor.create_model(AuthorWithDefaultHeight)
  3105. old_field = AuthorWithDefaultHeight._meta.get_field('height')
  3106. new_default = old_field.default * 2
  3107. new_field = PositiveIntegerField(null=True, blank=True, default=new_default)
  3108. new_field.set_attributes_from_name('height')
  3109. with connection.schema_editor() as editor, self.assertNumQueries(0):
  3110. editor.alter_field(AuthorWithDefaultHeight, old_field, new_field, strict=True)
  3111. @skipUnlessDBFeature('supports_foreign_keys')
  3112. def test_alter_field_fk_attributes_noop(self):
  3113. """
  3114. No queries are performed when changing field attributes that don't
  3115. affect the schema.
  3116. """
  3117. with connection.schema_editor() as editor:
  3118. editor.create_model(Author)
  3119. editor.create_model(Book)
  3120. old_field = Book._meta.get_field('author')
  3121. new_field = ForeignKey(
  3122. Author,
  3123. blank=True,
  3124. editable=False,
  3125. error_messages={'invalid': 'error message'},
  3126. help_text='help text',
  3127. limit_choices_to={'limit': 'choice'},
  3128. on_delete=PROTECT,
  3129. related_name='related_name',
  3130. related_query_name='related_query_name',
  3131. validators=[lambda x: x],
  3132. verbose_name='verbose name',
  3133. )
  3134. new_field.set_attributes_from_name('author')
  3135. with connection.schema_editor() as editor, self.assertNumQueries(0):
  3136. editor.alter_field(Book, old_field, new_field, strict=True)
  3137. with connection.schema_editor() as editor, self.assertNumQueries(0):
  3138. editor.alter_field(Book, new_field, old_field, strict=True)
  3139. def test_add_textfield_unhashable_default(self):
  3140. # Create the table
  3141. with connection.schema_editor() as editor:
  3142. editor.create_model(Author)
  3143. # Create a row
  3144. Author.objects.create(name='Anonymous1')
  3145. # Create a field that has an unhashable default
  3146. new_field = TextField(default={})
  3147. new_field.set_attributes_from_name("info")
  3148. with connection.schema_editor() as editor:
  3149. editor.add_field(Author, new_field)
  3150. @unittest.skipUnless(connection.vendor == 'postgresql', "PostgreSQL specific")
  3151. def test_add_indexed_charfield(self):
  3152. field = CharField(max_length=255, db_index=True)
  3153. field.set_attributes_from_name('nom_de_plume')
  3154. with connection.schema_editor() as editor:
  3155. editor.create_model(Author)
  3156. editor.add_field(Author, field)
  3157. # Should create two indexes; one for like operator.
  3158. self.assertEqual(
  3159. self.get_constraints_for_column(Author, 'nom_de_plume'),
  3160. ['schema_author_nom_de_plume_7570a851', 'schema_author_nom_de_plume_7570a851_like'],
  3161. )
  3162. @unittest.skipUnless(connection.vendor == 'postgresql', "PostgreSQL specific")
  3163. def test_add_unique_charfield(self):
  3164. field = CharField(max_length=255, unique=True)
  3165. field.set_attributes_from_name('nom_de_plume')
  3166. with connection.schema_editor() as editor:
  3167. editor.create_model(Author)
  3168. editor.add_field(Author, field)
  3169. # Should create two indexes; one for like operator.
  3170. self.assertEqual(
  3171. self.get_constraints_for_column(Author, 'nom_de_plume'),
  3172. ['schema_author_nom_de_plume_7570a851_like', 'schema_author_nom_de_plume_key']
  3173. )
  3174. @unittest.skipUnless(connection.vendor == 'postgresql', "PostgreSQL specific")
  3175. def test_alter_field_add_index_to_charfield(self):
  3176. # Create the table and verify no initial indexes.
  3177. with connection.schema_editor() as editor:
  3178. editor.create_model(Author)
  3179. self.assertEqual(self.get_constraints_for_column(Author, 'name'), [])
  3180. # Alter to add db_index=True and create 2 indexes.
  3181. old_field = Author._meta.get_field('name')
  3182. new_field = CharField(max_length=255, db_index=True)
  3183. new_field.set_attributes_from_name('name')
  3184. with connection.schema_editor() as editor:
  3185. editor.alter_field(Author, old_field, new_field, strict=True)
  3186. self.assertEqual(
  3187. self.get_constraints_for_column(Author, 'name'),
  3188. ['schema_author_name_1fbc5617', 'schema_author_name_1fbc5617_like']
  3189. )
  3190. # Remove db_index=True to drop both indexes.
  3191. with connection.schema_editor() as editor:
  3192. editor.alter_field(Author, new_field, old_field, strict=True)
  3193. self.assertEqual(self.get_constraints_for_column(Author, 'name'), [])
  3194. @unittest.skipUnless(connection.vendor == 'postgresql', "PostgreSQL specific")
  3195. def test_alter_field_add_unique_to_charfield(self):
  3196. # Create the table and verify no initial indexes.
  3197. with connection.schema_editor() as editor:
  3198. editor.create_model(Author)
  3199. self.assertEqual(self.get_constraints_for_column(Author, 'name'), [])
  3200. # Alter to add unique=True and create 2 indexes.
  3201. old_field = Author._meta.get_field('name')
  3202. new_field = CharField(max_length=255, unique=True)
  3203. new_field.set_attributes_from_name('name')
  3204. with connection.schema_editor() as editor:
  3205. editor.alter_field(Author, old_field, new_field, strict=True)
  3206. self.assertEqual(
  3207. self.get_constraints_for_column(Author, 'name'),
  3208. ['schema_author_name_1fbc5617_like', 'schema_author_name_1fbc5617_uniq']
  3209. )
  3210. # Remove unique=True to drop both indexes.
  3211. with connection.schema_editor() as editor:
  3212. editor.alter_field(Author, new_field, old_field, strict=True)
  3213. self.assertEqual(self.get_constraints_for_column(Author, 'name'), [])
  3214. @unittest.skipUnless(connection.vendor == 'postgresql', "PostgreSQL specific")
  3215. def test_alter_field_add_index_to_textfield(self):
  3216. # Create the table and verify no initial indexes.
  3217. with connection.schema_editor() as editor:
  3218. editor.create_model(Note)
  3219. self.assertEqual(self.get_constraints_for_column(Note, 'info'), [])
  3220. # Alter to add db_index=True and create 2 indexes.
  3221. old_field = Note._meta.get_field('info')
  3222. new_field = TextField(db_index=True)
  3223. new_field.set_attributes_from_name('info')
  3224. with connection.schema_editor() as editor:
  3225. editor.alter_field(Note, old_field, new_field, strict=True)
  3226. self.assertEqual(
  3227. self.get_constraints_for_column(Note, 'info'),
  3228. ['schema_note_info_4b0ea695', 'schema_note_info_4b0ea695_like']
  3229. )
  3230. # Remove db_index=True to drop both indexes.
  3231. with connection.schema_editor() as editor:
  3232. editor.alter_field(Note, new_field, old_field, strict=True)
  3233. self.assertEqual(self.get_constraints_for_column(Note, 'info'), [])
  3234. @unittest.skipUnless(connection.vendor == 'postgresql', "PostgreSQL specific")
  3235. def test_alter_field_add_unique_to_charfield_with_db_index(self):
  3236. # Create the table and verify initial indexes.
  3237. with connection.schema_editor() as editor:
  3238. editor.create_model(BookWithoutAuthor)
  3239. self.assertEqual(
  3240. self.get_constraints_for_column(BookWithoutAuthor, 'title'),
  3241. ['schema_book_title_2dfb2dff', 'schema_book_title_2dfb2dff_like']
  3242. )
  3243. # Alter to add unique=True (should replace the index)
  3244. old_field = BookWithoutAuthor._meta.get_field('title')
  3245. new_field = CharField(max_length=100, db_index=True, unique=True)
  3246. new_field.set_attributes_from_name('title')
  3247. with connection.schema_editor() as editor:
  3248. editor.alter_field(BookWithoutAuthor, old_field, new_field, strict=True)
  3249. self.assertEqual(
  3250. self.get_constraints_for_column(BookWithoutAuthor, 'title'),
  3251. ['schema_book_title_2dfb2dff_like', 'schema_book_title_2dfb2dff_uniq']
  3252. )
  3253. # Alter to remove unique=True (should drop unique index)
  3254. new_field2 = CharField(max_length=100, db_index=True)
  3255. new_field2.set_attributes_from_name('title')
  3256. with connection.schema_editor() as editor:
  3257. editor.alter_field(BookWithoutAuthor, new_field, new_field2, strict=True)
  3258. self.assertEqual(
  3259. self.get_constraints_for_column(BookWithoutAuthor, 'title'),
  3260. ['schema_book_title_2dfb2dff', 'schema_book_title_2dfb2dff_like']
  3261. )
  3262. @unittest.skipUnless(connection.vendor == 'postgresql', "PostgreSQL specific")
  3263. def test_alter_field_remove_unique_and_db_index_from_charfield(self):
  3264. # Create the table and verify initial indexes.
  3265. with connection.schema_editor() as editor:
  3266. editor.create_model(BookWithoutAuthor)
  3267. self.assertEqual(
  3268. self.get_constraints_for_column(BookWithoutAuthor, 'title'),
  3269. ['schema_book_title_2dfb2dff', 'schema_book_title_2dfb2dff_like']
  3270. )
  3271. # Alter to add unique=True (should replace the index)
  3272. old_field = BookWithoutAuthor._meta.get_field('title')
  3273. new_field = CharField(max_length=100, db_index=True, unique=True)
  3274. new_field.set_attributes_from_name('title')
  3275. with connection.schema_editor() as editor:
  3276. editor.alter_field(BookWithoutAuthor, old_field, new_field, strict=True)
  3277. self.assertEqual(
  3278. self.get_constraints_for_column(BookWithoutAuthor, 'title'),
  3279. ['schema_book_title_2dfb2dff_like', 'schema_book_title_2dfb2dff_uniq']
  3280. )
  3281. # Alter to remove both unique=True and db_index=True (should drop all indexes)
  3282. new_field2 = CharField(max_length=100)
  3283. new_field2.set_attributes_from_name('title')
  3284. with connection.schema_editor() as editor:
  3285. editor.alter_field(BookWithoutAuthor, new_field, new_field2, strict=True)
  3286. self.assertEqual(self.get_constraints_for_column(BookWithoutAuthor, 'title'), [])
  3287. @unittest.skipUnless(connection.vendor == 'postgresql', "PostgreSQL specific")
  3288. def test_alter_field_swap_unique_and_db_index_with_charfield(self):
  3289. # Create the table and verify initial indexes.
  3290. with connection.schema_editor() as editor:
  3291. editor.create_model(BookWithoutAuthor)
  3292. self.assertEqual(
  3293. self.get_constraints_for_column(BookWithoutAuthor, 'title'),
  3294. ['schema_book_title_2dfb2dff', 'schema_book_title_2dfb2dff_like']
  3295. )
  3296. # Alter to set unique=True and remove db_index=True (should replace the index)
  3297. old_field = BookWithoutAuthor._meta.get_field('title')
  3298. new_field = CharField(max_length=100, unique=True)
  3299. new_field.set_attributes_from_name('title')
  3300. with connection.schema_editor() as editor:
  3301. editor.alter_field(BookWithoutAuthor, old_field, new_field, strict=True)
  3302. self.assertEqual(
  3303. self.get_constraints_for_column(BookWithoutAuthor, 'title'),
  3304. ['schema_book_title_2dfb2dff_like', 'schema_book_title_2dfb2dff_uniq']
  3305. )
  3306. # Alter to set db_index=True and remove unique=True (should restore index)
  3307. new_field2 = CharField(max_length=100, db_index=True)
  3308. new_field2.set_attributes_from_name('title')
  3309. with connection.schema_editor() as editor:
  3310. editor.alter_field(BookWithoutAuthor, new_field, new_field2, strict=True)
  3311. self.assertEqual(
  3312. self.get_constraints_for_column(BookWithoutAuthor, 'title'),
  3313. ['schema_book_title_2dfb2dff', 'schema_book_title_2dfb2dff_like']
  3314. )
  3315. @unittest.skipUnless(connection.vendor == 'postgresql', "PostgreSQL specific")
  3316. def test_alter_field_add_db_index_to_charfield_with_unique(self):
  3317. # Create the table and verify initial indexes.
  3318. with connection.schema_editor() as editor:
  3319. editor.create_model(Tag)
  3320. self.assertEqual(
  3321. self.get_constraints_for_column(Tag, 'slug'),
  3322. ['schema_tag_slug_2c418ba3_like', 'schema_tag_slug_key']
  3323. )
  3324. # Alter to add db_index=True
  3325. old_field = Tag._meta.get_field('slug')
  3326. new_field = SlugField(db_index=True, unique=True)
  3327. new_field.set_attributes_from_name('slug')
  3328. with connection.schema_editor() as editor:
  3329. editor.alter_field(Tag, old_field, new_field, strict=True)
  3330. self.assertEqual(
  3331. self.get_constraints_for_column(Tag, 'slug'),
  3332. ['schema_tag_slug_2c418ba3_like', 'schema_tag_slug_key']
  3333. )
  3334. # Alter to remove db_index=True
  3335. new_field2 = SlugField(unique=True)
  3336. new_field2.set_attributes_from_name('slug')
  3337. with connection.schema_editor() as editor:
  3338. editor.alter_field(Tag, new_field, new_field2, strict=True)
  3339. self.assertEqual(
  3340. self.get_constraints_for_column(Tag, 'slug'),
  3341. ['schema_tag_slug_2c418ba3_like', 'schema_tag_slug_key']
  3342. )
  3343. def test_alter_field_add_index_to_integerfield(self):
  3344. # Create the table and verify no initial indexes.
  3345. with connection.schema_editor() as editor:
  3346. editor.create_model(Author)
  3347. self.assertEqual(self.get_constraints_for_column(Author, 'weight'), [])
  3348. # Alter to add db_index=True and create index.
  3349. old_field = Author._meta.get_field('weight')
  3350. new_field = IntegerField(null=True, db_index=True)
  3351. new_field.set_attributes_from_name('weight')
  3352. with connection.schema_editor() as editor:
  3353. editor.alter_field(Author, old_field, new_field, strict=True)
  3354. self.assertEqual(self.get_constraints_for_column(Author, 'weight'), ['schema_author_weight_587740f9'])
  3355. # Remove db_index=True to drop index.
  3356. with connection.schema_editor() as editor:
  3357. editor.alter_field(Author, new_field, old_field, strict=True)
  3358. self.assertEqual(self.get_constraints_for_column(Author, 'weight'), [])
  3359. def test_alter_pk_with_self_referential_field(self):
  3360. """
  3361. Changing the primary key field name of a model with a self-referential
  3362. foreign key (#26384).
  3363. """
  3364. with connection.schema_editor() as editor:
  3365. editor.create_model(Node)
  3366. old_field = Node._meta.get_field('node_id')
  3367. new_field = AutoField(primary_key=True)
  3368. new_field.set_attributes_from_name('id')
  3369. with connection.schema_editor() as editor:
  3370. editor.alter_field(Node, old_field, new_field, strict=True)
  3371. self.assertForeignKeyExists(Node, 'parent_id', Node._meta.db_table)
  3372. @mock.patch('django.db.backends.base.schema.datetime')
  3373. @mock.patch('django.db.backends.base.schema.timezone')
  3374. def test_add_datefield_and_datetimefield_use_effective_default(self, mocked_datetime, mocked_tz):
  3375. """
  3376. effective_default() should be used for DateField, DateTimeField, and
  3377. TimeField if auto_now or auto_now_add is set (#25005).
  3378. """
  3379. now = datetime.datetime(month=1, day=1, year=2000, hour=1, minute=1)
  3380. now_tz = datetime.datetime(month=1, day=1, year=2000, hour=1, minute=1, tzinfo=timezone.utc)
  3381. mocked_datetime.now = mock.MagicMock(return_value=now)
  3382. mocked_tz.now = mock.MagicMock(return_value=now_tz)
  3383. # Create the table
  3384. with connection.schema_editor() as editor:
  3385. editor.create_model(Author)
  3386. # Check auto_now/auto_now_add attributes are not defined
  3387. columns = self.column_classes(Author)
  3388. self.assertNotIn("dob_auto_now", columns)
  3389. self.assertNotIn("dob_auto_now_add", columns)
  3390. self.assertNotIn("dtob_auto_now", columns)
  3391. self.assertNotIn("dtob_auto_now_add", columns)
  3392. self.assertNotIn("tob_auto_now", columns)
  3393. self.assertNotIn("tob_auto_now_add", columns)
  3394. # Create a row
  3395. Author.objects.create(name='Anonymous1')
  3396. # Ensure fields were added with the correct defaults
  3397. dob_auto_now = DateField(auto_now=True)
  3398. dob_auto_now.set_attributes_from_name('dob_auto_now')
  3399. self.check_added_field_default(
  3400. editor, Author, dob_auto_now, 'dob_auto_now', now.date(),
  3401. cast_function=lambda x: x.date(),
  3402. )
  3403. dob_auto_now_add = DateField(auto_now_add=True)
  3404. dob_auto_now_add.set_attributes_from_name('dob_auto_now_add')
  3405. self.check_added_field_default(
  3406. editor, Author, dob_auto_now_add, 'dob_auto_now_add', now.date(),
  3407. cast_function=lambda x: x.date(),
  3408. )
  3409. dtob_auto_now = DateTimeField(auto_now=True)
  3410. dtob_auto_now.set_attributes_from_name('dtob_auto_now')
  3411. self.check_added_field_default(
  3412. editor, Author, dtob_auto_now, 'dtob_auto_now', now,
  3413. )
  3414. dt_tm_of_birth_auto_now_add = DateTimeField(auto_now_add=True)
  3415. dt_tm_of_birth_auto_now_add.set_attributes_from_name('dtob_auto_now_add')
  3416. self.check_added_field_default(
  3417. editor, Author, dt_tm_of_birth_auto_now_add, 'dtob_auto_now_add', now,
  3418. )
  3419. tob_auto_now = TimeField(auto_now=True)
  3420. tob_auto_now.set_attributes_from_name('tob_auto_now')
  3421. self.check_added_field_default(
  3422. editor, Author, tob_auto_now, 'tob_auto_now', now.time(),
  3423. cast_function=lambda x: x.time(),
  3424. )
  3425. tob_auto_now_add = TimeField(auto_now_add=True)
  3426. tob_auto_now_add.set_attributes_from_name('tob_auto_now_add')
  3427. self.check_added_field_default(
  3428. editor, Author, tob_auto_now_add, 'tob_auto_now_add', now.time(),
  3429. cast_function=lambda x: x.time(),
  3430. )
  3431. def test_namespaced_db_table_create_index_name(self):
  3432. """
  3433. Table names are stripped of their namespace/schema before being used to
  3434. generate index names.
  3435. """
  3436. with connection.schema_editor() as editor:
  3437. max_name_length = connection.ops.max_name_length() or 200
  3438. namespace = 'n' * max_name_length
  3439. table_name = 't' * max_name_length
  3440. namespaced_table_name = '"%s"."%s"' % (namespace, table_name)
  3441. self.assertEqual(
  3442. editor._create_index_name(table_name, []),
  3443. editor._create_index_name(namespaced_table_name, []),
  3444. )
  3445. @unittest.skipUnless(connection.vendor == 'oracle', 'Oracle specific db_table syntax')
  3446. def test_creation_with_db_table_double_quotes(self):
  3447. oracle_user = connection.creation._test_database_user()
  3448. class Student(Model):
  3449. name = CharField(max_length=30)
  3450. class Meta:
  3451. app_label = 'schema'
  3452. apps = new_apps
  3453. db_table = '"%s"."DJANGO_STUDENT_TABLE"' % oracle_user
  3454. class Document(Model):
  3455. name = CharField(max_length=30)
  3456. students = ManyToManyField(Student)
  3457. class Meta:
  3458. app_label = 'schema'
  3459. apps = new_apps
  3460. db_table = '"%s"."DJANGO_DOCUMENT_TABLE"' % oracle_user
  3461. self.local_models = [Student, Document]
  3462. with connection.schema_editor() as editor:
  3463. editor.create_model(Student)
  3464. editor.create_model(Document)
  3465. doc = Document.objects.create(name='Test Name')
  3466. student = Student.objects.create(name='Some man')
  3467. doc.students.add(student)
  3468. @isolate_apps('schema')
  3469. @unittest.skipUnless(connection.vendor == 'postgresql', 'PostgreSQL specific db_table syntax.')
  3470. def test_namespaced_db_table_foreign_key_reference(self):
  3471. with connection.cursor() as cursor:
  3472. cursor.execute('CREATE SCHEMA django_schema_tests')
  3473. def delete_schema():
  3474. with connection.cursor() as cursor:
  3475. cursor.execute('DROP SCHEMA django_schema_tests CASCADE')
  3476. self.addCleanup(delete_schema)
  3477. class Author(Model):
  3478. class Meta:
  3479. app_label = 'schema'
  3480. class Book(Model):
  3481. class Meta:
  3482. app_label = 'schema'
  3483. db_table = '"django_schema_tests"."schema_book"'
  3484. author = ForeignKey(Author, CASCADE)
  3485. author.set_attributes_from_name('author')
  3486. with connection.schema_editor() as editor:
  3487. editor.create_model(Author)
  3488. editor.create_model(Book)
  3489. editor.add_field(Book, author)
  3490. def test_rename_table_renames_deferred_sql_references(self):
  3491. atomic_rename = connection.features.supports_atomic_references_rename
  3492. with connection.schema_editor(atomic=atomic_rename) as editor:
  3493. editor.create_model(Author)
  3494. editor.create_model(Book)
  3495. editor.alter_db_table(Author, 'schema_author', 'schema_renamed_author')
  3496. editor.alter_db_table(Author, 'schema_book', 'schema_renamed_book')
  3497. self.assertGreater(len(editor.deferred_sql), 0)
  3498. for statement in editor.deferred_sql:
  3499. self.assertIs(statement.references_table('schema_author'), False)
  3500. self.assertIs(statement.references_table('schema_book'), False)
  3501. def test_rename_column_renames_deferred_sql_references(self):
  3502. with connection.schema_editor() as editor:
  3503. editor.create_model(Author)
  3504. editor.create_model(Book)
  3505. old_title = Book._meta.get_field('title')
  3506. new_title = CharField(max_length=100, db_index=True)
  3507. new_title.set_attributes_from_name('renamed_title')
  3508. editor.alter_field(Book, old_title, new_title)
  3509. old_author = Book._meta.get_field('author')
  3510. new_author = ForeignKey(Author, CASCADE)
  3511. new_author.set_attributes_from_name('renamed_author')
  3512. editor.alter_field(Book, old_author, new_author)
  3513. self.assertGreater(len(editor.deferred_sql), 0)
  3514. for statement in editor.deferred_sql:
  3515. self.assertIs(statement.references_column('book', 'title'), False)
  3516. self.assertIs(statement.references_column('book', 'author_id'), False)
  3517. @isolate_apps('schema')
  3518. def test_referenced_field_without_constraint_rename_inside_atomic_block(self):
  3519. """
  3520. Foreign keys without database level constraint don't prevent the field
  3521. they reference from being renamed in an atomic block.
  3522. """
  3523. class Foo(Model):
  3524. field = CharField(max_length=255, unique=True)
  3525. class Meta:
  3526. app_label = 'schema'
  3527. class Bar(Model):
  3528. foo = ForeignKey(Foo, CASCADE, to_field='field', db_constraint=False)
  3529. class Meta:
  3530. app_label = 'schema'
  3531. self.isolated_local_models = [Foo, Bar]
  3532. with connection.schema_editor() as editor:
  3533. editor.create_model(Foo)
  3534. editor.create_model(Bar)
  3535. new_field = CharField(max_length=255, unique=True)
  3536. new_field.set_attributes_from_name('renamed')
  3537. with connection.schema_editor(atomic=True) as editor:
  3538. editor.alter_field(Foo, Foo._meta.get_field('field'), new_field)
  3539. @isolate_apps('schema')
  3540. def test_referenced_table_without_constraint_rename_inside_atomic_block(self):
  3541. """
  3542. Foreign keys without database level constraint don't prevent the table
  3543. they reference from being renamed in an atomic block.
  3544. """
  3545. class Foo(Model):
  3546. field = CharField(max_length=255, unique=True)
  3547. class Meta:
  3548. app_label = 'schema'
  3549. class Bar(Model):
  3550. foo = ForeignKey(Foo, CASCADE, to_field='field', db_constraint=False)
  3551. class Meta:
  3552. app_label = 'schema'
  3553. self.isolated_local_models = [Foo, Bar]
  3554. with connection.schema_editor() as editor:
  3555. editor.create_model(Foo)
  3556. editor.create_model(Bar)
  3557. new_field = CharField(max_length=255, unique=True)
  3558. new_field.set_attributes_from_name('renamed')
  3559. with connection.schema_editor(atomic=True) as editor:
  3560. editor.alter_db_table(Foo, Foo._meta.db_table, 'renamed_table')
  3561. Foo._meta.db_table = 'renamed_table'
  3562. @isolate_apps('schema')
  3563. @skipUnlessDBFeature('supports_collation_on_charfield')
  3564. def test_db_collation_charfield(self):
  3565. collation = connection.features.test_collations.get('non_default')
  3566. if not collation:
  3567. self.skipTest('Language collations are not supported.')
  3568. class Foo(Model):
  3569. field = CharField(max_length=255, db_collation=collation)
  3570. class Meta:
  3571. app_label = 'schema'
  3572. self.isolated_local_models = [Foo]
  3573. with connection.schema_editor() as editor:
  3574. editor.create_model(Foo)
  3575. self.assertEqual(
  3576. self.get_column_collation(Foo._meta.db_table, 'field'),
  3577. collation,
  3578. )
  3579. @isolate_apps('schema')
  3580. @skipUnlessDBFeature('supports_collation_on_textfield')
  3581. def test_db_collation_textfield(self):
  3582. collation = connection.features.test_collations.get('non_default')
  3583. if not collation:
  3584. self.skipTest('Language collations are not supported.')
  3585. class Foo(Model):
  3586. field = TextField(db_collation=collation)
  3587. class Meta:
  3588. app_label = 'schema'
  3589. self.isolated_local_models = [Foo]
  3590. with connection.schema_editor() as editor:
  3591. editor.create_model(Foo)
  3592. self.assertEqual(
  3593. self.get_column_collation(Foo._meta.db_table, 'field'),
  3594. collation,
  3595. )
  3596. @skipUnlessDBFeature('supports_collation_on_charfield')
  3597. def test_add_field_db_collation(self):
  3598. collation = connection.features.test_collations.get('non_default')
  3599. if not collation:
  3600. self.skipTest('Language collations are not supported.')
  3601. with connection.schema_editor() as editor:
  3602. editor.create_model(Author)
  3603. new_field = CharField(max_length=255, db_collation=collation)
  3604. new_field.set_attributes_from_name('alias')
  3605. with connection.schema_editor() as editor:
  3606. editor.add_field(Author, new_field)
  3607. columns = self.column_classes(Author)
  3608. self.assertEqual(
  3609. columns['alias'][0],
  3610. connection.features.introspected_field_types['CharField'],
  3611. )
  3612. self.assertEqual(columns['alias'][1][8], collation)
  3613. @skipUnlessDBFeature('supports_collation_on_charfield')
  3614. def test_alter_field_db_collation(self):
  3615. collation = connection.features.test_collations.get('non_default')
  3616. if not collation:
  3617. self.skipTest('Language collations are not supported.')
  3618. with connection.schema_editor() as editor:
  3619. editor.create_model(Author)
  3620. old_field = Author._meta.get_field('name')
  3621. new_field = CharField(max_length=255, db_collation=collation)
  3622. new_field.set_attributes_from_name('name')
  3623. new_field.model = Author
  3624. with connection.schema_editor() as editor:
  3625. editor.alter_field(Author, old_field, new_field, strict=True)
  3626. self.assertEqual(
  3627. self.get_column_collation(Author._meta.db_table, 'name'),
  3628. collation,
  3629. )
  3630. with connection.schema_editor() as editor:
  3631. editor.alter_field(Author, new_field, old_field, strict=True)
  3632. self.assertIsNone(self.get_column_collation(Author._meta.db_table, 'name'))
  3633. @skipUnlessDBFeature('supports_collation_on_charfield')
  3634. def test_alter_field_type_and_db_collation(self):
  3635. collation = connection.features.test_collations.get('non_default')
  3636. if not collation:
  3637. self.skipTest('Language collations are not supported.')
  3638. with connection.schema_editor() as editor:
  3639. editor.create_model(Note)
  3640. old_field = Note._meta.get_field('info')
  3641. new_field = CharField(max_length=255, db_collation=collation)
  3642. new_field.set_attributes_from_name('info')
  3643. new_field.model = Note
  3644. with connection.schema_editor() as editor:
  3645. editor.alter_field(Note, old_field, new_field, strict=True)
  3646. columns = self.column_classes(Note)
  3647. self.assertEqual(
  3648. columns['info'][0],
  3649. connection.features.introspected_field_types['CharField'],
  3650. )
  3651. self.assertEqual(columns['info'][1][8], collation)
  3652. with connection.schema_editor() as editor:
  3653. editor.alter_field(Note, new_field, old_field, strict=True)
  3654. columns = self.column_classes(Note)
  3655. self.assertEqual(columns['info'][0], 'TextField')
  3656. self.assertIsNone(columns['info'][1][8])
  3657. @skipUnlessDBFeature(
  3658. 'supports_collation_on_charfield',
  3659. 'supports_non_deterministic_collations',
  3660. )
  3661. def test_ci_cs_db_collation(self):
  3662. cs_collation = connection.features.test_collations.get('cs')
  3663. ci_collation = connection.features.test_collations.get('ci')
  3664. try:
  3665. if connection.vendor == 'mysql':
  3666. cs_collation = 'latin1_general_cs'
  3667. elif connection.vendor == 'postgresql':
  3668. cs_collation = 'en-x-icu'
  3669. with connection.cursor() as cursor:
  3670. cursor.execute(
  3671. "CREATE COLLATION IF NOT EXISTS case_insensitive "
  3672. "(provider = icu, locale = 'und-u-ks-level2', "
  3673. "deterministic = false)"
  3674. )
  3675. ci_collation = 'case_insensitive'
  3676. # Create the table.
  3677. with connection.schema_editor() as editor:
  3678. editor.create_model(Author)
  3679. # Case-insensitive collation.
  3680. old_field = Author._meta.get_field('name')
  3681. new_field_ci = CharField(max_length=255, db_collation=ci_collation)
  3682. new_field_ci.set_attributes_from_name('name')
  3683. new_field_ci.model = Author
  3684. with connection.schema_editor() as editor:
  3685. editor.alter_field(Author, old_field, new_field_ci, strict=True)
  3686. Author.objects.create(name='ANDREW')
  3687. self.assertIs(Author.objects.filter(name='Andrew').exists(), True)
  3688. # Case-sensitive collation.
  3689. new_field_cs = CharField(max_length=255, db_collation=cs_collation)
  3690. new_field_cs.set_attributes_from_name('name')
  3691. new_field_cs.model = Author
  3692. with connection.schema_editor() as editor:
  3693. editor.alter_field(Author, new_field_ci, new_field_cs, strict=True)
  3694. self.assertIs(Author.objects.filter(name='Andrew').exists(), False)
  3695. finally:
  3696. if connection.vendor == 'postgresql':
  3697. with connection.cursor() as cursor:
  3698. cursor.execute('DROP COLLATION IF EXISTS case_insensitive')