tests.py 150 KB

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