tests.py 133 KB

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