tests.py 181 KB

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