tests.py 220 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401
  1. import datetime
  2. import itertools
  3. import unittest
  4. from copy import copy
  5. from unittest import mock
  6. from django.core.exceptions import FieldError
  7. from django.core.management.color import no_style
  8. from django.db import (
  9. DatabaseError,
  10. DataError,
  11. IntegrityError,
  12. OperationalError,
  13. connection,
  14. )
  15. from django.db.backends.utils import truncate_name
  16. from django.db.models import (
  17. CASCADE,
  18. PROTECT,
  19. AutoField,
  20. BigAutoField,
  21. BigIntegerField,
  22. BinaryField,
  23. BooleanField,
  24. CharField,
  25. CheckConstraint,
  26. DateField,
  27. DateTimeField,
  28. DecimalField,
  29. DurationField,
  30. F,
  31. FloatField,
  32. ForeignKey,
  33. ForeignObject,
  34. Index,
  35. IntegerField,
  36. JSONField,
  37. ManyToManyField,
  38. Model,
  39. OneToOneField,
  40. OrderBy,
  41. PositiveIntegerField,
  42. Q,
  43. SlugField,
  44. SmallAutoField,
  45. SmallIntegerField,
  46. TextField,
  47. TimeField,
  48. UniqueConstraint,
  49. UUIDField,
  50. Value,
  51. )
  52. from django.db.models.fields.json import KeyTextTransform
  53. from django.db.models.functions import Abs, Cast, Collate, Lower, Random, Upper
  54. from django.db.models.indexes import IndexExpression
  55. from django.db.transaction import TransactionManagementError, atomic
  56. from django.test import (
  57. TransactionTestCase,
  58. ignore_warnings,
  59. skipIfDBFeature,
  60. skipUnlessDBFeature,
  61. )
  62. from django.test.utils import CaptureQueriesContext, isolate_apps, register_lookup
  63. from django.utils.deprecation import RemovedInDjango51Warning
  64. from .fields import CustomManyToManyField, InheritedManyToManyField, MediumBlobField
  65. from .models import (
  66. Author,
  67. AuthorCharFieldWithIndex,
  68. AuthorTextFieldWithIndex,
  69. AuthorWithDefaultHeight,
  70. AuthorWithEvenLongerName,
  71. AuthorWithIndexedName,
  72. AuthorWithUniqueName,
  73. AuthorWithUniqueNameAndBirthday,
  74. Book,
  75. BookForeignObj,
  76. BookWeak,
  77. BookWithLongName,
  78. BookWithO2O,
  79. BookWithoutAuthor,
  80. BookWithSlug,
  81. IntegerPK,
  82. Node,
  83. Note,
  84. NoteRename,
  85. Tag,
  86. TagM2MTest,
  87. TagUniqueRename,
  88. Thing,
  89. UniqueTest,
  90. new_apps,
  91. )
  92. class SchemaTests(TransactionTestCase):
  93. """
  94. Tests for the schema-alteration code.
  95. Be aware that these tests are more liable than most to false results,
  96. as sometimes the code to check if a test has worked is almost as complex
  97. as the code it is testing.
  98. """
  99. available_apps = []
  100. models = [
  101. Author,
  102. AuthorCharFieldWithIndex,
  103. AuthorTextFieldWithIndex,
  104. AuthorWithDefaultHeight,
  105. AuthorWithEvenLongerName,
  106. Book,
  107. BookWeak,
  108. BookWithLongName,
  109. BookWithO2O,
  110. BookWithSlug,
  111. IntegerPK,
  112. Node,
  113. Note,
  114. Tag,
  115. TagM2MTest,
  116. TagUniqueRename,
  117. Thing,
  118. UniqueTest,
  119. ]
  120. # Utility functions
  121. def setUp(self):
  122. # local_models should contain test dependent model classes that will be
  123. # automatically removed from the app cache on test tear down.
  124. self.local_models = []
  125. # isolated_local_models contains models that are in test methods
  126. # decorated with @isolate_apps.
  127. self.isolated_local_models = []
  128. def tearDown(self):
  129. # Delete any tables made for our models
  130. self.delete_tables()
  131. new_apps.clear_cache()
  132. for model in new_apps.get_models():
  133. model._meta._expire_cache()
  134. if "schema" in new_apps.all_models:
  135. for model in self.local_models:
  136. for many_to_many in model._meta.many_to_many:
  137. through = many_to_many.remote_field.through
  138. if through and through._meta.auto_created:
  139. del new_apps.all_models["schema"][through._meta.model_name]
  140. del new_apps.all_models["schema"][model._meta.model_name]
  141. if self.isolated_local_models:
  142. with connection.schema_editor() as editor:
  143. for model in self.isolated_local_models:
  144. editor.delete_model(model)
  145. def delete_tables(self):
  146. "Deletes all model tables for our models for a clean test environment"
  147. converter = connection.introspection.identifier_converter
  148. with connection.schema_editor() as editor:
  149. connection.disable_constraint_checking()
  150. table_names = connection.introspection.table_names()
  151. if connection.features.ignores_table_name_case:
  152. table_names = [table_name.lower() for table_name in table_names]
  153. for model in itertools.chain(SchemaTests.models, self.local_models):
  154. tbl = converter(model._meta.db_table)
  155. if connection.features.ignores_table_name_case:
  156. tbl = tbl.lower()
  157. if tbl in table_names:
  158. editor.delete_model(model)
  159. table_names.remove(tbl)
  160. connection.enable_constraint_checking()
  161. def column_classes(self, model):
  162. with connection.cursor() as cursor:
  163. columns = {
  164. d[0]: (connection.introspection.get_field_type(d[1], d), d)
  165. for d in connection.introspection.get_table_description(
  166. cursor,
  167. model._meta.db_table,
  168. )
  169. }
  170. # SQLite has a different format for field_type
  171. for name, (type, desc) in columns.items():
  172. if isinstance(type, tuple):
  173. columns[name] = (type[0], desc)
  174. return columns
  175. def get_primary_key(self, table):
  176. with connection.cursor() as cursor:
  177. return connection.introspection.get_primary_key_column(cursor, table)
  178. def get_indexes(self, table):
  179. """
  180. Get the indexes on the table using a new cursor.
  181. """
  182. with connection.cursor() as cursor:
  183. return [
  184. c["columns"][0]
  185. for c in connection.introspection.get_constraints(
  186. cursor, table
  187. ).values()
  188. if c["index"] and len(c["columns"]) == 1
  189. ]
  190. def get_uniques(self, table):
  191. with connection.cursor() as cursor:
  192. return [
  193. c["columns"][0]
  194. for c in connection.introspection.get_constraints(
  195. cursor, table
  196. ).values()
  197. if c["unique"] and len(c["columns"]) == 1
  198. ]
  199. def get_constraints(self, table):
  200. """
  201. Get the constraints on a table using a new cursor.
  202. """
  203. with connection.cursor() as cursor:
  204. return connection.introspection.get_constraints(cursor, table)
  205. def get_constraints_for_column(self, model, column_name):
  206. constraints = self.get_constraints(model._meta.db_table)
  207. constraints_for_column = []
  208. for name, details in constraints.items():
  209. if details["columns"] == [column_name]:
  210. constraints_for_column.append(name)
  211. return sorted(constraints_for_column)
  212. def check_added_field_default(
  213. self,
  214. schema_editor,
  215. model,
  216. field,
  217. field_name,
  218. expected_default,
  219. cast_function=None,
  220. ):
  221. with connection.cursor() as cursor:
  222. schema_editor.add_field(model, field)
  223. cursor.execute(
  224. "SELECT {} FROM {};".format(field_name, model._meta.db_table)
  225. )
  226. database_default = cursor.fetchall()[0][0]
  227. if cast_function and type(database_default) != type(expected_default):
  228. database_default = cast_function(database_default)
  229. self.assertEqual(database_default, expected_default)
  230. def get_constraints_count(self, table, column, fk_to):
  231. """
  232. Return a dict with keys 'fks', 'uniques, and 'indexes' indicating the
  233. number of foreign keys, unique constraints, and indexes on
  234. `table`.`column`. The `fk_to` argument is a 2-tuple specifying the
  235. expected foreign key relationship's (table, column).
  236. """
  237. with connection.cursor() as cursor:
  238. constraints = connection.introspection.get_constraints(cursor, table)
  239. counts = {"fks": 0, "uniques": 0, "indexes": 0}
  240. for c in constraints.values():
  241. if c["columns"] == [column]:
  242. if c["foreign_key"] == fk_to:
  243. counts["fks"] += 1
  244. if c["unique"]:
  245. counts["uniques"] += 1
  246. elif c["index"]:
  247. counts["indexes"] += 1
  248. return counts
  249. def get_column_collation(self, table, column):
  250. with connection.cursor() as cursor:
  251. return next(
  252. f.collation
  253. for f in connection.introspection.get_table_description(cursor, table)
  254. if f.name == column
  255. )
  256. def get_column_comment(self, table, column):
  257. with connection.cursor() as cursor:
  258. return next(
  259. f.comment
  260. for f in connection.introspection.get_table_description(cursor, table)
  261. if f.name == column
  262. )
  263. def get_table_comment(self, table):
  264. with connection.cursor() as cursor:
  265. return next(
  266. t.comment
  267. for t in connection.introspection.get_table_list(cursor)
  268. if t.name == table
  269. )
  270. def assert_column_comment_not_exists(self, table, column):
  271. with connection.cursor() as cursor:
  272. columns = connection.introspection.get_table_description(cursor, table)
  273. self.assertFalse(any([c.name == column and c.comment for c in columns]))
  274. def assertIndexOrder(self, table, index, order):
  275. constraints = self.get_constraints(table)
  276. self.assertIn(index, constraints)
  277. index_orders = constraints[index]["orders"]
  278. self.assertTrue(
  279. all(val == expected for val, expected in zip(index_orders, order))
  280. )
  281. def assertForeignKeyExists(self, model, column, expected_fk_table, field="id"):
  282. """
  283. Fail if the FK constraint on `model.Meta.db_table`.`column` to
  284. `expected_fk_table`.id doesn't exist.
  285. """
  286. if not connection.features.can_introspect_foreign_keys:
  287. return
  288. constraints = self.get_constraints(model._meta.db_table)
  289. constraint_fk = None
  290. for details in constraints.values():
  291. if details["columns"] == [column] and details["foreign_key"]:
  292. constraint_fk = details["foreign_key"]
  293. break
  294. self.assertEqual(constraint_fk, (expected_fk_table, field))
  295. def assertForeignKeyNotExists(self, model, column, expected_fk_table):
  296. if not connection.features.can_introspect_foreign_keys:
  297. return
  298. with self.assertRaises(AssertionError):
  299. self.assertForeignKeyExists(model, column, expected_fk_table)
  300. # Tests
  301. def test_creation_deletion(self):
  302. """
  303. Tries creating a model's table, and then deleting it.
  304. """
  305. with connection.schema_editor() as editor:
  306. # Create the table
  307. editor.create_model(Author)
  308. # The table is there
  309. list(Author.objects.all())
  310. # Clean up that table
  311. editor.delete_model(Author)
  312. # No deferred SQL should be left over.
  313. self.assertEqual(editor.deferred_sql, [])
  314. # The table is gone
  315. with self.assertRaises(DatabaseError):
  316. list(Author.objects.all())
  317. @skipUnlessDBFeature("supports_foreign_keys")
  318. def test_fk(self):
  319. "Creating tables out of FK order, then repointing, works"
  320. # Create the table
  321. with connection.schema_editor() as editor:
  322. editor.create_model(Book)
  323. editor.create_model(Author)
  324. editor.create_model(Tag)
  325. # Initial tables are there
  326. list(Author.objects.all())
  327. list(Book.objects.all())
  328. # Make sure the FK constraint is present
  329. with self.assertRaises(IntegrityError):
  330. Book.objects.create(
  331. author_id=1,
  332. title="Much Ado About Foreign Keys",
  333. pub_date=datetime.datetime.now(),
  334. )
  335. # Repoint the FK constraint
  336. old_field = Book._meta.get_field("author")
  337. new_field = ForeignKey(Tag, CASCADE)
  338. new_field.set_attributes_from_name("author")
  339. with connection.schema_editor() as editor:
  340. editor.alter_field(Book, old_field, new_field, strict=True)
  341. self.assertForeignKeyExists(Book, "author_id", "schema_tag")
  342. @skipUnlessDBFeature("can_create_inline_fk")
  343. def test_inline_fk(self):
  344. # Create some tables.
  345. with connection.schema_editor() as editor:
  346. editor.create_model(Author)
  347. editor.create_model(Book)
  348. editor.create_model(Note)
  349. self.assertForeignKeyNotExists(Note, "book_id", "schema_book")
  350. # Add a foreign key from one to the other.
  351. with connection.schema_editor() as editor:
  352. new_field = ForeignKey(Book, CASCADE)
  353. new_field.set_attributes_from_name("book")
  354. editor.add_field(Note, new_field)
  355. self.assertForeignKeyExists(Note, "book_id", "schema_book")
  356. # Creating a FK field with a constraint uses a single statement without
  357. # a deferred ALTER TABLE.
  358. self.assertFalse(
  359. [
  360. sql
  361. for sql in (str(statement) for statement in editor.deferred_sql)
  362. if sql.startswith("ALTER TABLE") and "ADD CONSTRAINT" in sql
  363. ]
  364. )
  365. @skipUnlessDBFeature("can_create_inline_fk")
  366. def test_add_inline_fk_update_data(self):
  367. with connection.schema_editor() as editor:
  368. editor.create_model(Node)
  369. # Add an inline foreign key and update data in the same transaction.
  370. new_field = ForeignKey(Node, CASCADE, related_name="new_fk", null=True)
  371. new_field.set_attributes_from_name("new_parent_fk")
  372. parent = Node.objects.create()
  373. with connection.schema_editor() as editor:
  374. editor.add_field(Node, new_field)
  375. editor.execute("UPDATE schema_node SET new_parent_fk_id = %s;", [parent.pk])
  376. assertIndex = (
  377. self.assertIn
  378. if connection.features.indexes_foreign_keys
  379. else self.assertNotIn
  380. )
  381. assertIndex("new_parent_fk_id", self.get_indexes(Node._meta.db_table))
  382. @skipUnlessDBFeature(
  383. "can_create_inline_fk",
  384. "allows_multiple_constraints_on_same_fields",
  385. )
  386. @isolate_apps("schema")
  387. def test_add_inline_fk_index_update_data(self):
  388. class Node(Model):
  389. class Meta:
  390. app_label = "schema"
  391. with connection.schema_editor() as editor:
  392. editor.create_model(Node)
  393. # Add an inline foreign key, update data, and an index in the same
  394. # transaction.
  395. new_field = ForeignKey(Node, CASCADE, related_name="new_fk", null=True)
  396. new_field.set_attributes_from_name("new_parent_fk")
  397. parent = Node.objects.create()
  398. with connection.schema_editor() as editor:
  399. editor.add_field(Node, new_field)
  400. Node._meta.add_field(new_field)
  401. editor.execute("UPDATE schema_node SET new_parent_fk_id = %s;", [parent.pk])
  402. editor.add_index(
  403. Node, Index(fields=["new_parent_fk"], name="new_parent_inline_fk_idx")
  404. )
  405. self.assertIn("new_parent_fk_id", self.get_indexes(Node._meta.db_table))
  406. @skipUnlessDBFeature("supports_foreign_keys")
  407. def test_char_field_with_db_index_to_fk(self):
  408. # Create the table
  409. with connection.schema_editor() as editor:
  410. editor.create_model(Author)
  411. editor.create_model(AuthorCharFieldWithIndex)
  412. # Change CharField to FK
  413. old_field = AuthorCharFieldWithIndex._meta.get_field("char_field")
  414. new_field = ForeignKey(Author, CASCADE, blank=True)
  415. new_field.set_attributes_from_name("char_field")
  416. with connection.schema_editor() as editor:
  417. editor.alter_field(
  418. AuthorCharFieldWithIndex, old_field, new_field, strict=True
  419. )
  420. self.assertForeignKeyExists(
  421. AuthorCharFieldWithIndex, "char_field_id", "schema_author"
  422. )
  423. @skipUnlessDBFeature("supports_foreign_keys")
  424. @skipUnlessDBFeature("supports_index_on_text_field")
  425. def test_text_field_with_db_index_to_fk(self):
  426. # Create the table
  427. with connection.schema_editor() as editor:
  428. editor.create_model(Author)
  429. editor.create_model(AuthorTextFieldWithIndex)
  430. # Change TextField to FK
  431. old_field = AuthorTextFieldWithIndex._meta.get_field("text_field")
  432. new_field = ForeignKey(Author, CASCADE, blank=True)
  433. new_field.set_attributes_from_name("text_field")
  434. with connection.schema_editor() as editor:
  435. editor.alter_field(
  436. AuthorTextFieldWithIndex, old_field, new_field, strict=True
  437. )
  438. self.assertForeignKeyExists(
  439. AuthorTextFieldWithIndex, "text_field_id", "schema_author"
  440. )
  441. @isolate_apps("schema")
  442. def test_char_field_pk_to_auto_field(self):
  443. class Foo(Model):
  444. id = CharField(max_length=255, primary_key=True)
  445. class Meta:
  446. app_label = "schema"
  447. with connection.schema_editor() as editor:
  448. editor.create_model(Foo)
  449. self.isolated_local_models = [Foo]
  450. old_field = Foo._meta.get_field("id")
  451. new_field = AutoField(primary_key=True)
  452. new_field.set_attributes_from_name("id")
  453. new_field.model = Foo
  454. with connection.schema_editor() as editor:
  455. editor.alter_field(Foo, old_field, new_field, strict=True)
  456. @skipUnlessDBFeature("supports_foreign_keys")
  457. def test_fk_to_proxy(self):
  458. "Creating a FK to a proxy model creates database constraints."
  459. class AuthorProxy(Author):
  460. class Meta:
  461. app_label = "schema"
  462. apps = new_apps
  463. proxy = True
  464. class AuthorRef(Model):
  465. author = ForeignKey(AuthorProxy, on_delete=CASCADE)
  466. class Meta:
  467. app_label = "schema"
  468. apps = new_apps
  469. self.local_models = [AuthorProxy, AuthorRef]
  470. # Create the table
  471. with connection.schema_editor() as editor:
  472. editor.create_model(Author)
  473. editor.create_model(AuthorRef)
  474. self.assertForeignKeyExists(AuthorRef, "author_id", "schema_author")
  475. @skipUnlessDBFeature("supports_foreign_keys", "can_introspect_foreign_keys")
  476. def test_fk_db_constraint(self):
  477. "The db_constraint parameter is respected"
  478. # Create the table
  479. with connection.schema_editor() as editor:
  480. editor.create_model(Tag)
  481. editor.create_model(Author)
  482. editor.create_model(BookWeak)
  483. # Initial tables are there
  484. list(Author.objects.all())
  485. list(Tag.objects.all())
  486. list(BookWeak.objects.all())
  487. self.assertForeignKeyNotExists(BookWeak, "author_id", "schema_author")
  488. # Make a db_constraint=False FK
  489. new_field = ForeignKey(Tag, CASCADE, db_constraint=False)
  490. new_field.set_attributes_from_name("tag")
  491. with connection.schema_editor() as editor:
  492. editor.add_field(Author, new_field)
  493. self.assertForeignKeyNotExists(Author, "tag_id", "schema_tag")
  494. # Alter to one with a constraint
  495. new_field2 = ForeignKey(Tag, CASCADE)
  496. new_field2.set_attributes_from_name("tag")
  497. with connection.schema_editor() as editor:
  498. editor.alter_field(Author, new_field, new_field2, strict=True)
  499. self.assertForeignKeyExists(Author, "tag_id", "schema_tag")
  500. # Alter to one without a constraint again
  501. new_field2 = ForeignKey(Tag, CASCADE)
  502. new_field2.set_attributes_from_name("tag")
  503. with connection.schema_editor() as editor:
  504. editor.alter_field(Author, new_field2, new_field, strict=True)
  505. self.assertForeignKeyNotExists(Author, "tag_id", "schema_tag")
  506. @isolate_apps("schema")
  507. def test_no_db_constraint_added_during_primary_key_change(self):
  508. """
  509. When a primary key that's pointed to by a ForeignKey with
  510. db_constraint=False is altered, a foreign key constraint isn't added.
  511. """
  512. class Author(Model):
  513. class Meta:
  514. app_label = "schema"
  515. class BookWeak(Model):
  516. author = ForeignKey(Author, CASCADE, db_constraint=False)
  517. class Meta:
  518. app_label = "schema"
  519. with connection.schema_editor() as editor:
  520. editor.create_model(Author)
  521. editor.create_model(BookWeak)
  522. self.assertForeignKeyNotExists(BookWeak, "author_id", "schema_author")
  523. old_field = Author._meta.get_field("id")
  524. new_field = BigAutoField(primary_key=True)
  525. new_field.model = Author
  526. new_field.set_attributes_from_name("id")
  527. # @isolate_apps() and inner models are needed to have the model
  528. # relations populated, otherwise this doesn't act as a regression test.
  529. self.assertEqual(len(new_field.model._meta.related_objects), 1)
  530. with connection.schema_editor() as editor:
  531. editor.alter_field(Author, old_field, new_field, strict=True)
  532. self.assertForeignKeyNotExists(BookWeak, "author_id", "schema_author")
  533. def _test_m2m_db_constraint(self, M2MFieldClass):
  534. class LocalAuthorWithM2M(Model):
  535. name = CharField(max_length=255)
  536. class Meta:
  537. app_label = "schema"
  538. apps = new_apps
  539. self.local_models = [LocalAuthorWithM2M]
  540. # Create the table
  541. with connection.schema_editor() as editor:
  542. editor.create_model(Tag)
  543. editor.create_model(LocalAuthorWithM2M)
  544. # Initial tables are there
  545. list(LocalAuthorWithM2M.objects.all())
  546. list(Tag.objects.all())
  547. # Make a db_constraint=False FK
  548. new_field = M2MFieldClass(Tag, related_name="authors", db_constraint=False)
  549. new_field.contribute_to_class(LocalAuthorWithM2M, "tags")
  550. # Add the field
  551. with connection.schema_editor() as editor:
  552. editor.add_field(LocalAuthorWithM2M, new_field)
  553. self.assertForeignKeyNotExists(
  554. new_field.remote_field.through, "tag_id", "schema_tag"
  555. )
  556. @skipUnlessDBFeature("supports_foreign_keys")
  557. def test_m2m_db_constraint(self):
  558. self._test_m2m_db_constraint(ManyToManyField)
  559. @skipUnlessDBFeature("supports_foreign_keys")
  560. def test_m2m_db_constraint_custom(self):
  561. self._test_m2m_db_constraint(CustomManyToManyField)
  562. @skipUnlessDBFeature("supports_foreign_keys")
  563. def test_m2m_db_constraint_inherited(self):
  564. self._test_m2m_db_constraint(InheritedManyToManyField)
  565. def test_add_field(self):
  566. """
  567. Tests adding fields to models
  568. """
  569. # Create the table
  570. with connection.schema_editor() as editor:
  571. editor.create_model(Author)
  572. # Ensure there's no age field
  573. columns = self.column_classes(Author)
  574. self.assertNotIn("age", columns)
  575. # Add the new field
  576. new_field = IntegerField(null=True)
  577. new_field.set_attributes_from_name("age")
  578. with CaptureQueriesContext(
  579. connection
  580. ) as ctx, connection.schema_editor() as editor:
  581. editor.add_field(Author, new_field)
  582. drop_default_sql = editor.sql_alter_column_no_default % {
  583. "column": editor.quote_name(new_field.name),
  584. }
  585. self.assertFalse(
  586. any(drop_default_sql in query["sql"] for query in ctx.captured_queries)
  587. )
  588. # Table is not rebuilt.
  589. self.assertIs(
  590. any("CREATE TABLE" in query["sql"] for query in ctx.captured_queries), False
  591. )
  592. self.assertIs(
  593. any("DROP TABLE" in query["sql"] for query in ctx.captured_queries), False
  594. )
  595. columns = self.column_classes(Author)
  596. self.assertEqual(
  597. columns["age"][0],
  598. connection.features.introspected_field_types["IntegerField"],
  599. )
  600. self.assertTrue(columns["age"][1][6])
  601. def test_add_field_remove_field(self):
  602. """
  603. Adding a field and removing it removes all deferred sql referring to it.
  604. """
  605. with connection.schema_editor() as editor:
  606. # Create a table with a unique constraint on the slug field.
  607. editor.create_model(Tag)
  608. # Remove the slug column.
  609. editor.remove_field(Tag, Tag._meta.get_field("slug"))
  610. self.assertEqual(editor.deferred_sql, [])
  611. def test_add_field_temp_default(self):
  612. """
  613. Tests adding fields to models with a temporary default
  614. """
  615. # Create the table
  616. with connection.schema_editor() as editor:
  617. editor.create_model(Author)
  618. # Ensure there's no age field
  619. columns = self.column_classes(Author)
  620. self.assertNotIn("age", columns)
  621. # Add some rows of data
  622. Author.objects.create(name="Andrew", height=30)
  623. Author.objects.create(name="Andrea")
  624. # Add a not-null field
  625. new_field = CharField(max_length=30, default="Godwin")
  626. new_field.set_attributes_from_name("surname")
  627. with connection.schema_editor() as editor:
  628. editor.add_field(Author, new_field)
  629. columns = self.column_classes(Author)
  630. self.assertEqual(
  631. columns["surname"][0],
  632. connection.features.introspected_field_types["CharField"],
  633. )
  634. self.assertEqual(
  635. columns["surname"][1][6],
  636. connection.features.interprets_empty_strings_as_nulls,
  637. )
  638. def test_add_field_temp_default_boolean(self):
  639. """
  640. Tests adding fields to models with a temporary default where
  641. the default is False. (#21783)
  642. """
  643. # Create the table
  644. with connection.schema_editor() as editor:
  645. editor.create_model(Author)
  646. # Ensure there's no age field
  647. columns = self.column_classes(Author)
  648. self.assertNotIn("age", columns)
  649. # Add some rows of data
  650. Author.objects.create(name="Andrew", height=30)
  651. Author.objects.create(name="Andrea")
  652. # Add a not-null field
  653. new_field = BooleanField(default=False)
  654. new_field.set_attributes_from_name("awesome")
  655. with connection.schema_editor() as editor:
  656. editor.add_field(Author, new_field)
  657. columns = self.column_classes(Author)
  658. # BooleanField are stored as TINYINT(1) on MySQL.
  659. field_type = columns["awesome"][0]
  660. self.assertEqual(
  661. field_type, connection.features.introspected_field_types["BooleanField"]
  662. )
  663. def test_add_field_default_transform(self):
  664. """
  665. Tests adding fields to models with a default that is not directly
  666. valid in the database (#22581)
  667. """
  668. class TestTransformField(IntegerField):
  669. # Weird field that saves the count of items in its value
  670. def get_default(self):
  671. return self.default
  672. def get_prep_value(self, value):
  673. if value is None:
  674. return 0
  675. return len(value)
  676. # Create the table
  677. with connection.schema_editor() as editor:
  678. editor.create_model(Author)
  679. # Add some rows of data
  680. Author.objects.create(name="Andrew", height=30)
  681. Author.objects.create(name="Andrea")
  682. # Add the field with a default it needs to cast (to string in this case)
  683. new_field = TestTransformField(default={1: 2})
  684. new_field.set_attributes_from_name("thing")
  685. with connection.schema_editor() as editor:
  686. editor.add_field(Author, new_field)
  687. # Ensure the field is there
  688. columns = self.column_classes(Author)
  689. field_type, field_info = columns["thing"]
  690. self.assertEqual(
  691. field_type, connection.features.introspected_field_types["IntegerField"]
  692. )
  693. # Make sure the values were transformed correctly
  694. self.assertEqual(Author.objects.extra(where=["thing = 1"]).count(), 2)
  695. def test_add_field_o2o_nullable(self):
  696. with connection.schema_editor() as editor:
  697. editor.create_model(Author)
  698. editor.create_model(Note)
  699. new_field = OneToOneField(Note, CASCADE, null=True)
  700. new_field.set_attributes_from_name("note")
  701. with connection.schema_editor() as editor:
  702. editor.add_field(Author, new_field)
  703. columns = self.column_classes(Author)
  704. self.assertIn("note_id", columns)
  705. self.assertTrue(columns["note_id"][1][6])
  706. def test_add_field_binary(self):
  707. """
  708. Tests binary fields get a sane default (#22851)
  709. """
  710. # Create the table
  711. with connection.schema_editor() as editor:
  712. editor.create_model(Author)
  713. # Add the new field
  714. new_field = BinaryField(blank=True)
  715. new_field.set_attributes_from_name("bits")
  716. with connection.schema_editor() as editor:
  717. editor.add_field(Author, new_field)
  718. columns = self.column_classes(Author)
  719. # MySQL annoyingly uses the same backend, so it'll come back as one of
  720. # these two types.
  721. self.assertIn(columns["bits"][0], ("BinaryField", "TextField"))
  722. def test_add_field_durationfield_with_default(self):
  723. with connection.schema_editor() as editor:
  724. editor.create_model(Author)
  725. new_field = DurationField(default=datetime.timedelta(minutes=10))
  726. new_field.set_attributes_from_name("duration")
  727. with connection.schema_editor() as editor:
  728. editor.add_field(Author, new_field)
  729. columns = self.column_classes(Author)
  730. self.assertEqual(
  731. columns["duration"][0],
  732. connection.features.introspected_field_types["DurationField"],
  733. )
  734. @unittest.skipUnless(connection.vendor == "mysql", "MySQL specific")
  735. def test_add_binaryfield_mediumblob(self):
  736. """
  737. Test adding a custom-sized binary field on MySQL (#24846).
  738. """
  739. # Create the table
  740. with connection.schema_editor() as editor:
  741. editor.create_model(Author)
  742. # Add the new field with default
  743. new_field = MediumBlobField(blank=True, default=b"123")
  744. new_field.set_attributes_from_name("bits")
  745. with connection.schema_editor() as editor:
  746. editor.add_field(Author, new_field)
  747. columns = self.column_classes(Author)
  748. # Introspection treats BLOBs as TextFields
  749. self.assertEqual(columns["bits"][0], "TextField")
  750. @isolate_apps("schema")
  751. def test_add_auto_field(self):
  752. class AddAutoFieldModel(Model):
  753. name = CharField(max_length=255, primary_key=True)
  754. class Meta:
  755. app_label = "schema"
  756. with connection.schema_editor() as editor:
  757. editor.create_model(AddAutoFieldModel)
  758. self.isolated_local_models = [AddAutoFieldModel]
  759. old_field = AddAutoFieldModel._meta.get_field("name")
  760. new_field = CharField(max_length=255)
  761. new_field.set_attributes_from_name("name")
  762. new_field.model = AddAutoFieldModel
  763. with connection.schema_editor() as editor:
  764. editor.alter_field(AddAutoFieldModel, old_field, new_field)
  765. new_auto_field = AutoField(primary_key=True)
  766. new_auto_field.set_attributes_from_name("id")
  767. new_auto_field.model = AddAutoFieldModel()
  768. with connection.schema_editor() as editor:
  769. editor.add_field(AddAutoFieldModel, new_auto_field)
  770. # Crashes on PostgreSQL when the GENERATED BY suffix is missing.
  771. AddAutoFieldModel.objects.create(name="test")
  772. def test_remove_field(self):
  773. with connection.schema_editor() as editor:
  774. editor.create_model(Author)
  775. with CaptureQueriesContext(connection) as ctx:
  776. editor.remove_field(Author, Author._meta.get_field("name"))
  777. columns = self.column_classes(Author)
  778. self.assertNotIn("name", columns)
  779. if getattr(connection.features, "can_alter_table_drop_column", True):
  780. # Table is not rebuilt.
  781. self.assertIs(
  782. any("CREATE TABLE" in query["sql"] for query in ctx.captured_queries),
  783. False,
  784. )
  785. self.assertIs(
  786. any("DROP TABLE" in query["sql"] for query in ctx.captured_queries),
  787. False,
  788. )
  789. def test_remove_indexed_field(self):
  790. with connection.schema_editor() as editor:
  791. editor.create_model(AuthorCharFieldWithIndex)
  792. with connection.schema_editor() as editor:
  793. editor.remove_field(
  794. AuthorCharFieldWithIndex,
  795. AuthorCharFieldWithIndex._meta.get_field("char_field"),
  796. )
  797. columns = self.column_classes(AuthorCharFieldWithIndex)
  798. self.assertNotIn("char_field", columns)
  799. def test_alter(self):
  800. """
  801. Tests simple altering of fields
  802. """
  803. # Create the table
  804. with connection.schema_editor() as editor:
  805. editor.create_model(Author)
  806. # Ensure the field is right to begin with
  807. columns = self.column_classes(Author)
  808. self.assertEqual(
  809. columns["name"][0],
  810. connection.features.introspected_field_types["CharField"],
  811. )
  812. self.assertEqual(
  813. bool(columns["name"][1][6]),
  814. bool(connection.features.interprets_empty_strings_as_nulls),
  815. )
  816. # Alter the name field to a TextField
  817. old_field = Author._meta.get_field("name")
  818. new_field = TextField(null=True)
  819. new_field.set_attributes_from_name("name")
  820. with connection.schema_editor() as editor:
  821. editor.alter_field(Author, old_field, new_field, strict=True)
  822. columns = self.column_classes(Author)
  823. self.assertEqual(columns["name"][0], "TextField")
  824. self.assertTrue(columns["name"][1][6])
  825. # Change nullability again
  826. new_field2 = TextField(null=False)
  827. new_field2.set_attributes_from_name("name")
  828. with connection.schema_editor() as editor:
  829. editor.alter_field(Author, new_field, new_field2, strict=True)
  830. columns = self.column_classes(Author)
  831. self.assertEqual(columns["name"][0], "TextField")
  832. self.assertEqual(
  833. bool(columns["name"][1][6]),
  834. bool(connection.features.interprets_empty_strings_as_nulls),
  835. )
  836. def test_alter_auto_field_to_integer_field(self):
  837. # Create the table
  838. with connection.schema_editor() as editor:
  839. editor.create_model(Author)
  840. # Change AutoField to IntegerField
  841. old_field = Author._meta.get_field("id")
  842. new_field = IntegerField(primary_key=True)
  843. new_field.set_attributes_from_name("id")
  844. new_field.model = Author
  845. with connection.schema_editor() as editor:
  846. editor.alter_field(Author, old_field, new_field, strict=True)
  847. # Now that ID is an IntegerField, the database raises an error if it
  848. # isn't provided.
  849. if not connection.features.supports_unspecified_pk:
  850. with self.assertRaises(DatabaseError):
  851. Author.objects.create()
  852. def test_alter_auto_field_to_char_field(self):
  853. # Create the table
  854. with connection.schema_editor() as editor:
  855. editor.create_model(Author)
  856. # Change AutoField to CharField
  857. old_field = Author._meta.get_field("id")
  858. new_field = CharField(primary_key=True, max_length=50)
  859. new_field.set_attributes_from_name("id")
  860. new_field.model = Author
  861. with connection.schema_editor() as editor:
  862. editor.alter_field(Author, old_field, new_field, strict=True)
  863. @isolate_apps("schema")
  864. def test_alter_auto_field_quoted_db_column(self):
  865. class Foo(Model):
  866. id = AutoField(primary_key=True, db_column='"quoted_id"')
  867. class Meta:
  868. app_label = "schema"
  869. with connection.schema_editor() as editor:
  870. editor.create_model(Foo)
  871. self.isolated_local_models = [Foo]
  872. old_field = Foo._meta.get_field("id")
  873. new_field = BigAutoField(primary_key=True)
  874. new_field.model = Foo
  875. new_field.db_column = '"quoted_id"'
  876. new_field.set_attributes_from_name("id")
  877. with connection.schema_editor() as editor:
  878. editor.alter_field(Foo, old_field, new_field, strict=True)
  879. Foo.objects.create()
  880. def test_alter_not_unique_field_to_primary_key(self):
  881. # Create the table.
  882. with connection.schema_editor() as editor:
  883. editor.create_model(Author)
  884. # Change UUIDField to primary key.
  885. old_field = Author._meta.get_field("uuid")
  886. new_field = UUIDField(primary_key=True)
  887. new_field.set_attributes_from_name("uuid")
  888. new_field.model = Author
  889. with connection.schema_editor() as editor:
  890. editor.remove_field(Author, Author._meta.get_field("id"))
  891. editor.alter_field(Author, old_field, new_field, strict=True)
  892. # Redundant unique constraint is not added.
  893. count = self.get_constraints_count(
  894. Author._meta.db_table,
  895. Author._meta.get_field("uuid").column,
  896. None,
  897. )
  898. self.assertLessEqual(count["uniques"], 1)
  899. @isolate_apps("schema")
  900. def test_alter_primary_key_quoted_db_table(self):
  901. class Foo(Model):
  902. class Meta:
  903. app_label = "schema"
  904. db_table = '"foo"'
  905. with connection.schema_editor() as editor:
  906. editor.create_model(Foo)
  907. self.isolated_local_models = [Foo]
  908. old_field = Foo._meta.get_field("id")
  909. new_field = BigAutoField(primary_key=True)
  910. new_field.model = Foo
  911. new_field.set_attributes_from_name("id")
  912. with connection.schema_editor() as editor:
  913. editor.alter_field(Foo, old_field, new_field, strict=True)
  914. Foo.objects.create()
  915. def test_alter_text_field(self):
  916. # Regression for "BLOB/TEXT column 'info' can't have a default value")
  917. # on MySQL.
  918. # Create the table
  919. with connection.schema_editor() as editor:
  920. editor.create_model(Note)
  921. old_field = Note._meta.get_field("info")
  922. new_field = TextField(blank=True)
  923. new_field.set_attributes_from_name("info")
  924. with connection.schema_editor() as editor:
  925. editor.alter_field(Note, old_field, new_field, strict=True)
  926. def test_alter_text_field_to_not_null_with_default_value(self):
  927. with connection.schema_editor() as editor:
  928. editor.create_model(Note)
  929. old_field = Note._meta.get_field("address")
  930. new_field = TextField(blank=True, default="", null=False)
  931. new_field.set_attributes_from_name("address")
  932. with connection.schema_editor() as editor:
  933. editor.alter_field(Note, old_field, new_field, strict=True)
  934. @skipUnlessDBFeature("can_defer_constraint_checks", "can_rollback_ddl")
  935. def test_alter_fk_checks_deferred_constraints(self):
  936. """
  937. #25492 - Altering a foreign key's structure and data in the same
  938. transaction.
  939. """
  940. with connection.schema_editor() as editor:
  941. editor.create_model(Node)
  942. old_field = Node._meta.get_field("parent")
  943. new_field = ForeignKey(Node, CASCADE)
  944. new_field.set_attributes_from_name("parent")
  945. parent = Node.objects.create()
  946. with connection.schema_editor() as editor:
  947. # Update the parent FK to create a deferred constraint check.
  948. Node.objects.update(parent=parent)
  949. editor.alter_field(Node, old_field, new_field, strict=True)
  950. @isolate_apps("schema")
  951. def test_alter_null_with_default_value_deferred_constraints(self):
  952. class Publisher(Model):
  953. class Meta:
  954. app_label = "schema"
  955. class Article(Model):
  956. publisher = ForeignKey(Publisher, CASCADE)
  957. title = CharField(max_length=50, null=True)
  958. description = CharField(max_length=100, null=True)
  959. class Meta:
  960. app_label = "schema"
  961. with connection.schema_editor() as editor:
  962. editor.create_model(Publisher)
  963. editor.create_model(Article)
  964. self.isolated_local_models = [Article, Publisher]
  965. publisher = Publisher.objects.create()
  966. Article.objects.create(publisher=publisher)
  967. old_title = Article._meta.get_field("title")
  968. new_title = CharField(max_length=50, null=False, default="")
  969. new_title.set_attributes_from_name("title")
  970. old_description = Article._meta.get_field("description")
  971. new_description = CharField(max_length=100, null=False, default="")
  972. new_description.set_attributes_from_name("description")
  973. with connection.schema_editor() as editor:
  974. editor.alter_field(Article, old_title, new_title, strict=True)
  975. editor.alter_field(Article, old_description, new_description, strict=True)
  976. def test_alter_text_field_to_date_field(self):
  977. """
  978. #25002 - Test conversion of text field to date field.
  979. """
  980. with connection.schema_editor() as editor:
  981. editor.create_model(Note)
  982. Note.objects.create(info="1988-05-05")
  983. old_field = Note._meta.get_field("info")
  984. new_field = DateField(blank=True)
  985. new_field.set_attributes_from_name("info")
  986. with connection.schema_editor() as editor:
  987. editor.alter_field(Note, old_field, new_field, strict=True)
  988. # Make sure the field isn't nullable
  989. columns = self.column_classes(Note)
  990. self.assertFalse(columns["info"][1][6])
  991. def test_alter_text_field_to_datetime_field(self):
  992. """
  993. #25002 - Test conversion of text field to datetime field.
  994. """
  995. with connection.schema_editor() as editor:
  996. editor.create_model(Note)
  997. Note.objects.create(info="1988-05-05 3:16:17.4567")
  998. old_field = Note._meta.get_field("info")
  999. new_field = DateTimeField(blank=True)
  1000. new_field.set_attributes_from_name("info")
  1001. with connection.schema_editor() as editor:
  1002. editor.alter_field(Note, old_field, new_field, strict=True)
  1003. # Make sure the field isn't nullable
  1004. columns = self.column_classes(Note)
  1005. self.assertFalse(columns["info"][1][6])
  1006. def test_alter_text_field_to_time_field(self):
  1007. """
  1008. #25002 - Test conversion of text field to time field.
  1009. """
  1010. with connection.schema_editor() as editor:
  1011. editor.create_model(Note)
  1012. Note.objects.create(info="3:16:17.4567")
  1013. old_field = Note._meta.get_field("info")
  1014. new_field = TimeField(blank=True)
  1015. new_field.set_attributes_from_name("info")
  1016. with connection.schema_editor() as editor:
  1017. editor.alter_field(Note, old_field, new_field, strict=True)
  1018. # Make sure the field isn't nullable
  1019. columns = self.column_classes(Note)
  1020. self.assertFalse(columns["info"][1][6])
  1021. @skipIfDBFeature("interprets_empty_strings_as_nulls")
  1022. def test_alter_textual_field_keep_null_status(self):
  1023. """
  1024. Changing a field type shouldn't affect the not null status.
  1025. """
  1026. with connection.schema_editor() as editor:
  1027. editor.create_model(Note)
  1028. with self.assertRaises(IntegrityError):
  1029. Note.objects.create(info=None)
  1030. old_field = Note._meta.get_field("info")
  1031. new_field = CharField(max_length=50)
  1032. new_field.set_attributes_from_name("info")
  1033. with connection.schema_editor() as editor:
  1034. editor.alter_field(Note, old_field, new_field, strict=True)
  1035. with self.assertRaises(IntegrityError):
  1036. Note.objects.create(info=None)
  1037. @skipUnlessDBFeature("interprets_empty_strings_as_nulls")
  1038. def test_alter_textual_field_not_null_to_null(self):
  1039. """
  1040. Nullability for textual fields is preserved on databases that
  1041. interpret empty strings as NULLs.
  1042. """
  1043. with connection.schema_editor() as editor:
  1044. editor.create_model(Author)
  1045. columns = self.column_classes(Author)
  1046. # Field is nullable.
  1047. self.assertTrue(columns["uuid"][1][6])
  1048. # Change to NOT NULL.
  1049. old_field = Author._meta.get_field("uuid")
  1050. new_field = SlugField(null=False, blank=True)
  1051. new_field.set_attributes_from_name("uuid")
  1052. with connection.schema_editor() as editor:
  1053. editor.alter_field(Author, old_field, new_field, strict=True)
  1054. columns = self.column_classes(Author)
  1055. # Nullability is preserved.
  1056. self.assertTrue(columns["uuid"][1][6])
  1057. def test_alter_numeric_field_keep_null_status(self):
  1058. """
  1059. Changing a field type shouldn't affect the not null status.
  1060. """
  1061. with connection.schema_editor() as editor:
  1062. editor.create_model(UniqueTest)
  1063. with self.assertRaises(IntegrityError):
  1064. UniqueTest.objects.create(year=None, slug="aaa")
  1065. old_field = UniqueTest._meta.get_field("year")
  1066. new_field = BigIntegerField()
  1067. new_field.set_attributes_from_name("year")
  1068. with connection.schema_editor() as editor:
  1069. editor.alter_field(UniqueTest, old_field, new_field, strict=True)
  1070. with self.assertRaises(IntegrityError):
  1071. UniqueTest.objects.create(year=None, slug="bbb")
  1072. def test_alter_null_to_not_null(self):
  1073. """
  1074. #23609 - Tests handling of default values when altering from NULL to NOT NULL.
  1075. """
  1076. # Create the table
  1077. with connection.schema_editor() as editor:
  1078. editor.create_model(Author)
  1079. # Ensure the field is right to begin with
  1080. columns = self.column_classes(Author)
  1081. self.assertTrue(columns["height"][1][6])
  1082. # Create some test data
  1083. Author.objects.create(name="Not null author", height=12)
  1084. Author.objects.create(name="Null author")
  1085. # Verify null value
  1086. self.assertEqual(Author.objects.get(name="Not null author").height, 12)
  1087. self.assertIsNone(Author.objects.get(name="Null author").height)
  1088. # Alter the height field to NOT NULL with default
  1089. old_field = Author._meta.get_field("height")
  1090. new_field = PositiveIntegerField(default=42)
  1091. new_field.set_attributes_from_name("height")
  1092. with connection.schema_editor() as editor:
  1093. editor.alter_field(Author, old_field, new_field, strict=True)
  1094. columns = self.column_classes(Author)
  1095. self.assertFalse(columns["height"][1][6])
  1096. # Verify default value
  1097. self.assertEqual(Author.objects.get(name="Not null author").height, 12)
  1098. self.assertEqual(Author.objects.get(name="Null author").height, 42)
  1099. def test_alter_charfield_to_null(self):
  1100. """
  1101. #24307 - Should skip an alter statement on databases with
  1102. interprets_empty_strings_as_nulls when changing a CharField to null.
  1103. """
  1104. # Create the table
  1105. with connection.schema_editor() as editor:
  1106. editor.create_model(Author)
  1107. # Change the CharField to null
  1108. old_field = Author._meta.get_field("name")
  1109. new_field = copy(old_field)
  1110. new_field.null = True
  1111. with connection.schema_editor() as editor:
  1112. editor.alter_field(Author, old_field, new_field, strict=True)
  1113. @unittest.skipUnless(connection.vendor == "postgresql", "PostgreSQL specific")
  1114. def test_alter_char_field_decrease_length(self):
  1115. # Create the table.
  1116. with connection.schema_editor() as editor:
  1117. editor.create_model(Author)
  1118. Author.objects.create(name="x" * 255)
  1119. # Change max_length of CharField.
  1120. old_field = Author._meta.get_field("name")
  1121. new_field = CharField(max_length=254)
  1122. new_field.set_attributes_from_name("name")
  1123. with connection.schema_editor() as editor:
  1124. msg = "value too long for type character varying(254)"
  1125. with self.assertRaisesMessage(DataError, msg):
  1126. editor.alter_field(Author, old_field, new_field, strict=True)
  1127. @unittest.skipUnless(connection.vendor == "postgresql", "PostgreSQL specific")
  1128. def test_alter_field_with_custom_db_type(self):
  1129. from django.contrib.postgres.fields import ArrayField
  1130. class Foo(Model):
  1131. field = ArrayField(CharField(max_length=255))
  1132. class Meta:
  1133. app_label = "schema"
  1134. with connection.schema_editor() as editor:
  1135. editor.create_model(Foo)
  1136. self.isolated_local_models = [Foo]
  1137. old_field = Foo._meta.get_field("field")
  1138. new_field = ArrayField(CharField(max_length=16))
  1139. new_field.set_attributes_from_name("field")
  1140. new_field.model = Foo
  1141. with connection.schema_editor() as editor:
  1142. editor.alter_field(Foo, old_field, new_field, strict=True)
  1143. @isolate_apps("schema")
  1144. @unittest.skipUnless(connection.vendor == "postgresql", "PostgreSQL specific")
  1145. def test_alter_array_field_decrease_base_field_length(self):
  1146. from django.contrib.postgres.fields import ArrayField
  1147. class ArrayModel(Model):
  1148. field = ArrayField(CharField(max_length=16))
  1149. class Meta:
  1150. app_label = "schema"
  1151. with connection.schema_editor() as editor:
  1152. editor.create_model(ArrayModel)
  1153. self.isolated_local_models = [ArrayModel]
  1154. ArrayModel.objects.create(field=["x" * 16])
  1155. old_field = ArrayModel._meta.get_field("field")
  1156. new_field = ArrayField(CharField(max_length=15))
  1157. new_field.set_attributes_from_name("field")
  1158. new_field.model = ArrayModel
  1159. with connection.schema_editor() as editor:
  1160. msg = "value too long for type character varying(15)"
  1161. with self.assertRaisesMessage(DataError, msg):
  1162. editor.alter_field(ArrayModel, old_field, new_field, strict=True)
  1163. @isolate_apps("schema")
  1164. @unittest.skipUnless(connection.vendor == "postgresql", "PostgreSQL specific")
  1165. def test_alter_array_field_decrease_nested_base_field_length(self):
  1166. from django.contrib.postgres.fields import ArrayField
  1167. class ArrayModel(Model):
  1168. field = ArrayField(ArrayField(CharField(max_length=16)))
  1169. class Meta:
  1170. app_label = "schema"
  1171. with connection.schema_editor() as editor:
  1172. editor.create_model(ArrayModel)
  1173. self.isolated_local_models = [ArrayModel]
  1174. ArrayModel.objects.create(field=[["x" * 16]])
  1175. old_field = ArrayModel._meta.get_field("field")
  1176. new_field = ArrayField(ArrayField(CharField(max_length=15)))
  1177. new_field.set_attributes_from_name("field")
  1178. new_field.model = ArrayModel
  1179. with connection.schema_editor() as editor:
  1180. msg = "value too long for type character varying(15)"
  1181. with self.assertRaisesMessage(DataError, msg):
  1182. editor.alter_field(ArrayModel, old_field, new_field, strict=True)
  1183. def _add_ci_collation(self):
  1184. ci_collation = "case_insensitive"
  1185. def drop_collation():
  1186. with connection.cursor() as cursor:
  1187. cursor.execute(f"DROP COLLATION IF EXISTS {ci_collation}")
  1188. with connection.cursor() as cursor:
  1189. cursor.execute(
  1190. f"CREATE COLLATION IF NOT EXISTS {ci_collation} (provider=icu, "
  1191. f"locale='und-u-ks-level2', deterministic=false)"
  1192. )
  1193. self.addCleanup(drop_collation)
  1194. return ci_collation
  1195. @isolate_apps("schema")
  1196. @unittest.skipUnless(connection.vendor == "postgresql", "PostgreSQL specific")
  1197. @skipUnlessDBFeature(
  1198. "supports_collation_on_charfield",
  1199. "supports_non_deterministic_collations",
  1200. )
  1201. def test_db_collation_arrayfield(self):
  1202. from django.contrib.postgres.fields import ArrayField
  1203. ci_collation = self._add_ci_collation()
  1204. cs_collation = "en-x-icu"
  1205. class ArrayModel(Model):
  1206. field = ArrayField(CharField(max_length=16, db_collation=ci_collation))
  1207. class Meta:
  1208. app_label = "schema"
  1209. # Create the table.
  1210. with connection.schema_editor() as editor:
  1211. editor.create_model(ArrayModel)
  1212. self.isolated_local_models = [ArrayModel]
  1213. self.assertEqual(
  1214. self.get_column_collation(ArrayModel._meta.db_table, "field"),
  1215. ci_collation,
  1216. )
  1217. # Alter collation.
  1218. old_field = ArrayModel._meta.get_field("field")
  1219. new_field_cs = ArrayField(CharField(max_length=16, db_collation=cs_collation))
  1220. new_field_cs.set_attributes_from_name("field")
  1221. new_field_cs.model = ArrayField
  1222. with connection.schema_editor() as editor:
  1223. editor.alter_field(ArrayModel, old_field, new_field_cs, strict=True)
  1224. self.assertEqual(
  1225. self.get_column_collation(ArrayModel._meta.db_table, "field"),
  1226. cs_collation,
  1227. )
  1228. @isolate_apps("schema")
  1229. @unittest.skipUnless(connection.vendor == "postgresql", "PostgreSQL specific")
  1230. @skipUnlessDBFeature(
  1231. "supports_collation_on_charfield",
  1232. "supports_non_deterministic_collations",
  1233. )
  1234. def test_unique_with_collation_charfield(self):
  1235. ci_collation = self._add_ci_collation()
  1236. class CiCharModel(Model):
  1237. field = CharField(max_length=16, db_collation=ci_collation, unique=True)
  1238. class Meta:
  1239. app_label = "schema"
  1240. # Create the table.
  1241. with connection.schema_editor() as editor:
  1242. editor.create_model(CiCharModel)
  1243. self.isolated_local_models = [CiCharModel]
  1244. self.assertEqual(
  1245. self.get_column_collation(CiCharModel._meta.db_table, "field"),
  1246. ci_collation,
  1247. )
  1248. self.assertIn("field", self.get_uniques(CiCharModel._meta.db_table))
  1249. @isolate_apps("schema")
  1250. @unittest.skipUnless(connection.vendor == "postgresql", "PostgreSQL specific")
  1251. @skipUnlessDBFeature(
  1252. "supports_collation_on_charfield",
  1253. "supports_non_deterministic_collations",
  1254. )
  1255. def test_relation_to_collation_charfield(self):
  1256. ci_collation = self._add_ci_collation()
  1257. class CiCharModel(Model):
  1258. field = CharField(max_length=16, db_collation=ci_collation, unique=True)
  1259. class Meta:
  1260. app_label = "schema"
  1261. class RelationModel(Model):
  1262. field = OneToOneField(CiCharModel, CASCADE, to_field="field")
  1263. class Meta:
  1264. app_label = "schema"
  1265. # Create the table.
  1266. with connection.schema_editor() as editor:
  1267. editor.create_model(CiCharModel)
  1268. editor.create_model(RelationModel)
  1269. self.isolated_local_models = [CiCharModel, RelationModel]
  1270. self.assertEqual(
  1271. self.get_column_collation(RelationModel._meta.db_table, "field_id"),
  1272. ci_collation,
  1273. )
  1274. self.assertEqual(
  1275. self.get_column_collation(CiCharModel._meta.db_table, "field"),
  1276. ci_collation,
  1277. )
  1278. self.assertIn("field_id", self.get_uniques(RelationModel._meta.db_table))
  1279. def test_alter_textfield_to_null(self):
  1280. """
  1281. #24307 - Should skip an alter statement on databases with
  1282. interprets_empty_strings_as_nulls when changing a TextField to null.
  1283. """
  1284. # Create the table
  1285. with connection.schema_editor() as editor:
  1286. editor.create_model(Note)
  1287. # Change the TextField to null
  1288. old_field = Note._meta.get_field("info")
  1289. new_field = copy(old_field)
  1290. new_field.null = True
  1291. with connection.schema_editor() as editor:
  1292. editor.alter_field(Note, old_field, new_field, strict=True)
  1293. def test_alter_null_to_not_null_keeping_default(self):
  1294. """
  1295. #23738 - Can change a nullable field with default to non-nullable
  1296. with the same default.
  1297. """
  1298. # Create the table
  1299. with connection.schema_editor() as editor:
  1300. editor.create_model(AuthorWithDefaultHeight)
  1301. # Ensure the field is right to begin with
  1302. columns = self.column_classes(AuthorWithDefaultHeight)
  1303. self.assertTrue(columns["height"][1][6])
  1304. # Alter the height field to NOT NULL keeping the previous default
  1305. old_field = AuthorWithDefaultHeight._meta.get_field("height")
  1306. new_field = PositiveIntegerField(default=42)
  1307. new_field.set_attributes_from_name("height")
  1308. with connection.schema_editor() as editor:
  1309. editor.alter_field(
  1310. AuthorWithDefaultHeight, old_field, new_field, strict=True
  1311. )
  1312. columns = self.column_classes(AuthorWithDefaultHeight)
  1313. self.assertFalse(columns["height"][1][6])
  1314. @skipUnlessDBFeature("supports_foreign_keys")
  1315. def test_alter_fk(self):
  1316. """
  1317. Tests altering of FKs
  1318. """
  1319. # Create the table
  1320. with connection.schema_editor() as editor:
  1321. editor.create_model(Author)
  1322. editor.create_model(Book)
  1323. # Ensure the field is right to begin with
  1324. columns = self.column_classes(Book)
  1325. self.assertEqual(
  1326. columns["author_id"][0],
  1327. connection.features.introspected_field_types["IntegerField"],
  1328. )
  1329. self.assertForeignKeyExists(Book, "author_id", "schema_author")
  1330. # Alter the FK
  1331. old_field = Book._meta.get_field("author")
  1332. new_field = ForeignKey(Author, CASCADE, editable=False)
  1333. new_field.set_attributes_from_name("author")
  1334. with connection.schema_editor() as editor:
  1335. editor.alter_field(Book, old_field, new_field, strict=True)
  1336. columns = self.column_classes(Book)
  1337. self.assertEqual(
  1338. columns["author_id"][0],
  1339. connection.features.introspected_field_types["IntegerField"],
  1340. )
  1341. self.assertForeignKeyExists(Book, "author_id", "schema_author")
  1342. @skipUnlessDBFeature("supports_foreign_keys")
  1343. def test_alter_to_fk(self):
  1344. """
  1345. #24447 - Tests adding a FK constraint for an existing column
  1346. """
  1347. class LocalBook(Model):
  1348. author = IntegerField()
  1349. title = CharField(max_length=100, db_index=True)
  1350. pub_date = DateTimeField()
  1351. class Meta:
  1352. app_label = "schema"
  1353. apps = new_apps
  1354. self.local_models = [LocalBook]
  1355. # Create the tables
  1356. with connection.schema_editor() as editor:
  1357. editor.create_model(Author)
  1358. editor.create_model(LocalBook)
  1359. # Ensure no FK constraint exists
  1360. constraints = self.get_constraints(LocalBook._meta.db_table)
  1361. for details in constraints.values():
  1362. if details["foreign_key"]:
  1363. self.fail(
  1364. "Found an unexpected FK constraint to %s" % details["columns"]
  1365. )
  1366. old_field = LocalBook._meta.get_field("author")
  1367. new_field = ForeignKey(Author, CASCADE)
  1368. new_field.set_attributes_from_name("author")
  1369. with connection.schema_editor() as editor:
  1370. editor.alter_field(LocalBook, old_field, new_field, strict=True)
  1371. self.assertForeignKeyExists(LocalBook, "author_id", "schema_author")
  1372. @skipUnlessDBFeature("supports_foreign_keys", "can_introspect_foreign_keys")
  1373. def test_alter_o2o_to_fk(self):
  1374. """
  1375. #24163 - Tests altering of OneToOneField to ForeignKey
  1376. """
  1377. # Create the table
  1378. with connection.schema_editor() as editor:
  1379. editor.create_model(Author)
  1380. editor.create_model(BookWithO2O)
  1381. # Ensure the field is right to begin with
  1382. columns = self.column_classes(BookWithO2O)
  1383. self.assertEqual(
  1384. columns["author_id"][0],
  1385. connection.features.introspected_field_types["IntegerField"],
  1386. )
  1387. # Ensure the field is unique
  1388. author = Author.objects.create(name="Joe")
  1389. BookWithO2O.objects.create(
  1390. author=author, title="Django 1", pub_date=datetime.datetime.now()
  1391. )
  1392. with self.assertRaises(IntegrityError):
  1393. BookWithO2O.objects.create(
  1394. author=author, title="Django 2", pub_date=datetime.datetime.now()
  1395. )
  1396. BookWithO2O.objects.all().delete()
  1397. self.assertForeignKeyExists(BookWithO2O, "author_id", "schema_author")
  1398. # Alter the OneToOneField to ForeignKey
  1399. old_field = BookWithO2O._meta.get_field("author")
  1400. new_field = ForeignKey(Author, CASCADE)
  1401. new_field.set_attributes_from_name("author")
  1402. with connection.schema_editor() as editor:
  1403. editor.alter_field(BookWithO2O, old_field, new_field, strict=True)
  1404. columns = self.column_classes(Book)
  1405. self.assertEqual(
  1406. columns["author_id"][0],
  1407. connection.features.introspected_field_types["IntegerField"],
  1408. )
  1409. # Ensure the field is not unique anymore
  1410. Book.objects.create(
  1411. author=author, title="Django 1", pub_date=datetime.datetime.now()
  1412. )
  1413. Book.objects.create(
  1414. author=author, title="Django 2", pub_date=datetime.datetime.now()
  1415. )
  1416. self.assertForeignKeyExists(Book, "author_id", "schema_author")
  1417. @skipUnlessDBFeature("supports_foreign_keys", "can_introspect_foreign_keys")
  1418. def test_alter_fk_to_o2o(self):
  1419. """
  1420. #24163 - Tests altering of ForeignKey to OneToOneField
  1421. """
  1422. # Create the table
  1423. with connection.schema_editor() as editor:
  1424. editor.create_model(Author)
  1425. editor.create_model(Book)
  1426. # Ensure the field is right to begin with
  1427. columns = self.column_classes(Book)
  1428. self.assertEqual(
  1429. columns["author_id"][0],
  1430. connection.features.introspected_field_types["IntegerField"],
  1431. )
  1432. # Ensure the field is not unique
  1433. author = Author.objects.create(name="Joe")
  1434. Book.objects.create(
  1435. author=author, title="Django 1", pub_date=datetime.datetime.now()
  1436. )
  1437. Book.objects.create(
  1438. author=author, title="Django 2", pub_date=datetime.datetime.now()
  1439. )
  1440. Book.objects.all().delete()
  1441. self.assertForeignKeyExists(Book, "author_id", "schema_author")
  1442. # Alter the ForeignKey to OneToOneField
  1443. old_field = Book._meta.get_field("author")
  1444. new_field = OneToOneField(Author, CASCADE)
  1445. new_field.set_attributes_from_name("author")
  1446. with connection.schema_editor() as editor:
  1447. editor.alter_field(Book, old_field, new_field, strict=True)
  1448. columns = self.column_classes(BookWithO2O)
  1449. self.assertEqual(
  1450. columns["author_id"][0],
  1451. connection.features.introspected_field_types["IntegerField"],
  1452. )
  1453. # Ensure the field is unique now
  1454. BookWithO2O.objects.create(
  1455. author=author, title="Django 1", pub_date=datetime.datetime.now()
  1456. )
  1457. with self.assertRaises(IntegrityError):
  1458. BookWithO2O.objects.create(
  1459. author=author, title="Django 2", pub_date=datetime.datetime.now()
  1460. )
  1461. self.assertForeignKeyExists(BookWithO2O, "author_id", "schema_author")
  1462. def test_alter_field_fk_to_o2o(self):
  1463. with connection.schema_editor() as editor:
  1464. editor.create_model(Author)
  1465. editor.create_model(Book)
  1466. expected_fks = (
  1467. 1
  1468. if connection.features.supports_foreign_keys
  1469. and connection.features.can_introspect_foreign_keys
  1470. else 0
  1471. )
  1472. expected_indexes = 1 if connection.features.indexes_foreign_keys else 0
  1473. # Check the index is right to begin with.
  1474. counts = self.get_constraints_count(
  1475. Book._meta.db_table,
  1476. Book._meta.get_field("author").column,
  1477. (Author._meta.db_table, Author._meta.pk.column),
  1478. )
  1479. self.assertEqual(
  1480. counts,
  1481. {"fks": expected_fks, "uniques": 0, "indexes": expected_indexes},
  1482. )
  1483. old_field = Book._meta.get_field("author")
  1484. new_field = OneToOneField(Author, CASCADE)
  1485. new_field.set_attributes_from_name("author")
  1486. with connection.schema_editor() as editor:
  1487. editor.alter_field(Book, old_field, new_field)
  1488. counts = self.get_constraints_count(
  1489. Book._meta.db_table,
  1490. Book._meta.get_field("author").column,
  1491. (Author._meta.db_table, Author._meta.pk.column),
  1492. )
  1493. # The index on ForeignKey is replaced with a unique constraint for
  1494. # OneToOneField.
  1495. self.assertEqual(counts, {"fks": expected_fks, "uniques": 1, "indexes": 0})
  1496. def test_autofield_to_o2o(self):
  1497. with connection.schema_editor() as editor:
  1498. editor.create_model(Author)
  1499. editor.create_model(Note)
  1500. # Rename the field.
  1501. old_field = Author._meta.get_field("id")
  1502. new_field = AutoField(primary_key=True)
  1503. new_field.set_attributes_from_name("note_ptr")
  1504. new_field.model = Author
  1505. with connection.schema_editor() as editor:
  1506. editor.alter_field(Author, old_field, new_field, strict=True)
  1507. # Alter AutoField to OneToOneField.
  1508. new_field_o2o = OneToOneField(Note, CASCADE)
  1509. new_field_o2o.set_attributes_from_name("note_ptr")
  1510. new_field_o2o.model = Author
  1511. with connection.schema_editor() as editor:
  1512. editor.alter_field(Author, new_field, new_field_o2o, strict=True)
  1513. columns = self.column_classes(Author)
  1514. field_type, _ = columns["note_ptr_id"]
  1515. self.assertEqual(
  1516. field_type, connection.features.introspected_field_types["IntegerField"]
  1517. )
  1518. def test_alter_field_fk_keeps_index(self):
  1519. with connection.schema_editor() as editor:
  1520. editor.create_model(Author)
  1521. editor.create_model(Book)
  1522. expected_fks = (
  1523. 1
  1524. if connection.features.supports_foreign_keys
  1525. and connection.features.can_introspect_foreign_keys
  1526. else 0
  1527. )
  1528. expected_indexes = 1 if connection.features.indexes_foreign_keys else 0
  1529. # Check the index is right to begin with.
  1530. counts = self.get_constraints_count(
  1531. Book._meta.db_table,
  1532. Book._meta.get_field("author").column,
  1533. (Author._meta.db_table, Author._meta.pk.column),
  1534. )
  1535. self.assertEqual(
  1536. counts,
  1537. {"fks": expected_fks, "uniques": 0, "indexes": expected_indexes},
  1538. )
  1539. old_field = Book._meta.get_field("author")
  1540. # on_delete changed from CASCADE.
  1541. new_field = ForeignKey(Author, PROTECT)
  1542. new_field.set_attributes_from_name("author")
  1543. with connection.schema_editor() as editor:
  1544. editor.alter_field(Book, old_field, new_field, strict=True)
  1545. counts = self.get_constraints_count(
  1546. Book._meta.db_table,
  1547. Book._meta.get_field("author").column,
  1548. (Author._meta.db_table, Author._meta.pk.column),
  1549. )
  1550. # The index remains.
  1551. self.assertEqual(
  1552. counts,
  1553. {"fks": expected_fks, "uniques": 0, "indexes": expected_indexes},
  1554. )
  1555. def test_alter_field_o2o_to_fk(self):
  1556. with connection.schema_editor() as editor:
  1557. editor.create_model(Author)
  1558. editor.create_model(BookWithO2O)
  1559. expected_fks = (
  1560. 1
  1561. if connection.features.supports_foreign_keys
  1562. and connection.features.can_introspect_foreign_keys
  1563. else 0
  1564. )
  1565. # Check the unique constraint is right to begin with.
  1566. counts = self.get_constraints_count(
  1567. BookWithO2O._meta.db_table,
  1568. BookWithO2O._meta.get_field("author").column,
  1569. (Author._meta.db_table, Author._meta.pk.column),
  1570. )
  1571. self.assertEqual(counts, {"fks": expected_fks, "uniques": 1, "indexes": 0})
  1572. old_field = BookWithO2O._meta.get_field("author")
  1573. new_field = ForeignKey(Author, CASCADE)
  1574. new_field.set_attributes_from_name("author")
  1575. with connection.schema_editor() as editor:
  1576. editor.alter_field(BookWithO2O, old_field, new_field)
  1577. counts = self.get_constraints_count(
  1578. BookWithO2O._meta.db_table,
  1579. BookWithO2O._meta.get_field("author").column,
  1580. (Author._meta.db_table, Author._meta.pk.column),
  1581. )
  1582. # The unique constraint on OneToOneField is replaced with an index for
  1583. # ForeignKey.
  1584. self.assertEqual(counts, {"fks": expected_fks, "uniques": 0, "indexes": 1})
  1585. def test_alter_field_o2o_keeps_unique(self):
  1586. with connection.schema_editor() as editor:
  1587. editor.create_model(Author)
  1588. editor.create_model(BookWithO2O)
  1589. expected_fks = (
  1590. 1
  1591. if connection.features.supports_foreign_keys
  1592. and connection.features.can_introspect_foreign_keys
  1593. else 0
  1594. )
  1595. # Check the unique constraint is right to begin with.
  1596. counts = self.get_constraints_count(
  1597. BookWithO2O._meta.db_table,
  1598. BookWithO2O._meta.get_field("author").column,
  1599. (Author._meta.db_table, Author._meta.pk.column),
  1600. )
  1601. self.assertEqual(counts, {"fks": expected_fks, "uniques": 1, "indexes": 0})
  1602. old_field = BookWithO2O._meta.get_field("author")
  1603. # on_delete changed from CASCADE.
  1604. new_field = OneToOneField(Author, PROTECT)
  1605. new_field.set_attributes_from_name("author")
  1606. with connection.schema_editor() as editor:
  1607. editor.alter_field(BookWithO2O, old_field, new_field, strict=True)
  1608. counts = self.get_constraints_count(
  1609. BookWithO2O._meta.db_table,
  1610. BookWithO2O._meta.get_field("author").column,
  1611. (Author._meta.db_table, Author._meta.pk.column),
  1612. )
  1613. # The unique constraint remains.
  1614. self.assertEqual(counts, {"fks": expected_fks, "uniques": 1, "indexes": 0})
  1615. @skipUnlessDBFeature("ignores_table_name_case")
  1616. def test_alter_db_table_case(self):
  1617. # Create the table
  1618. with connection.schema_editor() as editor:
  1619. editor.create_model(Author)
  1620. # Alter the case of the table
  1621. old_table_name = Author._meta.db_table
  1622. with connection.schema_editor() as editor:
  1623. editor.alter_db_table(Author, old_table_name, old_table_name.upper())
  1624. def test_alter_implicit_id_to_explicit(self):
  1625. """
  1626. Should be able to convert an implicit "id" field to an explicit "id"
  1627. primary key field.
  1628. """
  1629. with connection.schema_editor() as editor:
  1630. editor.create_model(Author)
  1631. old_field = Author._meta.get_field("id")
  1632. new_field = AutoField(primary_key=True)
  1633. new_field.set_attributes_from_name("id")
  1634. new_field.model = Author
  1635. with connection.schema_editor() as editor:
  1636. editor.alter_field(Author, old_field, new_field, strict=True)
  1637. # This will fail if DROP DEFAULT is inadvertently executed on this
  1638. # field which drops the id sequence, at least on PostgreSQL.
  1639. Author.objects.create(name="Foo")
  1640. Author.objects.create(name="Bar")
  1641. def test_alter_autofield_pk_to_bigautofield_pk(self):
  1642. with connection.schema_editor() as editor:
  1643. editor.create_model(Author)
  1644. old_field = Author._meta.get_field("id")
  1645. new_field = BigAutoField(primary_key=True)
  1646. new_field.set_attributes_from_name("id")
  1647. new_field.model = Author
  1648. with connection.schema_editor() as editor:
  1649. editor.alter_field(Author, old_field, new_field, strict=True)
  1650. Author.objects.create(name="Foo", pk=1)
  1651. with connection.cursor() as cursor:
  1652. sequence_reset_sqls = connection.ops.sequence_reset_sql(
  1653. no_style(), [Author]
  1654. )
  1655. if sequence_reset_sqls:
  1656. cursor.execute(sequence_reset_sqls[0])
  1657. self.assertIsNotNone(Author.objects.create(name="Bar"))
  1658. def test_alter_autofield_pk_to_smallautofield_pk(self):
  1659. with connection.schema_editor() as editor:
  1660. editor.create_model(Author)
  1661. old_field = Author._meta.get_field("id")
  1662. new_field = SmallAutoField(primary_key=True)
  1663. new_field.set_attributes_from_name("id")
  1664. new_field.model = Author
  1665. with connection.schema_editor() as editor:
  1666. editor.alter_field(Author, old_field, new_field, strict=True)
  1667. Author.objects.create(name="Foo", pk=1)
  1668. with connection.cursor() as cursor:
  1669. sequence_reset_sqls = connection.ops.sequence_reset_sql(
  1670. no_style(), [Author]
  1671. )
  1672. if sequence_reset_sqls:
  1673. cursor.execute(sequence_reset_sqls[0])
  1674. self.assertIsNotNone(Author.objects.create(name="Bar"))
  1675. def test_alter_int_pk_to_autofield_pk(self):
  1676. """
  1677. Should be able to rename an IntegerField(primary_key=True) to
  1678. AutoField(primary_key=True).
  1679. """
  1680. with connection.schema_editor() as editor:
  1681. editor.create_model(IntegerPK)
  1682. old_field = IntegerPK._meta.get_field("i")
  1683. new_field = AutoField(primary_key=True)
  1684. new_field.model = IntegerPK
  1685. new_field.set_attributes_from_name("i")
  1686. with connection.schema_editor() as editor:
  1687. editor.alter_field(IntegerPK, old_field, new_field, strict=True)
  1688. # A model representing the updated model.
  1689. class IntegerPKToAutoField(Model):
  1690. i = AutoField(primary_key=True)
  1691. j = IntegerField(unique=True)
  1692. class Meta:
  1693. app_label = "schema"
  1694. apps = new_apps
  1695. db_table = IntegerPK._meta.db_table
  1696. # An id (i) is generated by the database.
  1697. obj = IntegerPKToAutoField.objects.create(j=1)
  1698. self.assertIsNotNone(obj.i)
  1699. def test_alter_int_pk_to_bigautofield_pk(self):
  1700. """
  1701. Should be able to rename an IntegerField(primary_key=True) to
  1702. BigAutoField(primary_key=True).
  1703. """
  1704. with connection.schema_editor() as editor:
  1705. editor.create_model(IntegerPK)
  1706. old_field = IntegerPK._meta.get_field("i")
  1707. new_field = BigAutoField(primary_key=True)
  1708. new_field.model = IntegerPK
  1709. new_field.set_attributes_from_name("i")
  1710. with connection.schema_editor() as editor:
  1711. editor.alter_field(IntegerPK, old_field, new_field, strict=True)
  1712. # A model representing the updated model.
  1713. class IntegerPKToBigAutoField(Model):
  1714. i = BigAutoField(primary_key=True)
  1715. j = IntegerField(unique=True)
  1716. class Meta:
  1717. app_label = "schema"
  1718. apps = new_apps
  1719. db_table = IntegerPK._meta.db_table
  1720. # An id (i) is generated by the database.
  1721. obj = IntegerPKToBigAutoField.objects.create(j=1)
  1722. self.assertIsNotNone(obj.i)
  1723. @isolate_apps("schema")
  1724. def test_alter_smallint_pk_to_smallautofield_pk(self):
  1725. """
  1726. Should be able to rename an SmallIntegerField(primary_key=True) to
  1727. SmallAutoField(primary_key=True).
  1728. """
  1729. class SmallIntegerPK(Model):
  1730. i = SmallIntegerField(primary_key=True)
  1731. class Meta:
  1732. app_label = "schema"
  1733. with connection.schema_editor() as editor:
  1734. editor.create_model(SmallIntegerPK)
  1735. self.isolated_local_models = [SmallIntegerPK]
  1736. old_field = SmallIntegerPK._meta.get_field("i")
  1737. new_field = SmallAutoField(primary_key=True)
  1738. new_field.model = SmallIntegerPK
  1739. new_field.set_attributes_from_name("i")
  1740. with connection.schema_editor() as editor:
  1741. editor.alter_field(SmallIntegerPK, old_field, new_field, strict=True)
  1742. @isolate_apps("schema")
  1743. @unittest.skipUnless(connection.vendor == "postgresql", "PostgreSQL specific")
  1744. def test_alter_serial_auto_field_to_bigautofield(self):
  1745. class SerialAutoField(Model):
  1746. id = SmallAutoField(primary_key=True)
  1747. class Meta:
  1748. app_label = "schema"
  1749. table = SerialAutoField._meta.db_table
  1750. column = SerialAutoField._meta.get_field("id").column
  1751. with connection.cursor() as cursor:
  1752. cursor.execute(
  1753. f'CREATE TABLE "{table}" '
  1754. f'("{column}" smallserial NOT NULL PRIMARY KEY)'
  1755. )
  1756. try:
  1757. old_field = SerialAutoField._meta.get_field("id")
  1758. new_field = BigAutoField(primary_key=True)
  1759. new_field.model = SerialAutoField
  1760. new_field.set_attributes_from_name("id")
  1761. with connection.schema_editor() as editor:
  1762. editor.alter_field(SerialAutoField, old_field, new_field, strict=True)
  1763. sequence_name = f"{table}_{column}_seq"
  1764. with connection.cursor() as cursor:
  1765. cursor.execute(
  1766. "SELECT data_type FROM pg_sequences WHERE sequencename = %s",
  1767. [sequence_name],
  1768. )
  1769. row = cursor.fetchone()
  1770. sequence_data_type = row[0] if row and row[0] else None
  1771. self.assertEqual(sequence_data_type, "bigint")
  1772. # Rename the column.
  1773. old_field = new_field
  1774. new_field = AutoField(primary_key=True)
  1775. new_field.model = SerialAutoField
  1776. new_field.set_attributes_from_name("renamed_id")
  1777. with connection.schema_editor() as editor:
  1778. editor.alter_field(SerialAutoField, old_field, new_field, strict=True)
  1779. with connection.cursor() as cursor:
  1780. cursor.execute(
  1781. "SELECT data_type FROM pg_sequences WHERE sequencename = %s",
  1782. [sequence_name],
  1783. )
  1784. row = cursor.fetchone()
  1785. sequence_data_type = row[0] if row and row[0] else None
  1786. self.assertEqual(sequence_data_type, "integer")
  1787. finally:
  1788. with connection.cursor() as cursor:
  1789. cursor.execute(f'DROP TABLE "{table}"')
  1790. def test_alter_int_pk_to_int_unique(self):
  1791. """
  1792. Should be able to rename an IntegerField(primary_key=True) to
  1793. IntegerField(unique=True).
  1794. """
  1795. with connection.schema_editor() as editor:
  1796. editor.create_model(IntegerPK)
  1797. # Delete the old PK
  1798. old_field = IntegerPK._meta.get_field("i")
  1799. new_field = IntegerField(unique=True)
  1800. new_field.model = IntegerPK
  1801. new_field.set_attributes_from_name("i")
  1802. with connection.schema_editor() as editor:
  1803. editor.alter_field(IntegerPK, old_field, new_field, strict=True)
  1804. # The primary key constraint is gone. Result depends on database:
  1805. # 'id' for SQLite, None for others (must not be 'i').
  1806. self.assertIn(self.get_primary_key(IntegerPK._meta.db_table), ("id", None))
  1807. # Set up a model class as it currently stands. The original IntegerPK
  1808. # class is now out of date and some backends make use of the whole
  1809. # model class when modifying a field (such as sqlite3 when remaking a
  1810. # table) so an outdated model class leads to incorrect results.
  1811. class Transitional(Model):
  1812. i = IntegerField(unique=True)
  1813. j = IntegerField(unique=True)
  1814. class Meta:
  1815. app_label = "schema"
  1816. apps = new_apps
  1817. db_table = "INTEGERPK"
  1818. # model requires a new PK
  1819. old_field = Transitional._meta.get_field("j")
  1820. new_field = IntegerField(primary_key=True)
  1821. new_field.model = Transitional
  1822. new_field.set_attributes_from_name("j")
  1823. with connection.schema_editor() as editor:
  1824. editor.alter_field(Transitional, old_field, new_field, strict=True)
  1825. # Create a model class representing the updated model.
  1826. class IntegerUnique(Model):
  1827. i = IntegerField(unique=True)
  1828. j = IntegerField(primary_key=True)
  1829. class Meta:
  1830. app_label = "schema"
  1831. apps = new_apps
  1832. db_table = "INTEGERPK"
  1833. # Ensure unique constraint works.
  1834. IntegerUnique.objects.create(i=1, j=1)
  1835. with self.assertRaises(IntegrityError):
  1836. IntegerUnique.objects.create(i=1, j=2)
  1837. def test_rename(self):
  1838. """
  1839. Tests simple altering of fields
  1840. """
  1841. # Create the table
  1842. with connection.schema_editor() as editor:
  1843. editor.create_model(Author)
  1844. # Ensure the field is right to begin with
  1845. columns = self.column_classes(Author)
  1846. self.assertEqual(
  1847. columns["name"][0],
  1848. connection.features.introspected_field_types["CharField"],
  1849. )
  1850. self.assertNotIn("display_name", columns)
  1851. # Alter the name field's name
  1852. old_field = Author._meta.get_field("name")
  1853. new_field = CharField(max_length=254)
  1854. new_field.set_attributes_from_name("display_name")
  1855. with connection.schema_editor() as editor:
  1856. editor.alter_field(Author, old_field, new_field, strict=True)
  1857. columns = self.column_classes(Author)
  1858. self.assertEqual(
  1859. columns["display_name"][0],
  1860. connection.features.introspected_field_types["CharField"],
  1861. )
  1862. self.assertNotIn("name", columns)
  1863. @isolate_apps("schema")
  1864. def test_rename_referenced_field(self):
  1865. class Author(Model):
  1866. name = CharField(max_length=255, unique=True)
  1867. class Meta:
  1868. app_label = "schema"
  1869. class Book(Model):
  1870. author = ForeignKey(Author, CASCADE, to_field="name")
  1871. class Meta:
  1872. app_label = "schema"
  1873. with connection.schema_editor() as editor:
  1874. editor.create_model(Author)
  1875. editor.create_model(Book)
  1876. new_field = CharField(max_length=255, unique=True)
  1877. new_field.set_attributes_from_name("renamed")
  1878. with connection.schema_editor(
  1879. atomic=connection.features.supports_atomic_references_rename
  1880. ) as editor:
  1881. editor.alter_field(Author, Author._meta.get_field("name"), new_field)
  1882. # Ensure the foreign key reference was updated.
  1883. self.assertForeignKeyExists(Book, "author_id", "schema_author", "renamed")
  1884. @skipIfDBFeature("interprets_empty_strings_as_nulls")
  1885. def test_rename_keep_null_status(self):
  1886. """
  1887. Renaming a field shouldn't affect the not null status.
  1888. """
  1889. with connection.schema_editor() as editor:
  1890. editor.create_model(Note)
  1891. with self.assertRaises(IntegrityError):
  1892. Note.objects.create(info=None)
  1893. old_field = Note._meta.get_field("info")
  1894. new_field = TextField()
  1895. new_field.set_attributes_from_name("detail_info")
  1896. with connection.schema_editor() as editor:
  1897. editor.alter_field(Note, old_field, new_field, strict=True)
  1898. columns = self.column_classes(Note)
  1899. self.assertEqual(columns["detail_info"][0], "TextField")
  1900. self.assertNotIn("info", columns)
  1901. with self.assertRaises(IntegrityError):
  1902. NoteRename.objects.create(detail_info=None)
  1903. @isolate_apps("schema")
  1904. def test_rename_keep_db_default(self):
  1905. """Renaming a field shouldn't affect a database default."""
  1906. class AuthorDbDefault(Model):
  1907. birth_year = IntegerField(db_default=1985)
  1908. class Meta:
  1909. app_label = "schema"
  1910. self.isolated_local_models = [AuthorDbDefault]
  1911. with connection.schema_editor() as editor:
  1912. editor.create_model(AuthorDbDefault)
  1913. columns = self.column_classes(AuthorDbDefault)
  1914. self.assertEqual(columns["birth_year"][1].default, "1985")
  1915. old_field = AuthorDbDefault._meta.get_field("birth_year")
  1916. new_field = IntegerField(db_default=1985)
  1917. new_field.set_attributes_from_name("renamed_year")
  1918. new_field.model = AuthorDbDefault
  1919. with connection.schema_editor(
  1920. atomic=connection.features.supports_atomic_references_rename
  1921. ) as editor:
  1922. editor.alter_field(AuthorDbDefault, old_field, new_field, strict=True)
  1923. columns = self.column_classes(AuthorDbDefault)
  1924. self.assertEqual(columns["renamed_year"][1].default, "1985")
  1925. @skipUnlessDBFeature(
  1926. "supports_column_check_constraints", "can_introspect_check_constraints"
  1927. )
  1928. @isolate_apps("schema")
  1929. def test_rename_field_with_check_to_truncated_name(self):
  1930. class AuthorWithLongColumn(Model):
  1931. field_with_very_looooooong_name = PositiveIntegerField(null=True)
  1932. class Meta:
  1933. app_label = "schema"
  1934. self.isolated_local_models = [AuthorWithLongColumn]
  1935. with connection.schema_editor() as editor:
  1936. editor.create_model(AuthorWithLongColumn)
  1937. old_field = AuthorWithLongColumn._meta.get_field(
  1938. "field_with_very_looooooong_name"
  1939. )
  1940. new_field = PositiveIntegerField(null=True)
  1941. new_field.set_attributes_from_name("renamed_field_with_very_long_name")
  1942. with connection.schema_editor() as editor:
  1943. editor.alter_field(AuthorWithLongColumn, old_field, new_field, strict=True)
  1944. new_field_name = truncate_name(
  1945. new_field.column, connection.ops.max_name_length()
  1946. )
  1947. constraints = self.get_constraints(AuthorWithLongColumn._meta.db_table)
  1948. check_constraints = [
  1949. name
  1950. for name, details in constraints.items()
  1951. if details["columns"] == [new_field_name] and details["check"]
  1952. ]
  1953. self.assertEqual(len(check_constraints), 1)
  1954. def _test_m2m_create(self, M2MFieldClass):
  1955. """
  1956. Tests M2M fields on models during creation
  1957. """
  1958. class LocalBookWithM2M(Model):
  1959. author = ForeignKey(Author, CASCADE)
  1960. title = CharField(max_length=100, db_index=True)
  1961. pub_date = DateTimeField()
  1962. tags = M2MFieldClass("TagM2MTest", related_name="books")
  1963. class Meta:
  1964. app_label = "schema"
  1965. apps = new_apps
  1966. self.local_models = [LocalBookWithM2M]
  1967. # Create the tables
  1968. with connection.schema_editor() as editor:
  1969. editor.create_model(Author)
  1970. editor.create_model(TagM2MTest)
  1971. editor.create_model(LocalBookWithM2M)
  1972. # Ensure there is now an m2m table there
  1973. columns = self.column_classes(
  1974. LocalBookWithM2M._meta.get_field("tags").remote_field.through
  1975. )
  1976. self.assertEqual(
  1977. columns["tagm2mtest_id"][0],
  1978. connection.features.introspected_field_types["IntegerField"],
  1979. )
  1980. def test_m2m_create(self):
  1981. self._test_m2m_create(ManyToManyField)
  1982. def test_m2m_create_custom(self):
  1983. self._test_m2m_create(CustomManyToManyField)
  1984. def test_m2m_create_inherited(self):
  1985. self._test_m2m_create(InheritedManyToManyField)
  1986. def _test_m2m_create_through(self, M2MFieldClass):
  1987. """
  1988. Tests M2M fields on models during creation with through models
  1989. """
  1990. class LocalTagThrough(Model):
  1991. book = ForeignKey("schema.LocalBookWithM2MThrough", CASCADE)
  1992. tag = ForeignKey("schema.TagM2MTest", CASCADE)
  1993. class Meta:
  1994. app_label = "schema"
  1995. apps = new_apps
  1996. class LocalBookWithM2MThrough(Model):
  1997. tags = M2MFieldClass(
  1998. "TagM2MTest", related_name="books", through=LocalTagThrough
  1999. )
  2000. class Meta:
  2001. app_label = "schema"
  2002. apps = new_apps
  2003. self.local_models = [LocalTagThrough, LocalBookWithM2MThrough]
  2004. # Create the tables
  2005. with connection.schema_editor() as editor:
  2006. editor.create_model(LocalTagThrough)
  2007. editor.create_model(TagM2MTest)
  2008. editor.create_model(LocalBookWithM2MThrough)
  2009. # Ensure there is now an m2m table there
  2010. columns = self.column_classes(LocalTagThrough)
  2011. self.assertEqual(
  2012. columns["book_id"][0],
  2013. connection.features.introspected_field_types["IntegerField"],
  2014. )
  2015. self.assertEqual(
  2016. columns["tag_id"][0],
  2017. connection.features.introspected_field_types["IntegerField"],
  2018. )
  2019. def test_m2m_create_through(self):
  2020. self._test_m2m_create_through(ManyToManyField)
  2021. def test_m2m_create_through_custom(self):
  2022. self._test_m2m_create_through(CustomManyToManyField)
  2023. def test_m2m_create_through_inherited(self):
  2024. self._test_m2m_create_through(InheritedManyToManyField)
  2025. def test_m2m_through_remove(self):
  2026. class LocalAuthorNoteThrough(Model):
  2027. book = ForeignKey("schema.Author", CASCADE)
  2028. tag = ForeignKey("self", CASCADE)
  2029. class Meta:
  2030. app_label = "schema"
  2031. apps = new_apps
  2032. class LocalNoteWithM2MThrough(Model):
  2033. authors = ManyToManyField("schema.Author", through=LocalAuthorNoteThrough)
  2034. class Meta:
  2035. app_label = "schema"
  2036. apps = new_apps
  2037. self.local_models = [LocalAuthorNoteThrough, LocalNoteWithM2MThrough]
  2038. # Create the tables.
  2039. with connection.schema_editor() as editor:
  2040. editor.create_model(Author)
  2041. editor.create_model(LocalAuthorNoteThrough)
  2042. editor.create_model(LocalNoteWithM2MThrough)
  2043. # Remove the through parameter.
  2044. old_field = LocalNoteWithM2MThrough._meta.get_field("authors")
  2045. new_field = ManyToManyField("Author")
  2046. new_field.set_attributes_from_name("authors")
  2047. msg = (
  2048. f"Cannot alter field {old_field} into {new_field} - they are not "
  2049. f"compatible types (you cannot alter to or from M2M fields, or add or "
  2050. f"remove through= on M2M fields)"
  2051. )
  2052. with connection.schema_editor() as editor:
  2053. with self.assertRaisesMessage(ValueError, msg):
  2054. editor.alter_field(LocalNoteWithM2MThrough, old_field, new_field)
  2055. def _test_m2m(self, M2MFieldClass):
  2056. """
  2057. Tests adding/removing M2M fields on models
  2058. """
  2059. class LocalAuthorWithM2M(Model):
  2060. name = CharField(max_length=255)
  2061. class Meta:
  2062. app_label = "schema"
  2063. apps = new_apps
  2064. self.local_models = [LocalAuthorWithM2M]
  2065. # Create the tables
  2066. with connection.schema_editor() as editor:
  2067. editor.create_model(LocalAuthorWithM2M)
  2068. editor.create_model(TagM2MTest)
  2069. # Create an M2M field
  2070. new_field = M2MFieldClass("schema.TagM2MTest", related_name="authors")
  2071. new_field.contribute_to_class(LocalAuthorWithM2M, "tags")
  2072. # Ensure there's no m2m table there
  2073. with self.assertRaises(DatabaseError):
  2074. self.column_classes(new_field.remote_field.through)
  2075. # Add the field
  2076. with CaptureQueriesContext(
  2077. connection
  2078. ) as ctx, connection.schema_editor() as editor:
  2079. editor.add_field(LocalAuthorWithM2M, new_field)
  2080. # Table is not rebuilt.
  2081. self.assertEqual(
  2082. len(
  2083. [
  2084. query["sql"]
  2085. for query in ctx.captured_queries
  2086. if "CREATE TABLE" in query["sql"]
  2087. ]
  2088. ),
  2089. 1,
  2090. )
  2091. self.assertIs(
  2092. any("DROP TABLE" in query["sql"] for query in ctx.captured_queries),
  2093. False,
  2094. )
  2095. # Ensure there is now an m2m table there
  2096. columns = self.column_classes(new_field.remote_field.through)
  2097. self.assertEqual(
  2098. columns["tagm2mtest_id"][0],
  2099. connection.features.introspected_field_types["IntegerField"],
  2100. )
  2101. # "Alter" the field. This should not rename the DB table to itself.
  2102. with connection.schema_editor() as editor:
  2103. editor.alter_field(LocalAuthorWithM2M, new_field, new_field, strict=True)
  2104. # Remove the M2M table again
  2105. with connection.schema_editor() as editor:
  2106. editor.remove_field(LocalAuthorWithM2M, new_field)
  2107. # Ensure there's no m2m table there
  2108. with self.assertRaises(DatabaseError):
  2109. self.column_classes(new_field.remote_field.through)
  2110. # Make sure the model state is coherent with the table one now that
  2111. # we've removed the tags field.
  2112. opts = LocalAuthorWithM2M._meta
  2113. opts.local_many_to_many.remove(new_field)
  2114. del new_apps.all_models["schema"][
  2115. new_field.remote_field.through._meta.model_name
  2116. ]
  2117. opts._expire_cache()
  2118. def test_m2m(self):
  2119. self._test_m2m(ManyToManyField)
  2120. def test_m2m_custom(self):
  2121. self._test_m2m(CustomManyToManyField)
  2122. def test_m2m_inherited(self):
  2123. self._test_m2m(InheritedManyToManyField)
  2124. def _test_m2m_through_alter(self, M2MFieldClass):
  2125. """
  2126. Tests altering M2Ms with explicit through models (should no-op)
  2127. """
  2128. class LocalAuthorTag(Model):
  2129. author = ForeignKey("schema.LocalAuthorWithM2MThrough", CASCADE)
  2130. tag = ForeignKey("schema.TagM2MTest", CASCADE)
  2131. class Meta:
  2132. app_label = "schema"
  2133. apps = new_apps
  2134. class LocalAuthorWithM2MThrough(Model):
  2135. name = CharField(max_length=255)
  2136. tags = M2MFieldClass(
  2137. "schema.TagM2MTest", related_name="authors", through=LocalAuthorTag
  2138. )
  2139. class Meta:
  2140. app_label = "schema"
  2141. apps = new_apps
  2142. self.local_models = [LocalAuthorTag, LocalAuthorWithM2MThrough]
  2143. # Create the tables
  2144. with connection.schema_editor() as editor:
  2145. editor.create_model(LocalAuthorTag)
  2146. editor.create_model(LocalAuthorWithM2MThrough)
  2147. editor.create_model(TagM2MTest)
  2148. # Ensure the m2m table is there
  2149. self.assertEqual(len(self.column_classes(LocalAuthorTag)), 3)
  2150. # "Alter" the field's blankness. This should not actually do anything.
  2151. old_field = LocalAuthorWithM2MThrough._meta.get_field("tags")
  2152. new_field = M2MFieldClass(
  2153. "schema.TagM2MTest", related_name="authors", through=LocalAuthorTag
  2154. )
  2155. new_field.contribute_to_class(LocalAuthorWithM2MThrough, "tags")
  2156. with connection.schema_editor() as editor:
  2157. editor.alter_field(
  2158. LocalAuthorWithM2MThrough, old_field, new_field, strict=True
  2159. )
  2160. # Ensure the m2m table is still there
  2161. self.assertEqual(len(self.column_classes(LocalAuthorTag)), 3)
  2162. def test_m2m_through_alter(self):
  2163. self._test_m2m_through_alter(ManyToManyField)
  2164. def test_m2m_through_alter_custom(self):
  2165. self._test_m2m_through_alter(CustomManyToManyField)
  2166. def test_m2m_through_alter_inherited(self):
  2167. self._test_m2m_through_alter(InheritedManyToManyField)
  2168. def _test_m2m_repoint(self, M2MFieldClass):
  2169. """
  2170. Tests repointing M2M fields
  2171. """
  2172. class LocalBookWithM2M(Model):
  2173. author = ForeignKey(Author, CASCADE)
  2174. title = CharField(max_length=100, db_index=True)
  2175. pub_date = DateTimeField()
  2176. tags = M2MFieldClass("TagM2MTest", related_name="books")
  2177. class Meta:
  2178. app_label = "schema"
  2179. apps = new_apps
  2180. self.local_models = [LocalBookWithM2M]
  2181. # Create the tables
  2182. with connection.schema_editor() as editor:
  2183. editor.create_model(Author)
  2184. editor.create_model(LocalBookWithM2M)
  2185. editor.create_model(TagM2MTest)
  2186. editor.create_model(UniqueTest)
  2187. # Ensure the M2M exists and points to TagM2MTest
  2188. if connection.features.supports_foreign_keys:
  2189. self.assertForeignKeyExists(
  2190. LocalBookWithM2M._meta.get_field("tags").remote_field.through,
  2191. "tagm2mtest_id",
  2192. "schema_tagm2mtest",
  2193. )
  2194. # Repoint the M2M
  2195. old_field = LocalBookWithM2M._meta.get_field("tags")
  2196. new_field = M2MFieldClass(UniqueTest)
  2197. new_field.contribute_to_class(LocalBookWithM2M, "uniques")
  2198. with connection.schema_editor() as editor:
  2199. editor.alter_field(LocalBookWithM2M, old_field, new_field, strict=True)
  2200. # Ensure old M2M is gone
  2201. with self.assertRaises(DatabaseError):
  2202. self.column_classes(
  2203. LocalBookWithM2M._meta.get_field("tags").remote_field.through
  2204. )
  2205. # This model looks like the new model and is used for teardown.
  2206. opts = LocalBookWithM2M._meta
  2207. opts.local_many_to_many.remove(old_field)
  2208. # Ensure the new M2M exists and points to UniqueTest
  2209. if connection.features.supports_foreign_keys:
  2210. self.assertForeignKeyExists(
  2211. new_field.remote_field.through, "uniquetest_id", "schema_uniquetest"
  2212. )
  2213. def test_m2m_repoint(self):
  2214. self._test_m2m_repoint(ManyToManyField)
  2215. def test_m2m_repoint_custom(self):
  2216. self._test_m2m_repoint(CustomManyToManyField)
  2217. def test_m2m_repoint_inherited(self):
  2218. self._test_m2m_repoint(InheritedManyToManyField)
  2219. @isolate_apps("schema")
  2220. def test_m2m_rename_field_in_target_model(self):
  2221. class LocalTagM2MTest(Model):
  2222. title = CharField(max_length=255)
  2223. class Meta:
  2224. app_label = "schema"
  2225. class LocalM2M(Model):
  2226. tags = ManyToManyField(LocalTagM2MTest)
  2227. class Meta:
  2228. app_label = "schema"
  2229. # Create the tables.
  2230. with connection.schema_editor() as editor:
  2231. editor.create_model(LocalM2M)
  2232. editor.create_model(LocalTagM2MTest)
  2233. self.isolated_local_models = [LocalM2M, LocalTagM2MTest]
  2234. # Ensure the m2m table is there.
  2235. self.assertEqual(len(self.column_classes(LocalM2M)), 1)
  2236. # Alter a field in LocalTagM2MTest.
  2237. old_field = LocalTagM2MTest._meta.get_field("title")
  2238. new_field = CharField(max_length=254)
  2239. new_field.contribute_to_class(LocalTagM2MTest, "title1")
  2240. # @isolate_apps() and inner models are needed to have the model
  2241. # relations populated, otherwise this doesn't act as a regression test.
  2242. self.assertEqual(len(new_field.model._meta.related_objects), 1)
  2243. with connection.schema_editor() as editor:
  2244. editor.alter_field(LocalTagM2MTest, old_field, new_field, strict=True)
  2245. # Ensure the m2m table is still there.
  2246. self.assertEqual(len(self.column_classes(LocalM2M)), 1)
  2247. @skipUnlessDBFeature(
  2248. "supports_column_check_constraints", "can_introspect_check_constraints"
  2249. )
  2250. def test_check_constraints(self):
  2251. """
  2252. Tests creating/deleting CHECK constraints
  2253. """
  2254. # Create the tables
  2255. with connection.schema_editor() as editor:
  2256. editor.create_model(Author)
  2257. # Ensure the constraint exists
  2258. constraints = self.get_constraints(Author._meta.db_table)
  2259. if not any(
  2260. details["columns"] == ["height"] and details["check"]
  2261. for details in constraints.values()
  2262. ):
  2263. self.fail("No check constraint for height found")
  2264. # Alter the column to remove it
  2265. old_field = Author._meta.get_field("height")
  2266. new_field = IntegerField(null=True, blank=True)
  2267. new_field.set_attributes_from_name("height")
  2268. with connection.schema_editor() as editor:
  2269. editor.alter_field(Author, old_field, new_field, strict=True)
  2270. constraints = self.get_constraints(Author._meta.db_table)
  2271. for details in constraints.values():
  2272. if details["columns"] == ["height"] and details["check"]:
  2273. self.fail("Check constraint for height found")
  2274. # Alter the column to re-add it
  2275. new_field2 = Author._meta.get_field("height")
  2276. with connection.schema_editor() as editor:
  2277. editor.alter_field(Author, new_field, new_field2, strict=True)
  2278. constraints = self.get_constraints(Author._meta.db_table)
  2279. if not any(
  2280. details["columns"] == ["height"] and details["check"]
  2281. for details in constraints.values()
  2282. ):
  2283. self.fail("No check constraint for height found")
  2284. @skipUnlessDBFeature(
  2285. "supports_column_check_constraints", "can_introspect_check_constraints"
  2286. )
  2287. @isolate_apps("schema")
  2288. def test_check_constraint_timedelta_param(self):
  2289. class DurationModel(Model):
  2290. duration = DurationField()
  2291. class Meta:
  2292. app_label = "schema"
  2293. with connection.schema_editor() as editor:
  2294. editor.create_model(DurationModel)
  2295. self.isolated_local_models = [DurationModel]
  2296. constraint_name = "duration_gte_5_minutes"
  2297. constraint = CheckConstraint(
  2298. check=Q(duration__gt=datetime.timedelta(minutes=5)),
  2299. name=constraint_name,
  2300. )
  2301. DurationModel._meta.constraints = [constraint]
  2302. with connection.schema_editor() as editor:
  2303. editor.add_constraint(DurationModel, constraint)
  2304. constraints = self.get_constraints(DurationModel._meta.db_table)
  2305. self.assertIn(constraint_name, constraints)
  2306. with self.assertRaises(IntegrityError), atomic():
  2307. DurationModel.objects.create(duration=datetime.timedelta(minutes=4))
  2308. DurationModel.objects.create(duration=datetime.timedelta(minutes=10))
  2309. @skipUnlessDBFeature(
  2310. "supports_column_check_constraints", "can_introspect_check_constraints"
  2311. )
  2312. def test_remove_field_check_does_not_remove_meta_constraints(self):
  2313. with connection.schema_editor() as editor:
  2314. editor.create_model(Author)
  2315. # Add the custom check constraint
  2316. constraint = CheckConstraint(
  2317. check=Q(height__gte=0), name="author_height_gte_0_check"
  2318. )
  2319. custom_constraint_name = constraint.name
  2320. Author._meta.constraints = [constraint]
  2321. with connection.schema_editor() as editor:
  2322. editor.add_constraint(Author, constraint)
  2323. # Ensure the constraints exist
  2324. constraints = self.get_constraints(Author._meta.db_table)
  2325. self.assertIn(custom_constraint_name, constraints)
  2326. other_constraints = [
  2327. name
  2328. for name, details in constraints.items()
  2329. if details["columns"] == ["height"]
  2330. and details["check"]
  2331. and name != custom_constraint_name
  2332. ]
  2333. self.assertEqual(len(other_constraints), 1)
  2334. # Alter the column to remove field check
  2335. old_field = Author._meta.get_field("height")
  2336. new_field = IntegerField(null=True, blank=True)
  2337. new_field.set_attributes_from_name("height")
  2338. with connection.schema_editor() as editor:
  2339. editor.alter_field(Author, old_field, new_field, strict=True)
  2340. constraints = self.get_constraints(Author._meta.db_table)
  2341. self.assertIn(custom_constraint_name, constraints)
  2342. other_constraints = [
  2343. name
  2344. for name, details in constraints.items()
  2345. if details["columns"] == ["height"]
  2346. and details["check"]
  2347. and name != custom_constraint_name
  2348. ]
  2349. self.assertEqual(len(other_constraints), 0)
  2350. # Alter the column to re-add field check
  2351. new_field2 = Author._meta.get_field("height")
  2352. with connection.schema_editor() as editor:
  2353. editor.alter_field(Author, new_field, new_field2, strict=True)
  2354. constraints = self.get_constraints(Author._meta.db_table)
  2355. self.assertIn(custom_constraint_name, constraints)
  2356. other_constraints = [
  2357. name
  2358. for name, details in constraints.items()
  2359. if details["columns"] == ["height"]
  2360. and details["check"]
  2361. and name != custom_constraint_name
  2362. ]
  2363. self.assertEqual(len(other_constraints), 1)
  2364. # Drop the check constraint
  2365. with connection.schema_editor() as editor:
  2366. Author._meta.constraints = []
  2367. editor.remove_constraint(Author, constraint)
  2368. def test_unique(self):
  2369. """
  2370. Tests removing and adding unique constraints to a single column.
  2371. """
  2372. # Create the table
  2373. with connection.schema_editor() as editor:
  2374. editor.create_model(Tag)
  2375. # Ensure the field is unique to begin with
  2376. Tag.objects.create(title="foo", slug="foo")
  2377. with self.assertRaises(IntegrityError):
  2378. Tag.objects.create(title="bar", slug="foo")
  2379. Tag.objects.all().delete()
  2380. # Alter the slug field to be non-unique
  2381. old_field = Tag._meta.get_field("slug")
  2382. new_field = SlugField(unique=False)
  2383. new_field.set_attributes_from_name("slug")
  2384. with connection.schema_editor() as editor:
  2385. editor.alter_field(Tag, old_field, new_field, strict=True)
  2386. # Ensure the field is no longer unique
  2387. Tag.objects.create(title="foo", slug="foo")
  2388. Tag.objects.create(title="bar", slug="foo")
  2389. Tag.objects.all().delete()
  2390. # Alter the slug field to be unique
  2391. new_field2 = SlugField(unique=True)
  2392. new_field2.set_attributes_from_name("slug")
  2393. with connection.schema_editor() as editor:
  2394. editor.alter_field(Tag, new_field, new_field2, strict=True)
  2395. # Ensure the field is unique again
  2396. Tag.objects.create(title="foo", slug="foo")
  2397. with self.assertRaises(IntegrityError):
  2398. Tag.objects.create(title="bar", slug="foo")
  2399. Tag.objects.all().delete()
  2400. # Rename the field
  2401. new_field3 = SlugField(unique=True)
  2402. new_field3.set_attributes_from_name("slug2")
  2403. with connection.schema_editor() as editor:
  2404. editor.alter_field(Tag, new_field2, new_field3, strict=True)
  2405. # Ensure the field is still unique
  2406. TagUniqueRename.objects.create(title="foo", slug2="foo")
  2407. with self.assertRaises(IntegrityError):
  2408. TagUniqueRename.objects.create(title="bar", slug2="foo")
  2409. Tag.objects.all().delete()
  2410. def test_unique_name_quoting(self):
  2411. old_table_name = TagUniqueRename._meta.db_table
  2412. try:
  2413. with connection.schema_editor() as editor:
  2414. editor.create_model(TagUniqueRename)
  2415. editor.alter_db_table(TagUniqueRename, old_table_name, "unique-table")
  2416. TagUniqueRename._meta.db_table = "unique-table"
  2417. # This fails if the unique index name isn't quoted.
  2418. editor.alter_unique_together(TagUniqueRename, [], (("title", "slug2"),))
  2419. finally:
  2420. with connection.schema_editor() as editor:
  2421. editor.delete_model(TagUniqueRename)
  2422. TagUniqueRename._meta.db_table = old_table_name
  2423. @isolate_apps("schema")
  2424. @skipUnlessDBFeature("supports_foreign_keys")
  2425. def test_unique_no_unnecessary_fk_drops(self):
  2426. """
  2427. If AlterField isn't selective about dropping foreign key constraints
  2428. when modifying a field with a unique constraint, the AlterField
  2429. incorrectly drops and recreates the Book.author foreign key even though
  2430. it doesn't restrict the field being changed (#29193).
  2431. """
  2432. class Author(Model):
  2433. name = CharField(max_length=254, unique=True)
  2434. class Meta:
  2435. app_label = "schema"
  2436. class Book(Model):
  2437. author = ForeignKey(Author, CASCADE)
  2438. class Meta:
  2439. app_label = "schema"
  2440. with connection.schema_editor() as editor:
  2441. editor.create_model(Author)
  2442. editor.create_model(Book)
  2443. new_field = CharField(max_length=255, unique=True)
  2444. new_field.model = Author
  2445. new_field.set_attributes_from_name("name")
  2446. with self.assertLogs("django.db.backends.schema", "DEBUG") as cm:
  2447. with connection.schema_editor() as editor:
  2448. editor.alter_field(Author, Author._meta.get_field("name"), new_field)
  2449. # One SQL statement is executed to alter the field.
  2450. self.assertEqual(len(cm.records), 1)
  2451. @isolate_apps("schema")
  2452. def test_unique_and_reverse_m2m(self):
  2453. """
  2454. AlterField can modify a unique field when there's a reverse M2M
  2455. relation on the model.
  2456. """
  2457. class Tag(Model):
  2458. title = CharField(max_length=255)
  2459. slug = SlugField(unique=True)
  2460. class Meta:
  2461. app_label = "schema"
  2462. class Book(Model):
  2463. tags = ManyToManyField(Tag, related_name="books")
  2464. class Meta:
  2465. app_label = "schema"
  2466. self.isolated_local_models = [Book._meta.get_field("tags").remote_field.through]
  2467. with connection.schema_editor() as editor:
  2468. editor.create_model(Tag)
  2469. editor.create_model(Book)
  2470. new_field = SlugField(max_length=75, unique=True)
  2471. new_field.model = Tag
  2472. new_field.set_attributes_from_name("slug")
  2473. with self.assertLogs("django.db.backends.schema", "DEBUG") as cm:
  2474. with connection.schema_editor() as editor:
  2475. editor.alter_field(Tag, Tag._meta.get_field("slug"), new_field)
  2476. # One SQL statement is executed to alter the field.
  2477. self.assertEqual(len(cm.records), 1)
  2478. # Ensure that the field is still unique.
  2479. Tag.objects.create(title="foo", slug="foo")
  2480. with self.assertRaises(IntegrityError):
  2481. Tag.objects.create(title="bar", slug="foo")
  2482. def test_remove_ignored_unique_constraint_not_create_fk_index(self):
  2483. with connection.schema_editor() as editor:
  2484. editor.create_model(Author)
  2485. editor.create_model(Book)
  2486. constraint = UniqueConstraint(
  2487. "author",
  2488. condition=Q(title__in=["tHGttG", "tRatEotU"]),
  2489. name="book_author_condition_uniq",
  2490. )
  2491. # Add unique constraint.
  2492. with connection.schema_editor() as editor:
  2493. editor.add_constraint(Book, constraint)
  2494. old_constraints = self.get_constraints_for_column(
  2495. Book,
  2496. Book._meta.get_field("author").column,
  2497. )
  2498. # Remove unique constraint.
  2499. with connection.schema_editor() as editor:
  2500. editor.remove_constraint(Book, constraint)
  2501. new_constraints = self.get_constraints_for_column(
  2502. Book,
  2503. Book._meta.get_field("author").column,
  2504. )
  2505. # Redundant foreign key index is not added.
  2506. self.assertEqual(
  2507. len(old_constraints) - 1
  2508. if connection.features.supports_partial_indexes
  2509. else len(old_constraints),
  2510. len(new_constraints),
  2511. )
  2512. @skipUnlessDBFeature("allows_multiple_constraints_on_same_fields")
  2513. def test_remove_field_unique_does_not_remove_meta_constraints(self):
  2514. with connection.schema_editor() as editor:
  2515. editor.create_model(AuthorWithUniqueName)
  2516. self.local_models = [AuthorWithUniqueName]
  2517. # Add the custom unique constraint
  2518. constraint = UniqueConstraint(fields=["name"], name="author_name_uniq")
  2519. custom_constraint_name = constraint.name
  2520. AuthorWithUniqueName._meta.constraints = [constraint]
  2521. with connection.schema_editor() as editor:
  2522. editor.add_constraint(AuthorWithUniqueName, constraint)
  2523. # Ensure the constraints exist
  2524. constraints = self.get_constraints(AuthorWithUniqueName._meta.db_table)
  2525. self.assertIn(custom_constraint_name, constraints)
  2526. other_constraints = [
  2527. name
  2528. for name, details in constraints.items()
  2529. if details["columns"] == ["name"]
  2530. and details["unique"]
  2531. and name != custom_constraint_name
  2532. ]
  2533. self.assertEqual(len(other_constraints), 1)
  2534. # Alter the column to remove field uniqueness
  2535. old_field = AuthorWithUniqueName._meta.get_field("name")
  2536. new_field = CharField(max_length=255)
  2537. new_field.set_attributes_from_name("name")
  2538. with connection.schema_editor() as editor:
  2539. editor.alter_field(AuthorWithUniqueName, old_field, new_field, strict=True)
  2540. constraints = self.get_constraints(AuthorWithUniqueName._meta.db_table)
  2541. self.assertIn(custom_constraint_name, constraints)
  2542. other_constraints = [
  2543. name
  2544. for name, details in constraints.items()
  2545. if details["columns"] == ["name"]
  2546. and details["unique"]
  2547. and name != custom_constraint_name
  2548. ]
  2549. self.assertEqual(len(other_constraints), 0)
  2550. # Alter the column to re-add field uniqueness
  2551. new_field2 = AuthorWithUniqueName._meta.get_field("name")
  2552. with connection.schema_editor() as editor:
  2553. editor.alter_field(AuthorWithUniqueName, new_field, new_field2, strict=True)
  2554. constraints = self.get_constraints(AuthorWithUniqueName._meta.db_table)
  2555. self.assertIn(custom_constraint_name, constraints)
  2556. other_constraints = [
  2557. name
  2558. for name, details in constraints.items()
  2559. if details["columns"] == ["name"]
  2560. and details["unique"]
  2561. and name != custom_constraint_name
  2562. ]
  2563. self.assertEqual(len(other_constraints), 1)
  2564. # Drop the unique constraint
  2565. with connection.schema_editor() as editor:
  2566. AuthorWithUniqueName._meta.constraints = []
  2567. editor.remove_constraint(AuthorWithUniqueName, constraint)
  2568. def test_unique_together(self):
  2569. """
  2570. Tests removing and adding unique_together constraints on a model.
  2571. """
  2572. # Create the table
  2573. with connection.schema_editor() as editor:
  2574. editor.create_model(UniqueTest)
  2575. # Ensure the fields are unique to begin with
  2576. UniqueTest.objects.create(year=2012, slug="foo")
  2577. UniqueTest.objects.create(year=2011, slug="foo")
  2578. UniqueTest.objects.create(year=2011, slug="bar")
  2579. with self.assertRaises(IntegrityError):
  2580. UniqueTest.objects.create(year=2012, slug="foo")
  2581. UniqueTest.objects.all().delete()
  2582. # Alter the model to its non-unique-together companion
  2583. with connection.schema_editor() as editor:
  2584. editor.alter_unique_together(
  2585. UniqueTest, UniqueTest._meta.unique_together, []
  2586. )
  2587. # Ensure the fields are no longer unique
  2588. UniqueTest.objects.create(year=2012, slug="foo")
  2589. UniqueTest.objects.create(year=2012, slug="foo")
  2590. UniqueTest.objects.all().delete()
  2591. # Alter it back
  2592. new_field2 = SlugField(unique=True)
  2593. new_field2.set_attributes_from_name("slug")
  2594. with connection.schema_editor() as editor:
  2595. editor.alter_unique_together(
  2596. UniqueTest, [], UniqueTest._meta.unique_together
  2597. )
  2598. # Ensure the fields are unique again
  2599. UniqueTest.objects.create(year=2012, slug="foo")
  2600. with self.assertRaises(IntegrityError):
  2601. UniqueTest.objects.create(year=2012, slug="foo")
  2602. UniqueTest.objects.all().delete()
  2603. def test_unique_together_with_fk(self):
  2604. """
  2605. Tests removing and adding unique_together constraints that include
  2606. a foreign key.
  2607. """
  2608. # Create the table
  2609. with connection.schema_editor() as editor:
  2610. editor.create_model(Author)
  2611. editor.create_model(Book)
  2612. # Ensure the fields are unique to begin with
  2613. self.assertEqual(Book._meta.unique_together, ())
  2614. # Add the unique_together constraint
  2615. with connection.schema_editor() as editor:
  2616. editor.alter_unique_together(Book, [], [["author", "title"]])
  2617. # Alter it back
  2618. with connection.schema_editor() as editor:
  2619. editor.alter_unique_together(Book, [["author", "title"]], [])
  2620. def test_unique_together_with_fk_with_existing_index(self):
  2621. """
  2622. Tests removing and adding unique_together constraints that include
  2623. a foreign key, where the foreign key is added after the model is
  2624. created.
  2625. """
  2626. # Create the tables
  2627. with connection.schema_editor() as editor:
  2628. editor.create_model(Author)
  2629. editor.create_model(BookWithoutAuthor)
  2630. new_field = ForeignKey(Author, CASCADE)
  2631. new_field.set_attributes_from_name("author")
  2632. editor.add_field(BookWithoutAuthor, new_field)
  2633. # Ensure the fields aren't unique to begin with
  2634. self.assertEqual(Book._meta.unique_together, ())
  2635. # Add the unique_together constraint
  2636. with connection.schema_editor() as editor:
  2637. editor.alter_unique_together(Book, [], [["author", "title"]])
  2638. # Alter it back
  2639. with connection.schema_editor() as editor:
  2640. editor.alter_unique_together(Book, [["author", "title"]], [])
  2641. def _test_composed_index_with_fk(self, index):
  2642. with connection.schema_editor() as editor:
  2643. editor.create_model(Author)
  2644. editor.create_model(Book)
  2645. table = Book._meta.db_table
  2646. self.assertEqual(Book._meta.indexes, [])
  2647. Book._meta.indexes = [index]
  2648. with connection.schema_editor() as editor:
  2649. editor.add_index(Book, index)
  2650. self.assertIn(index.name, self.get_constraints(table))
  2651. Book._meta.indexes = []
  2652. with connection.schema_editor() as editor:
  2653. editor.remove_index(Book, index)
  2654. self.assertNotIn(index.name, self.get_constraints(table))
  2655. def test_composed_index_with_fk(self):
  2656. index = Index(fields=["author", "title"], name="book_author_title_idx")
  2657. self._test_composed_index_with_fk(index)
  2658. def test_composed_desc_index_with_fk(self):
  2659. index = Index(fields=["-author", "title"], name="book_author_title_idx")
  2660. self._test_composed_index_with_fk(index)
  2661. @skipUnlessDBFeature("supports_expression_indexes")
  2662. def test_composed_func_index_with_fk(self):
  2663. index = Index(F("author"), F("title"), name="book_author_title_idx")
  2664. self._test_composed_index_with_fk(index)
  2665. @skipUnlessDBFeature("supports_expression_indexes")
  2666. def test_composed_desc_func_index_with_fk(self):
  2667. index = Index(F("author").desc(), F("title"), name="book_author_title_idx")
  2668. self._test_composed_index_with_fk(index)
  2669. @skipUnlessDBFeature("supports_expression_indexes")
  2670. def test_composed_func_transform_index_with_fk(self):
  2671. index = Index(F("title__lower"), name="book_title_lower_idx")
  2672. with register_lookup(CharField, Lower):
  2673. self._test_composed_index_with_fk(index)
  2674. def _test_composed_constraint_with_fk(self, constraint):
  2675. with connection.schema_editor() as editor:
  2676. editor.create_model(Author)
  2677. editor.create_model(Book)
  2678. table = Book._meta.db_table
  2679. self.assertEqual(Book._meta.constraints, [])
  2680. Book._meta.constraints = [constraint]
  2681. with connection.schema_editor() as editor:
  2682. editor.add_constraint(Book, constraint)
  2683. self.assertIn(constraint.name, self.get_constraints(table))
  2684. Book._meta.constraints = []
  2685. with connection.schema_editor() as editor:
  2686. editor.remove_constraint(Book, constraint)
  2687. self.assertNotIn(constraint.name, self.get_constraints(table))
  2688. def test_composed_constraint_with_fk(self):
  2689. constraint = UniqueConstraint(
  2690. fields=["author", "title"],
  2691. name="book_author_title_uniq",
  2692. )
  2693. self._test_composed_constraint_with_fk(constraint)
  2694. @skipUnlessDBFeature(
  2695. "supports_column_check_constraints", "can_introspect_check_constraints"
  2696. )
  2697. def test_composed_check_constraint_with_fk(self):
  2698. constraint = CheckConstraint(check=Q(author__gt=0), name="book_author_check")
  2699. self._test_composed_constraint_with_fk(constraint)
  2700. @skipUnlessDBFeature("allows_multiple_constraints_on_same_fields")
  2701. def test_remove_unique_together_does_not_remove_meta_constraints(self):
  2702. with connection.schema_editor() as editor:
  2703. editor.create_model(AuthorWithUniqueNameAndBirthday)
  2704. self.local_models = [AuthorWithUniqueNameAndBirthday]
  2705. # Add the custom unique constraint
  2706. constraint = UniqueConstraint(
  2707. fields=["name", "birthday"], name="author_name_birthday_uniq"
  2708. )
  2709. custom_constraint_name = constraint.name
  2710. AuthorWithUniqueNameAndBirthday._meta.constraints = [constraint]
  2711. with connection.schema_editor() as editor:
  2712. editor.add_constraint(AuthorWithUniqueNameAndBirthday, constraint)
  2713. # Ensure the constraints exist
  2714. constraints = self.get_constraints(
  2715. AuthorWithUniqueNameAndBirthday._meta.db_table
  2716. )
  2717. self.assertIn(custom_constraint_name, constraints)
  2718. other_constraints = [
  2719. name
  2720. for name, details in constraints.items()
  2721. if details["columns"] == ["name", "birthday"]
  2722. and details["unique"]
  2723. and name != custom_constraint_name
  2724. ]
  2725. self.assertEqual(len(other_constraints), 1)
  2726. # Remove unique together
  2727. unique_together = AuthorWithUniqueNameAndBirthday._meta.unique_together
  2728. with connection.schema_editor() as editor:
  2729. editor.alter_unique_together(
  2730. AuthorWithUniqueNameAndBirthday, unique_together, []
  2731. )
  2732. constraints = self.get_constraints(
  2733. AuthorWithUniqueNameAndBirthday._meta.db_table
  2734. )
  2735. self.assertIn(custom_constraint_name, constraints)
  2736. other_constraints = [
  2737. name
  2738. for name, details in constraints.items()
  2739. if details["columns"] == ["name", "birthday"]
  2740. and details["unique"]
  2741. and name != custom_constraint_name
  2742. ]
  2743. self.assertEqual(len(other_constraints), 0)
  2744. # Re-add unique together
  2745. with connection.schema_editor() as editor:
  2746. editor.alter_unique_together(
  2747. AuthorWithUniqueNameAndBirthday, [], unique_together
  2748. )
  2749. constraints = self.get_constraints(
  2750. AuthorWithUniqueNameAndBirthday._meta.db_table
  2751. )
  2752. self.assertIn(custom_constraint_name, constraints)
  2753. other_constraints = [
  2754. name
  2755. for name, details in constraints.items()
  2756. if details["columns"] == ["name", "birthday"]
  2757. and details["unique"]
  2758. and name != custom_constraint_name
  2759. ]
  2760. self.assertEqual(len(other_constraints), 1)
  2761. # Drop the unique constraint
  2762. with connection.schema_editor() as editor:
  2763. AuthorWithUniqueNameAndBirthday._meta.constraints = []
  2764. editor.remove_constraint(AuthorWithUniqueNameAndBirthday, constraint)
  2765. def test_unique_constraint(self):
  2766. with connection.schema_editor() as editor:
  2767. editor.create_model(Author)
  2768. constraint = UniqueConstraint(fields=["name"], name="name_uq")
  2769. # Add constraint.
  2770. with connection.schema_editor() as editor:
  2771. editor.add_constraint(Author, constraint)
  2772. sql = constraint.create_sql(Author, editor)
  2773. table = Author._meta.db_table
  2774. self.assertIs(sql.references_table(table), True)
  2775. self.assertIs(sql.references_column(table, "name"), True)
  2776. # Remove constraint.
  2777. with connection.schema_editor() as editor:
  2778. editor.remove_constraint(Author, constraint)
  2779. self.assertNotIn(constraint.name, self.get_constraints(table))
  2780. @skipUnlessDBFeature("supports_expression_indexes")
  2781. def test_func_unique_constraint(self):
  2782. with connection.schema_editor() as editor:
  2783. editor.create_model(Author)
  2784. constraint = UniqueConstraint(Upper("name").desc(), name="func_upper_uq")
  2785. # Add constraint.
  2786. with connection.schema_editor() as editor:
  2787. editor.add_constraint(Author, constraint)
  2788. sql = constraint.create_sql(Author, editor)
  2789. table = Author._meta.db_table
  2790. constraints = self.get_constraints(table)
  2791. if connection.features.supports_index_column_ordering:
  2792. self.assertIndexOrder(table, constraint.name, ["DESC"])
  2793. self.assertIn(constraint.name, constraints)
  2794. self.assertIs(constraints[constraint.name]["unique"], True)
  2795. # SQL contains a database function.
  2796. self.assertIs(sql.references_column(table, "name"), True)
  2797. self.assertIn("UPPER(%s)" % editor.quote_name("name"), str(sql))
  2798. # Remove constraint.
  2799. with connection.schema_editor() as editor:
  2800. editor.remove_constraint(Author, constraint)
  2801. self.assertNotIn(constraint.name, self.get_constraints(table))
  2802. @skipUnlessDBFeature("supports_expression_indexes")
  2803. def test_composite_func_unique_constraint(self):
  2804. with connection.schema_editor() as editor:
  2805. editor.create_model(Author)
  2806. editor.create_model(BookWithSlug)
  2807. constraint = UniqueConstraint(
  2808. Upper("title"),
  2809. Lower("slug"),
  2810. name="func_upper_lower_unq",
  2811. )
  2812. # Add constraint.
  2813. with connection.schema_editor() as editor:
  2814. editor.add_constraint(BookWithSlug, constraint)
  2815. sql = constraint.create_sql(BookWithSlug, editor)
  2816. table = BookWithSlug._meta.db_table
  2817. constraints = self.get_constraints(table)
  2818. self.assertIn(constraint.name, constraints)
  2819. self.assertIs(constraints[constraint.name]["unique"], True)
  2820. # SQL contains database functions.
  2821. self.assertIs(sql.references_column(table, "title"), True)
  2822. self.assertIs(sql.references_column(table, "slug"), True)
  2823. sql = str(sql)
  2824. self.assertIn("UPPER(%s)" % editor.quote_name("title"), sql)
  2825. self.assertIn("LOWER(%s)" % editor.quote_name("slug"), sql)
  2826. self.assertLess(sql.index("UPPER"), sql.index("LOWER"))
  2827. # Remove constraint.
  2828. with connection.schema_editor() as editor:
  2829. editor.remove_constraint(BookWithSlug, constraint)
  2830. self.assertNotIn(constraint.name, self.get_constraints(table))
  2831. @skipUnlessDBFeature("supports_expression_indexes")
  2832. def test_unique_constraint_field_and_expression(self):
  2833. with connection.schema_editor() as editor:
  2834. editor.create_model(Author)
  2835. constraint = UniqueConstraint(
  2836. F("height").desc(),
  2837. "uuid",
  2838. Lower("name").asc(),
  2839. name="func_f_lower_field_unq",
  2840. )
  2841. # Add constraint.
  2842. with connection.schema_editor() as editor:
  2843. editor.add_constraint(Author, constraint)
  2844. sql = constraint.create_sql(Author, editor)
  2845. table = Author._meta.db_table
  2846. if connection.features.supports_index_column_ordering:
  2847. self.assertIndexOrder(table, constraint.name, ["DESC", "ASC", "ASC"])
  2848. constraints = self.get_constraints(table)
  2849. self.assertIs(constraints[constraint.name]["unique"], True)
  2850. self.assertEqual(len(constraints[constraint.name]["columns"]), 3)
  2851. self.assertEqual(constraints[constraint.name]["columns"][1], "uuid")
  2852. # SQL contains database functions and columns.
  2853. self.assertIs(sql.references_column(table, "height"), True)
  2854. self.assertIs(sql.references_column(table, "name"), True)
  2855. self.assertIs(sql.references_column(table, "uuid"), True)
  2856. self.assertIn("LOWER(%s)" % editor.quote_name("name"), str(sql))
  2857. # Remove constraint.
  2858. with connection.schema_editor() as editor:
  2859. editor.remove_constraint(Author, constraint)
  2860. self.assertNotIn(constraint.name, self.get_constraints(table))
  2861. @skipUnlessDBFeature("supports_expression_indexes", "supports_partial_indexes")
  2862. def test_func_unique_constraint_partial(self):
  2863. with connection.schema_editor() as editor:
  2864. editor.create_model(Author)
  2865. constraint = UniqueConstraint(
  2866. Upper("name"),
  2867. name="func_upper_cond_weight_uq",
  2868. condition=Q(weight__isnull=False),
  2869. )
  2870. # Add constraint.
  2871. with connection.schema_editor() as editor:
  2872. editor.add_constraint(Author, constraint)
  2873. sql = constraint.create_sql(Author, editor)
  2874. table = Author._meta.db_table
  2875. constraints = self.get_constraints(table)
  2876. self.assertIn(constraint.name, constraints)
  2877. self.assertIs(constraints[constraint.name]["unique"], True)
  2878. self.assertIs(sql.references_column(table, "name"), True)
  2879. self.assertIn("UPPER(%s)" % editor.quote_name("name"), str(sql))
  2880. self.assertIn(
  2881. "WHERE %s IS NOT NULL" % editor.quote_name("weight"),
  2882. str(sql),
  2883. )
  2884. # Remove constraint.
  2885. with connection.schema_editor() as editor:
  2886. editor.remove_constraint(Author, constraint)
  2887. self.assertNotIn(constraint.name, self.get_constraints(table))
  2888. @skipUnlessDBFeature("supports_expression_indexes", "supports_covering_indexes")
  2889. def test_func_unique_constraint_covering(self):
  2890. with connection.schema_editor() as editor:
  2891. editor.create_model(Author)
  2892. constraint = UniqueConstraint(
  2893. Upper("name"),
  2894. name="func_upper_covering_uq",
  2895. include=["weight", "height"],
  2896. )
  2897. # Add constraint.
  2898. with connection.schema_editor() as editor:
  2899. editor.add_constraint(Author, constraint)
  2900. sql = constraint.create_sql(Author, editor)
  2901. table = Author._meta.db_table
  2902. constraints = self.get_constraints(table)
  2903. self.assertIn(constraint.name, constraints)
  2904. self.assertIs(constraints[constraint.name]["unique"], True)
  2905. self.assertEqual(
  2906. constraints[constraint.name]["columns"],
  2907. [None, "weight", "height"],
  2908. )
  2909. self.assertIs(sql.references_column(table, "name"), True)
  2910. self.assertIs(sql.references_column(table, "weight"), True)
  2911. self.assertIs(sql.references_column(table, "height"), True)
  2912. self.assertIn("UPPER(%s)" % editor.quote_name("name"), str(sql))
  2913. self.assertIn(
  2914. "INCLUDE (%s, %s)"
  2915. % (
  2916. editor.quote_name("weight"),
  2917. editor.quote_name("height"),
  2918. ),
  2919. str(sql),
  2920. )
  2921. # Remove constraint.
  2922. with connection.schema_editor() as editor:
  2923. editor.remove_constraint(Author, constraint)
  2924. self.assertNotIn(constraint.name, self.get_constraints(table))
  2925. @skipUnlessDBFeature("supports_expression_indexes")
  2926. def test_func_unique_constraint_lookups(self):
  2927. with connection.schema_editor() as editor:
  2928. editor.create_model(Author)
  2929. with register_lookup(CharField, Lower), register_lookup(IntegerField, Abs):
  2930. constraint = UniqueConstraint(
  2931. F("name__lower"),
  2932. F("weight__abs"),
  2933. name="func_lower_abs_lookup_uq",
  2934. )
  2935. # Add constraint.
  2936. with connection.schema_editor() as editor:
  2937. editor.add_constraint(Author, constraint)
  2938. sql = constraint.create_sql(Author, editor)
  2939. table = Author._meta.db_table
  2940. constraints = self.get_constraints(table)
  2941. self.assertIn(constraint.name, constraints)
  2942. self.assertIs(constraints[constraint.name]["unique"], True)
  2943. # SQL contains columns.
  2944. self.assertIs(sql.references_column(table, "name"), True)
  2945. self.assertIs(sql.references_column(table, "weight"), True)
  2946. # Remove constraint.
  2947. with connection.schema_editor() as editor:
  2948. editor.remove_constraint(Author, constraint)
  2949. self.assertNotIn(constraint.name, self.get_constraints(table))
  2950. @skipUnlessDBFeature("supports_expression_indexes")
  2951. def test_func_unique_constraint_collate(self):
  2952. collation = connection.features.test_collations.get("non_default")
  2953. if not collation:
  2954. self.skipTest("This backend does not support case-insensitive collations.")
  2955. with connection.schema_editor() as editor:
  2956. editor.create_model(Author)
  2957. editor.create_model(BookWithSlug)
  2958. constraint = UniqueConstraint(
  2959. Collate(F("title"), collation=collation).desc(),
  2960. Collate("slug", collation=collation),
  2961. name="func_collate_uq",
  2962. )
  2963. # Add constraint.
  2964. with connection.schema_editor() as editor:
  2965. editor.add_constraint(BookWithSlug, constraint)
  2966. sql = constraint.create_sql(BookWithSlug, editor)
  2967. table = BookWithSlug._meta.db_table
  2968. constraints = self.get_constraints(table)
  2969. self.assertIn(constraint.name, constraints)
  2970. self.assertIs(constraints[constraint.name]["unique"], True)
  2971. if connection.features.supports_index_column_ordering:
  2972. self.assertIndexOrder(table, constraint.name, ["DESC", "ASC"])
  2973. # SQL contains columns and a collation.
  2974. self.assertIs(sql.references_column(table, "title"), True)
  2975. self.assertIs(sql.references_column(table, "slug"), True)
  2976. self.assertIn("COLLATE %s" % editor.quote_name(collation), str(sql))
  2977. # Remove constraint.
  2978. with connection.schema_editor() as editor:
  2979. editor.remove_constraint(BookWithSlug, constraint)
  2980. self.assertNotIn(constraint.name, self.get_constraints(table))
  2981. @skipIfDBFeature("supports_expression_indexes")
  2982. def test_func_unique_constraint_unsupported(self):
  2983. # UniqueConstraint is ignored on databases that don't support indexes on
  2984. # expressions.
  2985. with connection.schema_editor() as editor:
  2986. editor.create_model(Author)
  2987. constraint = UniqueConstraint(F("name"), name="func_name_uq")
  2988. with connection.schema_editor() as editor, self.assertNumQueries(0):
  2989. self.assertIsNone(editor.add_constraint(Author, constraint))
  2990. self.assertIsNone(editor.remove_constraint(Author, constraint))
  2991. @skipUnlessDBFeature("supports_expression_indexes")
  2992. def test_func_unique_constraint_nonexistent_field(self):
  2993. constraint = UniqueConstraint(Lower("nonexistent"), name="func_nonexistent_uq")
  2994. msg = (
  2995. "Cannot resolve keyword 'nonexistent' into field. Choices are: "
  2996. "height, id, name, uuid, weight"
  2997. )
  2998. with self.assertRaisesMessage(FieldError, msg):
  2999. with connection.schema_editor() as editor:
  3000. editor.add_constraint(Author, constraint)
  3001. @skipUnlessDBFeature("supports_expression_indexes")
  3002. def test_func_unique_constraint_nondeterministic(self):
  3003. with connection.schema_editor() as editor:
  3004. editor.create_model(Author)
  3005. constraint = UniqueConstraint(Random(), name="func_random_uq")
  3006. with connection.schema_editor() as editor:
  3007. with self.assertRaises(DatabaseError):
  3008. editor.add_constraint(Author, constraint)
  3009. @ignore_warnings(category=RemovedInDjango51Warning)
  3010. def test_index_together(self):
  3011. """
  3012. Tests removing and adding index_together constraints on a model.
  3013. """
  3014. # Create the table
  3015. with connection.schema_editor() as editor:
  3016. editor.create_model(Tag)
  3017. # Ensure there's no index on the year/slug columns first
  3018. self.assertIs(
  3019. any(
  3020. c["index"]
  3021. for c in self.get_constraints("schema_tag").values()
  3022. if c["columns"] == ["slug", "title"]
  3023. ),
  3024. False,
  3025. )
  3026. # Alter the model to add an index
  3027. with connection.schema_editor() as editor:
  3028. editor.alter_index_together(Tag, [], [("slug", "title")])
  3029. # Ensure there is now an index
  3030. self.assertIs(
  3031. any(
  3032. c["index"]
  3033. for c in self.get_constraints("schema_tag").values()
  3034. if c["columns"] == ["slug", "title"]
  3035. ),
  3036. True,
  3037. )
  3038. # Alter it back
  3039. new_field2 = SlugField(unique=True)
  3040. new_field2.set_attributes_from_name("slug")
  3041. with connection.schema_editor() as editor:
  3042. editor.alter_index_together(Tag, [("slug", "title")], [])
  3043. # Ensure there's no index
  3044. self.assertIs(
  3045. any(
  3046. c["index"]
  3047. for c in self.get_constraints("schema_tag").values()
  3048. if c["columns"] == ["slug", "title"]
  3049. ),
  3050. False,
  3051. )
  3052. @ignore_warnings(category=RemovedInDjango51Warning)
  3053. def test_index_together_with_fk(self):
  3054. """
  3055. Tests removing and adding index_together constraints that include
  3056. a foreign key.
  3057. """
  3058. # Create the table
  3059. with connection.schema_editor() as editor:
  3060. editor.create_model(Author)
  3061. editor.create_model(Book)
  3062. # Ensure the fields are unique to begin with
  3063. self.assertEqual(Book._meta.index_together, ())
  3064. # Add the unique_together constraint
  3065. with connection.schema_editor() as editor:
  3066. editor.alter_index_together(Book, [], [["author", "title"]])
  3067. # Alter it back
  3068. with connection.schema_editor() as editor:
  3069. editor.alter_index_together(Book, [["author", "title"]], [])
  3070. @ignore_warnings(category=RemovedInDjango51Warning)
  3071. @isolate_apps("schema")
  3072. def test_create_index_together(self):
  3073. """
  3074. Tests creating models with index_together already defined
  3075. """
  3076. class TagIndexed(Model):
  3077. title = CharField(max_length=255)
  3078. slug = SlugField(unique=True)
  3079. class Meta:
  3080. app_label = "schema"
  3081. index_together = [["slug", "title"]]
  3082. # Create the table
  3083. with connection.schema_editor() as editor:
  3084. editor.create_model(TagIndexed)
  3085. self.isolated_local_models = [TagIndexed]
  3086. # Ensure there is an index
  3087. self.assertIs(
  3088. any(
  3089. c["index"]
  3090. for c in self.get_constraints("schema_tagindexed").values()
  3091. if c["columns"] == ["slug", "title"]
  3092. ),
  3093. True,
  3094. )
  3095. @skipUnlessDBFeature("allows_multiple_constraints_on_same_fields")
  3096. @ignore_warnings(category=RemovedInDjango51Warning)
  3097. @isolate_apps("schema")
  3098. def test_remove_index_together_does_not_remove_meta_indexes(self):
  3099. class AuthorWithIndexedNameAndBirthday(Model):
  3100. name = CharField(max_length=255)
  3101. birthday = DateField()
  3102. class Meta:
  3103. app_label = "schema"
  3104. index_together = [["name", "birthday"]]
  3105. with connection.schema_editor() as editor:
  3106. editor.create_model(AuthorWithIndexedNameAndBirthday)
  3107. self.isolated_local_models = [AuthorWithIndexedNameAndBirthday]
  3108. # Add the custom index
  3109. index = Index(fields=["name", "birthday"], name="author_name_birthday_idx")
  3110. custom_index_name = index.name
  3111. AuthorWithIndexedNameAndBirthday._meta.indexes = [index]
  3112. with connection.schema_editor() as editor:
  3113. editor.add_index(AuthorWithIndexedNameAndBirthday, index)
  3114. # Ensure the indexes exist
  3115. constraints = self.get_constraints(
  3116. AuthorWithIndexedNameAndBirthday._meta.db_table
  3117. )
  3118. self.assertIn(custom_index_name, constraints)
  3119. other_constraints = [
  3120. name
  3121. for name, details in constraints.items()
  3122. if details["columns"] == ["name", "birthday"]
  3123. and details["index"]
  3124. and name != custom_index_name
  3125. ]
  3126. self.assertEqual(len(other_constraints), 1)
  3127. # Remove index together
  3128. index_together = AuthorWithIndexedNameAndBirthday._meta.index_together
  3129. with connection.schema_editor() as editor:
  3130. editor.alter_index_together(
  3131. AuthorWithIndexedNameAndBirthday, index_together, []
  3132. )
  3133. constraints = self.get_constraints(
  3134. AuthorWithIndexedNameAndBirthday._meta.db_table
  3135. )
  3136. self.assertIn(custom_index_name, constraints)
  3137. other_constraints = [
  3138. name
  3139. for name, details in constraints.items()
  3140. if details["columns"] == ["name", "birthday"]
  3141. and details["index"]
  3142. and name != custom_index_name
  3143. ]
  3144. self.assertEqual(len(other_constraints), 0)
  3145. # Re-add index together
  3146. with connection.schema_editor() as editor:
  3147. editor.alter_index_together(
  3148. AuthorWithIndexedNameAndBirthday, [], index_together
  3149. )
  3150. constraints = self.get_constraints(
  3151. AuthorWithIndexedNameAndBirthday._meta.db_table
  3152. )
  3153. self.assertIn(custom_index_name, constraints)
  3154. other_constraints = [
  3155. name
  3156. for name, details in constraints.items()
  3157. if details["columns"] == ["name", "birthday"]
  3158. and details["index"]
  3159. and name != custom_index_name
  3160. ]
  3161. self.assertEqual(len(other_constraints), 1)
  3162. # Drop the index
  3163. with connection.schema_editor() as editor:
  3164. AuthorWithIndexedNameAndBirthday._meta.indexes = []
  3165. editor.remove_index(AuthorWithIndexedNameAndBirthday, index)
  3166. @isolate_apps("schema")
  3167. def test_db_table(self):
  3168. """
  3169. Tests renaming of the table
  3170. """
  3171. class Author(Model):
  3172. name = CharField(max_length=255)
  3173. class Meta:
  3174. app_label = "schema"
  3175. class Book(Model):
  3176. author = ForeignKey(Author, CASCADE)
  3177. class Meta:
  3178. app_label = "schema"
  3179. # Create the table and one referring it.
  3180. with connection.schema_editor() as editor:
  3181. editor.create_model(Author)
  3182. editor.create_model(Book)
  3183. # Ensure the table is there to begin with
  3184. columns = self.column_classes(Author)
  3185. self.assertEqual(
  3186. columns["name"][0],
  3187. connection.features.introspected_field_types["CharField"],
  3188. )
  3189. # Alter the table
  3190. with connection.schema_editor(
  3191. atomic=connection.features.supports_atomic_references_rename
  3192. ) as editor:
  3193. editor.alter_db_table(Author, "schema_author", "schema_otherauthor")
  3194. Author._meta.db_table = "schema_otherauthor"
  3195. columns = self.column_classes(Author)
  3196. self.assertEqual(
  3197. columns["name"][0],
  3198. connection.features.introspected_field_types["CharField"],
  3199. )
  3200. # Ensure the foreign key reference was updated
  3201. self.assertForeignKeyExists(Book, "author_id", "schema_otherauthor")
  3202. # Alter the table again
  3203. with connection.schema_editor(
  3204. atomic=connection.features.supports_atomic_references_rename
  3205. ) as editor:
  3206. editor.alter_db_table(Author, "schema_otherauthor", "schema_author")
  3207. # Ensure the table is still there
  3208. Author._meta.db_table = "schema_author"
  3209. columns = self.column_classes(Author)
  3210. self.assertEqual(
  3211. columns["name"][0],
  3212. connection.features.introspected_field_types["CharField"],
  3213. )
  3214. def test_add_remove_index(self):
  3215. """
  3216. Tests index addition and removal
  3217. """
  3218. # Create the table
  3219. with connection.schema_editor() as editor:
  3220. editor.create_model(Author)
  3221. # Ensure the table is there and has no index
  3222. self.assertNotIn("title", self.get_indexes(Author._meta.db_table))
  3223. # Add the index
  3224. index = Index(fields=["name"], name="author_title_idx")
  3225. with connection.schema_editor() as editor:
  3226. editor.add_index(Author, index)
  3227. self.assertIn("name", self.get_indexes(Author._meta.db_table))
  3228. # Drop the index
  3229. with connection.schema_editor() as editor:
  3230. editor.remove_index(Author, index)
  3231. self.assertNotIn("name", self.get_indexes(Author._meta.db_table))
  3232. def test_remove_db_index_doesnt_remove_custom_indexes(self):
  3233. """
  3234. Changing db_index to False doesn't remove indexes from Meta.indexes.
  3235. """
  3236. with connection.schema_editor() as editor:
  3237. editor.create_model(AuthorWithIndexedName)
  3238. self.local_models = [AuthorWithIndexedName]
  3239. # Ensure the table has its index
  3240. self.assertIn("name", self.get_indexes(AuthorWithIndexedName._meta.db_table))
  3241. # Add the custom index
  3242. index = Index(fields=["-name"], name="author_name_idx")
  3243. author_index_name = index.name
  3244. with connection.schema_editor() as editor:
  3245. db_index_name = editor._create_index_name(
  3246. table_name=AuthorWithIndexedName._meta.db_table,
  3247. column_names=("name",),
  3248. )
  3249. try:
  3250. AuthorWithIndexedName._meta.indexes = [index]
  3251. with connection.schema_editor() as editor:
  3252. editor.add_index(AuthorWithIndexedName, index)
  3253. old_constraints = self.get_constraints(AuthorWithIndexedName._meta.db_table)
  3254. self.assertIn(author_index_name, old_constraints)
  3255. self.assertIn(db_index_name, old_constraints)
  3256. # Change name field to db_index=False
  3257. old_field = AuthorWithIndexedName._meta.get_field("name")
  3258. new_field = CharField(max_length=255)
  3259. new_field.set_attributes_from_name("name")
  3260. with connection.schema_editor() as editor:
  3261. editor.alter_field(
  3262. AuthorWithIndexedName, old_field, new_field, strict=True
  3263. )
  3264. new_constraints = self.get_constraints(AuthorWithIndexedName._meta.db_table)
  3265. self.assertNotIn(db_index_name, new_constraints)
  3266. # The index from Meta.indexes is still in the database.
  3267. self.assertIn(author_index_name, new_constraints)
  3268. # Drop the index
  3269. with connection.schema_editor() as editor:
  3270. editor.remove_index(AuthorWithIndexedName, index)
  3271. finally:
  3272. AuthorWithIndexedName._meta.indexes = []
  3273. def test_order_index(self):
  3274. """
  3275. Indexes defined with ordering (ASC/DESC) defined on column
  3276. """
  3277. with connection.schema_editor() as editor:
  3278. editor.create_model(Author)
  3279. # The table doesn't have an index
  3280. self.assertNotIn("title", self.get_indexes(Author._meta.db_table))
  3281. index_name = "author_name_idx"
  3282. # Add the index
  3283. index = Index(fields=["name", "-weight"], name=index_name)
  3284. with connection.schema_editor() as editor:
  3285. editor.add_index(Author, index)
  3286. if connection.features.supports_index_column_ordering:
  3287. self.assertIndexOrder(Author._meta.db_table, index_name, ["ASC", "DESC"])
  3288. # Drop the index
  3289. with connection.schema_editor() as editor:
  3290. editor.remove_index(Author, index)
  3291. def test_indexes(self):
  3292. """
  3293. Tests creation/altering of indexes
  3294. """
  3295. # Create the table
  3296. with connection.schema_editor() as editor:
  3297. editor.create_model(Author)
  3298. editor.create_model(Book)
  3299. # Ensure the table is there and has the right index
  3300. self.assertIn(
  3301. "title",
  3302. self.get_indexes(Book._meta.db_table),
  3303. )
  3304. # Alter to remove the index
  3305. old_field = Book._meta.get_field("title")
  3306. new_field = CharField(max_length=100, db_index=False)
  3307. new_field.set_attributes_from_name("title")
  3308. with connection.schema_editor() as editor:
  3309. editor.alter_field(Book, old_field, new_field, strict=True)
  3310. # Ensure the table is there and has no index
  3311. self.assertNotIn(
  3312. "title",
  3313. self.get_indexes(Book._meta.db_table),
  3314. )
  3315. # Alter to re-add the index
  3316. new_field2 = Book._meta.get_field("title")
  3317. with connection.schema_editor() as editor:
  3318. editor.alter_field(Book, new_field, new_field2, strict=True)
  3319. # Ensure the table is there and has the index again
  3320. self.assertIn(
  3321. "title",
  3322. self.get_indexes(Book._meta.db_table),
  3323. )
  3324. # Add a unique column, verify that creates an implicit index
  3325. new_field3 = BookWithSlug._meta.get_field("slug")
  3326. with connection.schema_editor() as editor:
  3327. editor.add_field(Book, new_field3)
  3328. self.assertIn(
  3329. "slug",
  3330. self.get_uniques(Book._meta.db_table),
  3331. )
  3332. # Remove the unique, check the index goes with it
  3333. new_field4 = CharField(max_length=20, unique=False)
  3334. new_field4.set_attributes_from_name("slug")
  3335. with connection.schema_editor() as editor:
  3336. editor.alter_field(BookWithSlug, new_field3, new_field4, strict=True)
  3337. self.assertNotIn(
  3338. "slug",
  3339. self.get_uniques(Book._meta.db_table),
  3340. )
  3341. def test_text_field_with_db_index(self):
  3342. with connection.schema_editor() as editor:
  3343. editor.create_model(AuthorTextFieldWithIndex)
  3344. # The text_field index is present if the database supports it.
  3345. assertion = (
  3346. self.assertIn
  3347. if connection.features.supports_index_on_text_field
  3348. else self.assertNotIn
  3349. )
  3350. assertion(
  3351. "text_field", self.get_indexes(AuthorTextFieldWithIndex._meta.db_table)
  3352. )
  3353. def _index_expressions_wrappers(self):
  3354. index_expression = IndexExpression()
  3355. index_expression.set_wrapper_classes(connection)
  3356. return ", ".join(
  3357. [
  3358. wrapper_cls.__qualname__
  3359. for wrapper_cls in index_expression.wrapper_classes
  3360. ]
  3361. )
  3362. @skipUnlessDBFeature("supports_expression_indexes")
  3363. def test_func_index_multiple_wrapper_references(self):
  3364. index = Index(OrderBy(F("name").desc(), descending=True), name="name")
  3365. msg = (
  3366. "Multiple references to %s can't be used in an indexed expression."
  3367. % self._index_expressions_wrappers()
  3368. )
  3369. with connection.schema_editor() as editor:
  3370. with self.assertRaisesMessage(ValueError, msg):
  3371. editor.add_index(Author, index)
  3372. @skipUnlessDBFeature("supports_expression_indexes")
  3373. def test_func_index_invalid_topmost_expressions(self):
  3374. index = Index(Upper(F("name").desc()), name="name")
  3375. msg = (
  3376. "%s must be topmost expressions in an indexed expression."
  3377. % self._index_expressions_wrappers()
  3378. )
  3379. with connection.schema_editor() as editor:
  3380. with self.assertRaisesMessage(ValueError, msg):
  3381. editor.add_index(Author, index)
  3382. @skipUnlessDBFeature("supports_expression_indexes")
  3383. def test_func_index(self):
  3384. with connection.schema_editor() as editor:
  3385. editor.create_model(Author)
  3386. index = Index(Lower("name").desc(), name="func_lower_idx")
  3387. # Add index.
  3388. with connection.schema_editor() as editor:
  3389. editor.add_index(Author, index)
  3390. sql = index.create_sql(Author, editor)
  3391. table = Author._meta.db_table
  3392. if connection.features.supports_index_column_ordering:
  3393. self.assertIndexOrder(table, index.name, ["DESC"])
  3394. # SQL contains a database function.
  3395. self.assertIs(sql.references_column(table, "name"), True)
  3396. self.assertIn("LOWER(%s)" % editor.quote_name("name"), str(sql))
  3397. # Remove index.
  3398. with connection.schema_editor() as editor:
  3399. editor.remove_index(Author, index)
  3400. self.assertNotIn(index.name, self.get_constraints(table))
  3401. @skipUnlessDBFeature("supports_expression_indexes")
  3402. def test_func_index_f(self):
  3403. with connection.schema_editor() as editor:
  3404. editor.create_model(Tag)
  3405. index = Index("slug", F("title").desc(), name="func_f_idx")
  3406. # Add index.
  3407. with connection.schema_editor() as editor:
  3408. editor.add_index(Tag, index)
  3409. sql = index.create_sql(Tag, editor)
  3410. table = Tag._meta.db_table
  3411. self.assertIn(index.name, self.get_constraints(table))
  3412. if connection.features.supports_index_column_ordering:
  3413. self.assertIndexOrder(Tag._meta.db_table, index.name, ["ASC", "DESC"])
  3414. # SQL contains columns.
  3415. self.assertIs(sql.references_column(table, "slug"), True)
  3416. self.assertIs(sql.references_column(table, "title"), True)
  3417. # Remove index.
  3418. with connection.schema_editor() as editor:
  3419. editor.remove_index(Tag, index)
  3420. self.assertNotIn(index.name, self.get_constraints(table))
  3421. @skipUnlessDBFeature("supports_expression_indexes")
  3422. def test_func_index_lookups(self):
  3423. with connection.schema_editor() as editor:
  3424. editor.create_model(Author)
  3425. with register_lookup(CharField, Lower), register_lookup(IntegerField, Abs):
  3426. index = Index(
  3427. F("name__lower"),
  3428. F("weight__abs"),
  3429. name="func_lower_abs_lookup_idx",
  3430. )
  3431. # Add index.
  3432. with connection.schema_editor() as editor:
  3433. editor.add_index(Author, index)
  3434. sql = index.create_sql(Author, editor)
  3435. table = Author._meta.db_table
  3436. self.assertIn(index.name, self.get_constraints(table))
  3437. # SQL contains columns.
  3438. self.assertIs(sql.references_column(table, "name"), True)
  3439. self.assertIs(sql.references_column(table, "weight"), True)
  3440. # Remove index.
  3441. with connection.schema_editor() as editor:
  3442. editor.remove_index(Author, index)
  3443. self.assertNotIn(index.name, self.get_constraints(table))
  3444. @skipUnlessDBFeature("supports_expression_indexes")
  3445. def test_composite_func_index(self):
  3446. with connection.schema_editor() as editor:
  3447. editor.create_model(Author)
  3448. index = Index(Lower("name"), Upper("name"), name="func_lower_upper_idx")
  3449. # Add index.
  3450. with connection.schema_editor() as editor:
  3451. editor.add_index(Author, index)
  3452. sql = index.create_sql(Author, editor)
  3453. table = Author._meta.db_table
  3454. self.assertIn(index.name, self.get_constraints(table))
  3455. # SQL contains database functions.
  3456. self.assertIs(sql.references_column(table, "name"), True)
  3457. sql = str(sql)
  3458. self.assertIn("LOWER(%s)" % editor.quote_name("name"), sql)
  3459. self.assertIn("UPPER(%s)" % editor.quote_name("name"), sql)
  3460. self.assertLess(sql.index("LOWER"), sql.index("UPPER"))
  3461. # Remove index.
  3462. with connection.schema_editor() as editor:
  3463. editor.remove_index(Author, index)
  3464. self.assertNotIn(index.name, self.get_constraints(table))
  3465. @skipUnlessDBFeature("supports_expression_indexes")
  3466. def test_composite_func_index_field_and_expression(self):
  3467. with connection.schema_editor() as editor:
  3468. editor.create_model(Author)
  3469. editor.create_model(Book)
  3470. index = Index(
  3471. F("author").desc(),
  3472. Lower("title").asc(),
  3473. "pub_date",
  3474. name="func_f_lower_field_idx",
  3475. )
  3476. # Add index.
  3477. with connection.schema_editor() as editor:
  3478. editor.add_index(Book, index)
  3479. sql = index.create_sql(Book, editor)
  3480. table = Book._meta.db_table
  3481. constraints = self.get_constraints(table)
  3482. if connection.features.supports_index_column_ordering:
  3483. self.assertIndexOrder(table, index.name, ["DESC", "ASC", "ASC"])
  3484. self.assertEqual(len(constraints[index.name]["columns"]), 3)
  3485. self.assertEqual(constraints[index.name]["columns"][2], "pub_date")
  3486. # SQL contains database functions and columns.
  3487. self.assertIs(sql.references_column(table, "author_id"), True)
  3488. self.assertIs(sql.references_column(table, "title"), True)
  3489. self.assertIs(sql.references_column(table, "pub_date"), True)
  3490. self.assertIn("LOWER(%s)" % editor.quote_name("title"), str(sql))
  3491. # Remove index.
  3492. with connection.schema_editor() as editor:
  3493. editor.remove_index(Book, index)
  3494. self.assertNotIn(index.name, self.get_constraints(table))
  3495. @skipUnlessDBFeature("supports_expression_indexes")
  3496. @isolate_apps("schema")
  3497. def test_func_index_f_decimalfield(self):
  3498. class Node(Model):
  3499. value = DecimalField(max_digits=5, decimal_places=2)
  3500. class Meta:
  3501. app_label = "schema"
  3502. with connection.schema_editor() as editor:
  3503. editor.create_model(Node)
  3504. index = Index(F("value"), name="func_f_decimalfield_idx")
  3505. # Add index.
  3506. with connection.schema_editor() as editor:
  3507. editor.add_index(Node, index)
  3508. sql = index.create_sql(Node, editor)
  3509. table = Node._meta.db_table
  3510. self.assertIn(index.name, self.get_constraints(table))
  3511. self.assertIs(sql.references_column(table, "value"), True)
  3512. # SQL doesn't contain casting.
  3513. self.assertNotIn("CAST", str(sql))
  3514. # Remove index.
  3515. with connection.schema_editor() as editor:
  3516. editor.remove_index(Node, index)
  3517. self.assertNotIn(index.name, self.get_constraints(table))
  3518. @skipUnlessDBFeature("supports_expression_indexes")
  3519. def test_func_index_cast(self):
  3520. with connection.schema_editor() as editor:
  3521. editor.create_model(Author)
  3522. index = Index(Cast("weight", FloatField()), name="func_cast_idx")
  3523. # Add index.
  3524. with connection.schema_editor() as editor:
  3525. editor.add_index(Author, index)
  3526. sql = index.create_sql(Author, editor)
  3527. table = Author._meta.db_table
  3528. self.assertIn(index.name, self.get_constraints(table))
  3529. self.assertIs(sql.references_column(table, "weight"), True)
  3530. # Remove index.
  3531. with connection.schema_editor() as editor:
  3532. editor.remove_index(Author, index)
  3533. self.assertNotIn(index.name, self.get_constraints(table))
  3534. @skipUnlessDBFeature("supports_expression_indexes")
  3535. def test_func_index_collate(self):
  3536. collation = connection.features.test_collations.get("non_default")
  3537. if not collation:
  3538. self.skipTest("This backend does not support case-insensitive collations.")
  3539. with connection.schema_editor() as editor:
  3540. editor.create_model(Author)
  3541. editor.create_model(BookWithSlug)
  3542. index = Index(
  3543. Collate(F("title"), collation=collation).desc(),
  3544. Collate("slug", collation=collation),
  3545. name="func_collate_idx",
  3546. )
  3547. # Add index.
  3548. with connection.schema_editor() as editor:
  3549. editor.add_index(BookWithSlug, index)
  3550. sql = index.create_sql(BookWithSlug, editor)
  3551. table = Book._meta.db_table
  3552. self.assertIn(index.name, self.get_constraints(table))
  3553. if connection.features.supports_index_column_ordering:
  3554. self.assertIndexOrder(table, index.name, ["DESC", "ASC"])
  3555. # SQL contains columns and a collation.
  3556. self.assertIs(sql.references_column(table, "title"), True)
  3557. self.assertIs(sql.references_column(table, "slug"), True)
  3558. self.assertIn("COLLATE %s" % editor.quote_name(collation), str(sql))
  3559. # Remove index.
  3560. with connection.schema_editor() as editor:
  3561. editor.remove_index(Book, index)
  3562. self.assertNotIn(index.name, self.get_constraints(table))
  3563. @skipUnlessDBFeature("supports_expression_indexes")
  3564. @skipIfDBFeature("collate_as_index_expression")
  3565. def test_func_index_collate_f_ordered(self):
  3566. collation = connection.features.test_collations.get("non_default")
  3567. if not collation:
  3568. self.skipTest("This backend does not support case-insensitive collations.")
  3569. with connection.schema_editor() as editor:
  3570. editor.create_model(Author)
  3571. index = Index(
  3572. Collate(F("name").desc(), collation=collation),
  3573. name="func_collate_f_desc_idx",
  3574. )
  3575. # Add index.
  3576. with connection.schema_editor() as editor:
  3577. editor.add_index(Author, index)
  3578. sql = index.create_sql(Author, editor)
  3579. table = Author._meta.db_table
  3580. self.assertIn(index.name, self.get_constraints(table))
  3581. if connection.features.supports_index_column_ordering:
  3582. self.assertIndexOrder(table, index.name, ["DESC"])
  3583. # SQL contains columns and a collation.
  3584. self.assertIs(sql.references_column(table, "name"), True)
  3585. self.assertIn("COLLATE %s" % editor.quote_name(collation), str(sql))
  3586. # Remove index.
  3587. with connection.schema_editor() as editor:
  3588. editor.remove_index(Author, index)
  3589. self.assertNotIn(index.name, self.get_constraints(table))
  3590. @skipUnlessDBFeature("supports_expression_indexes")
  3591. def test_func_index_calc(self):
  3592. with connection.schema_editor() as editor:
  3593. editor.create_model(Author)
  3594. index = Index(F("height") / (F("weight") + Value(5)), name="func_calc_idx")
  3595. # Add index.
  3596. with connection.schema_editor() as editor:
  3597. editor.add_index(Author, index)
  3598. sql = index.create_sql(Author, editor)
  3599. table = Author._meta.db_table
  3600. self.assertIn(index.name, self.get_constraints(table))
  3601. # SQL contains columns and expressions.
  3602. self.assertIs(sql.references_column(table, "height"), True)
  3603. self.assertIs(sql.references_column(table, "weight"), True)
  3604. sql = str(sql)
  3605. self.assertIs(
  3606. sql.index(editor.quote_name("height"))
  3607. < sql.index("/")
  3608. < sql.index(editor.quote_name("weight"))
  3609. < sql.index("+")
  3610. < sql.index("5"),
  3611. True,
  3612. )
  3613. # Remove index.
  3614. with connection.schema_editor() as editor:
  3615. editor.remove_index(Author, index)
  3616. self.assertNotIn(index.name, self.get_constraints(table))
  3617. @skipUnlessDBFeature("supports_expression_indexes", "supports_json_field")
  3618. @isolate_apps("schema")
  3619. def test_func_index_json_key_transform(self):
  3620. class JSONModel(Model):
  3621. field = JSONField()
  3622. class Meta:
  3623. app_label = "schema"
  3624. with connection.schema_editor() as editor:
  3625. editor.create_model(JSONModel)
  3626. self.isolated_local_models = [JSONModel]
  3627. index = Index("field__some_key", name="func_json_key_idx")
  3628. with connection.schema_editor() as editor:
  3629. editor.add_index(JSONModel, index)
  3630. sql = index.create_sql(JSONModel, editor)
  3631. table = JSONModel._meta.db_table
  3632. self.assertIn(index.name, self.get_constraints(table))
  3633. self.assertIs(sql.references_column(table, "field"), True)
  3634. with connection.schema_editor() as editor:
  3635. editor.remove_index(JSONModel, index)
  3636. self.assertNotIn(index.name, self.get_constraints(table))
  3637. @skipUnlessDBFeature("supports_expression_indexes", "supports_json_field")
  3638. @isolate_apps("schema")
  3639. def test_func_index_json_key_transform_cast(self):
  3640. class JSONModel(Model):
  3641. field = JSONField()
  3642. class Meta:
  3643. app_label = "schema"
  3644. with connection.schema_editor() as editor:
  3645. editor.create_model(JSONModel)
  3646. self.isolated_local_models = [JSONModel]
  3647. index = Index(
  3648. Cast(KeyTextTransform("some_key", "field"), IntegerField()),
  3649. name="func_json_key_cast_idx",
  3650. )
  3651. with connection.schema_editor() as editor:
  3652. editor.add_index(JSONModel, index)
  3653. sql = index.create_sql(JSONModel, editor)
  3654. table = JSONModel._meta.db_table
  3655. self.assertIn(index.name, self.get_constraints(table))
  3656. self.assertIs(sql.references_column(table, "field"), True)
  3657. with connection.schema_editor() as editor:
  3658. editor.remove_index(JSONModel, index)
  3659. self.assertNotIn(index.name, self.get_constraints(table))
  3660. @skipIfDBFeature("supports_expression_indexes")
  3661. def test_func_index_unsupported(self):
  3662. # Index is ignored on databases that don't support indexes on
  3663. # expressions.
  3664. with connection.schema_editor() as editor:
  3665. editor.create_model(Author)
  3666. index = Index(F("name"), name="random_idx")
  3667. with connection.schema_editor() as editor, self.assertNumQueries(0):
  3668. self.assertIsNone(editor.add_index(Author, index))
  3669. self.assertIsNone(editor.remove_index(Author, index))
  3670. @skipUnlessDBFeature("supports_expression_indexes")
  3671. def test_func_index_nonexistent_field(self):
  3672. index = Index(Lower("nonexistent"), name="func_nonexistent_idx")
  3673. msg = (
  3674. "Cannot resolve keyword 'nonexistent' into field. Choices are: "
  3675. "height, id, name, uuid, weight"
  3676. )
  3677. with self.assertRaisesMessage(FieldError, msg):
  3678. with connection.schema_editor() as editor:
  3679. editor.add_index(Author, index)
  3680. @skipUnlessDBFeature("supports_expression_indexes")
  3681. def test_func_index_nondeterministic(self):
  3682. with connection.schema_editor() as editor:
  3683. editor.create_model(Author)
  3684. index = Index(Random(), name="func_random_idx")
  3685. with connection.schema_editor() as editor:
  3686. with self.assertRaises(DatabaseError):
  3687. editor.add_index(Author, index)
  3688. def test_primary_key(self):
  3689. """
  3690. Tests altering of the primary key
  3691. """
  3692. # Create the table
  3693. with connection.schema_editor() as editor:
  3694. editor.create_model(Tag)
  3695. # Ensure the table is there and has the right PK
  3696. self.assertEqual(self.get_primary_key(Tag._meta.db_table), "id")
  3697. # Alter to change the PK
  3698. id_field = Tag._meta.get_field("id")
  3699. old_field = Tag._meta.get_field("slug")
  3700. new_field = SlugField(primary_key=True)
  3701. new_field.set_attributes_from_name("slug")
  3702. new_field.model = Tag
  3703. with connection.schema_editor() as editor:
  3704. editor.remove_field(Tag, id_field)
  3705. editor.alter_field(Tag, old_field, new_field)
  3706. # Ensure the PK changed
  3707. self.assertNotIn(
  3708. "id",
  3709. self.get_indexes(Tag._meta.db_table),
  3710. )
  3711. self.assertEqual(self.get_primary_key(Tag._meta.db_table), "slug")
  3712. def test_alter_primary_key_the_same_name(self):
  3713. with connection.schema_editor() as editor:
  3714. editor.create_model(Thing)
  3715. old_field = Thing._meta.get_field("when")
  3716. new_field = CharField(max_length=2, primary_key=True)
  3717. new_field.set_attributes_from_name("when")
  3718. new_field.model = Thing
  3719. with connection.schema_editor() as editor:
  3720. editor.alter_field(Thing, old_field, new_field, strict=True)
  3721. self.assertEqual(self.get_primary_key(Thing._meta.db_table), "when")
  3722. with connection.schema_editor() as editor:
  3723. editor.alter_field(Thing, new_field, old_field, strict=True)
  3724. self.assertEqual(self.get_primary_key(Thing._meta.db_table), "when")
  3725. def test_context_manager_exit(self):
  3726. """
  3727. Ensures transaction is correctly closed when an error occurs
  3728. inside a SchemaEditor context.
  3729. """
  3730. class SomeError(Exception):
  3731. pass
  3732. try:
  3733. with connection.schema_editor():
  3734. raise SomeError
  3735. except SomeError:
  3736. self.assertFalse(connection.in_atomic_block)
  3737. @skipIfDBFeature("can_rollback_ddl")
  3738. def test_unsupported_transactional_ddl_disallowed(self):
  3739. message = (
  3740. "Executing DDL statements while in a transaction on databases "
  3741. "that can't perform a rollback is prohibited."
  3742. )
  3743. with atomic(), connection.schema_editor() as editor:
  3744. with self.assertRaisesMessage(TransactionManagementError, message):
  3745. editor.execute(
  3746. editor.sql_create_table % {"table": "foo", "definition": ""}
  3747. )
  3748. @skipUnlessDBFeature("supports_foreign_keys", "indexes_foreign_keys")
  3749. def test_foreign_key_index_long_names_regression(self):
  3750. """
  3751. Regression test for #21497.
  3752. Only affects databases that supports foreign keys.
  3753. """
  3754. # Create the table
  3755. with connection.schema_editor() as editor:
  3756. editor.create_model(AuthorWithEvenLongerName)
  3757. editor.create_model(BookWithLongName)
  3758. # Find the properly shortened column name
  3759. column_name = connection.ops.quote_name(
  3760. "author_foreign_key_with_really_long_field_name_id"
  3761. )
  3762. column_name = column_name[1:-1].lower() # unquote, and, for Oracle, un-upcase
  3763. # Ensure the table is there and has an index on the column
  3764. self.assertIn(
  3765. column_name,
  3766. self.get_indexes(BookWithLongName._meta.db_table),
  3767. )
  3768. @skipUnlessDBFeature("supports_foreign_keys")
  3769. def test_add_foreign_key_long_names(self):
  3770. """
  3771. Regression test for #23009.
  3772. Only affects databases that supports foreign keys.
  3773. """
  3774. # Create the initial tables
  3775. with connection.schema_editor() as editor:
  3776. editor.create_model(AuthorWithEvenLongerName)
  3777. editor.create_model(BookWithLongName)
  3778. # Add a second FK, this would fail due to long ref name before the fix
  3779. new_field = ForeignKey(
  3780. AuthorWithEvenLongerName, CASCADE, related_name="something"
  3781. )
  3782. new_field.set_attributes_from_name(
  3783. "author_other_really_long_named_i_mean_so_long_fk"
  3784. )
  3785. with connection.schema_editor() as editor:
  3786. editor.add_field(BookWithLongName, new_field)
  3787. @isolate_apps("schema")
  3788. @skipUnlessDBFeature("supports_foreign_keys")
  3789. def test_add_foreign_key_quoted_db_table(self):
  3790. class Author(Model):
  3791. class Meta:
  3792. db_table = '"table_author_double_quoted"'
  3793. app_label = "schema"
  3794. class Book(Model):
  3795. author = ForeignKey(Author, CASCADE)
  3796. class Meta:
  3797. app_label = "schema"
  3798. with connection.schema_editor() as editor:
  3799. editor.create_model(Author)
  3800. editor.create_model(Book)
  3801. self.isolated_local_models = [Author]
  3802. if connection.vendor == "mysql":
  3803. self.assertForeignKeyExists(
  3804. Book, "author_id", '"table_author_double_quoted"'
  3805. )
  3806. else:
  3807. self.assertForeignKeyExists(Book, "author_id", "table_author_double_quoted")
  3808. def test_add_foreign_object(self):
  3809. with connection.schema_editor() as editor:
  3810. editor.create_model(BookForeignObj)
  3811. self.local_models = [BookForeignObj]
  3812. new_field = ForeignObject(
  3813. Author, on_delete=CASCADE, from_fields=["author_id"], to_fields=["id"]
  3814. )
  3815. new_field.set_attributes_from_name("author")
  3816. with connection.schema_editor() as editor:
  3817. editor.add_field(BookForeignObj, new_field)
  3818. def test_creation_deletion_reserved_names(self):
  3819. """
  3820. Tries creating a model's table, and then deleting it when it has a
  3821. SQL reserved name.
  3822. """
  3823. # Create the table
  3824. with connection.schema_editor() as editor:
  3825. try:
  3826. editor.create_model(Thing)
  3827. except OperationalError as e:
  3828. self.fail(
  3829. "Errors when applying initial migration for a model "
  3830. "with a table named after an SQL reserved word: %s" % e
  3831. )
  3832. # The table is there
  3833. list(Thing.objects.all())
  3834. # Clean up that table
  3835. with connection.schema_editor() as editor:
  3836. editor.delete_model(Thing)
  3837. # The table is gone
  3838. with self.assertRaises(DatabaseError):
  3839. list(Thing.objects.all())
  3840. def test_remove_constraints_capital_letters(self):
  3841. """
  3842. #23065 - Constraint names must be quoted if they contain capital letters.
  3843. """
  3844. def get_field(*args, field_class=IntegerField, **kwargs):
  3845. kwargs["db_column"] = "CamelCase"
  3846. field = field_class(*args, **kwargs)
  3847. field.set_attributes_from_name("CamelCase")
  3848. return field
  3849. model = Author
  3850. field = get_field()
  3851. table = model._meta.db_table
  3852. column = field.column
  3853. identifier_converter = connection.introspection.identifier_converter
  3854. with connection.schema_editor() as editor:
  3855. editor.create_model(model)
  3856. editor.add_field(model, field)
  3857. constraint_name = "CamelCaseIndex"
  3858. expected_constraint_name = identifier_converter(constraint_name)
  3859. editor.execute(
  3860. editor.sql_create_index
  3861. % {
  3862. "table": editor.quote_name(table),
  3863. "name": editor.quote_name(constraint_name),
  3864. "using": "",
  3865. "columns": editor.quote_name(column),
  3866. "extra": "",
  3867. "condition": "",
  3868. "include": "",
  3869. }
  3870. )
  3871. self.assertIn(
  3872. expected_constraint_name, self.get_constraints(model._meta.db_table)
  3873. )
  3874. editor.alter_field(model, get_field(db_index=True), field, strict=True)
  3875. self.assertNotIn(
  3876. expected_constraint_name, self.get_constraints(model._meta.db_table)
  3877. )
  3878. constraint_name = "CamelCaseUniqConstraint"
  3879. expected_constraint_name = identifier_converter(constraint_name)
  3880. editor.execute(editor._create_unique_sql(model, [field], constraint_name))
  3881. self.assertIn(
  3882. expected_constraint_name, self.get_constraints(model._meta.db_table)
  3883. )
  3884. editor.alter_field(model, get_field(unique=True), field, strict=True)
  3885. self.assertNotIn(
  3886. expected_constraint_name, self.get_constraints(model._meta.db_table)
  3887. )
  3888. if editor.sql_create_fk and connection.features.can_introspect_foreign_keys:
  3889. constraint_name = "CamelCaseFKConstraint"
  3890. expected_constraint_name = identifier_converter(constraint_name)
  3891. editor.execute(
  3892. editor.sql_create_fk
  3893. % {
  3894. "table": editor.quote_name(table),
  3895. "name": editor.quote_name(constraint_name),
  3896. "column": editor.quote_name(column),
  3897. "to_table": editor.quote_name(table),
  3898. "to_column": editor.quote_name(model._meta.auto_field.column),
  3899. "deferrable": connection.ops.deferrable_sql(),
  3900. }
  3901. )
  3902. self.assertIn(
  3903. expected_constraint_name, self.get_constraints(model._meta.db_table)
  3904. )
  3905. editor.alter_field(
  3906. model,
  3907. get_field(Author, CASCADE, field_class=ForeignKey),
  3908. field,
  3909. strict=True,
  3910. )
  3911. self.assertNotIn(
  3912. expected_constraint_name, self.get_constraints(model._meta.db_table)
  3913. )
  3914. def test_add_field_use_effective_default(self):
  3915. """
  3916. #23987 - effective_default() should be used as the field default when
  3917. adding a new field.
  3918. """
  3919. # Create the table
  3920. with connection.schema_editor() as editor:
  3921. editor.create_model(Author)
  3922. # Ensure there's no surname field
  3923. columns = self.column_classes(Author)
  3924. self.assertNotIn("surname", columns)
  3925. # Create a row
  3926. Author.objects.create(name="Anonymous1")
  3927. # Add new CharField to ensure default will be used from effective_default
  3928. new_field = CharField(max_length=15, blank=True)
  3929. new_field.set_attributes_from_name("surname")
  3930. with connection.schema_editor() as editor:
  3931. editor.add_field(Author, new_field)
  3932. # Ensure field was added with the right default
  3933. with connection.cursor() as cursor:
  3934. cursor.execute("SELECT surname FROM schema_author;")
  3935. item = cursor.fetchall()[0]
  3936. self.assertEqual(
  3937. item[0],
  3938. None if connection.features.interprets_empty_strings_as_nulls else "",
  3939. )
  3940. def test_add_field_default_dropped(self):
  3941. # Create the table
  3942. with connection.schema_editor() as editor:
  3943. editor.create_model(Author)
  3944. # Ensure there's no surname field
  3945. columns = self.column_classes(Author)
  3946. self.assertNotIn("surname", columns)
  3947. # Create a row
  3948. Author.objects.create(name="Anonymous1")
  3949. # Add new CharField with a default
  3950. new_field = CharField(max_length=15, blank=True, default="surname default")
  3951. new_field.set_attributes_from_name("surname")
  3952. with connection.schema_editor() as editor:
  3953. editor.add_field(Author, new_field)
  3954. # Ensure field was added with the right default
  3955. with connection.cursor() as cursor:
  3956. cursor.execute("SELECT surname FROM schema_author;")
  3957. item = cursor.fetchall()[0]
  3958. self.assertEqual(item[0], "surname default")
  3959. # And that the default is no longer set in the database.
  3960. field = next(
  3961. f
  3962. for f in connection.introspection.get_table_description(
  3963. cursor, "schema_author"
  3964. )
  3965. if f.name == "surname"
  3966. )
  3967. if connection.features.can_introspect_default:
  3968. self.assertIsNone(field.default)
  3969. def test_add_field_default_nullable(self):
  3970. with connection.schema_editor() as editor:
  3971. editor.create_model(Author)
  3972. # Add new nullable CharField with a default.
  3973. new_field = CharField(max_length=15, blank=True, null=True, default="surname")
  3974. new_field.set_attributes_from_name("surname")
  3975. with connection.schema_editor() as editor:
  3976. editor.add_field(Author, new_field)
  3977. Author.objects.create(name="Anonymous1")
  3978. with connection.cursor() as cursor:
  3979. cursor.execute("SELECT surname FROM schema_author;")
  3980. item = cursor.fetchall()[0]
  3981. self.assertIsNone(item[0])
  3982. field = next(
  3983. f
  3984. for f in connection.introspection.get_table_description(
  3985. cursor,
  3986. "schema_author",
  3987. )
  3988. if f.name == "surname"
  3989. )
  3990. # Field is still nullable.
  3991. self.assertTrue(field.null_ok)
  3992. # The database default is no longer set.
  3993. if connection.features.can_introspect_default:
  3994. self.assertIn(field.default, ["NULL", None])
  3995. def test_add_textfield_default_nullable(self):
  3996. with connection.schema_editor() as editor:
  3997. editor.create_model(Author)
  3998. # Add new nullable TextField with a default.
  3999. new_field = TextField(blank=True, null=True, default="text")
  4000. new_field.set_attributes_from_name("description")
  4001. with connection.schema_editor() as editor:
  4002. editor.add_field(Author, new_field)
  4003. Author.objects.create(name="Anonymous1")
  4004. with connection.cursor() as cursor:
  4005. cursor.execute("SELECT description FROM schema_author;")
  4006. item = cursor.fetchall()[0]
  4007. self.assertIsNone(item[0])
  4008. field = next(
  4009. f
  4010. for f in connection.introspection.get_table_description(
  4011. cursor,
  4012. "schema_author",
  4013. )
  4014. if f.name == "description"
  4015. )
  4016. # Field is still nullable.
  4017. self.assertTrue(field.null_ok)
  4018. # The database default is no longer set.
  4019. if connection.features.can_introspect_default:
  4020. self.assertIn(field.default, ["NULL", None])
  4021. def test_alter_field_default_dropped(self):
  4022. # Create the table
  4023. with connection.schema_editor() as editor:
  4024. editor.create_model(Author)
  4025. # Create a row
  4026. Author.objects.create(name="Anonymous1")
  4027. self.assertIsNone(Author.objects.get().height)
  4028. old_field = Author._meta.get_field("height")
  4029. # The default from the new field is used in updating existing rows.
  4030. new_field = IntegerField(blank=True, default=42)
  4031. new_field.set_attributes_from_name("height")
  4032. with connection.schema_editor() as editor:
  4033. editor.alter_field(Author, old_field, new_field, strict=True)
  4034. self.assertEqual(Author.objects.get().height, 42)
  4035. # The database default should be removed.
  4036. with connection.cursor() as cursor:
  4037. field = next(
  4038. f
  4039. for f in connection.introspection.get_table_description(
  4040. cursor, "schema_author"
  4041. )
  4042. if f.name == "height"
  4043. )
  4044. if connection.features.can_introspect_default:
  4045. self.assertIsNone(field.default)
  4046. def test_alter_field_default_doesnt_perform_queries(self):
  4047. """
  4048. No queries are performed if a field default changes and the field's
  4049. not changing from null to non-null.
  4050. """
  4051. with connection.schema_editor() as editor:
  4052. editor.create_model(AuthorWithDefaultHeight)
  4053. old_field = AuthorWithDefaultHeight._meta.get_field("height")
  4054. new_default = old_field.default * 2
  4055. new_field = PositiveIntegerField(null=True, blank=True, default=new_default)
  4056. new_field.set_attributes_from_name("height")
  4057. with connection.schema_editor() as editor, self.assertNumQueries(0):
  4058. editor.alter_field(
  4059. AuthorWithDefaultHeight, old_field, new_field, strict=True
  4060. )
  4061. @skipUnlessDBFeature("supports_foreign_keys")
  4062. def test_alter_field_fk_attributes_noop(self):
  4063. """
  4064. No queries are performed when changing field attributes that don't
  4065. affect the schema.
  4066. """
  4067. with connection.schema_editor() as editor:
  4068. editor.create_model(Author)
  4069. editor.create_model(Book)
  4070. old_field = Book._meta.get_field("author")
  4071. new_field = ForeignKey(
  4072. Author,
  4073. blank=True,
  4074. editable=False,
  4075. error_messages={"invalid": "error message"},
  4076. help_text="help text",
  4077. limit_choices_to={"limit": "choice"},
  4078. on_delete=PROTECT,
  4079. related_name="related_name",
  4080. related_query_name="related_query_name",
  4081. validators=[lambda x: x],
  4082. verbose_name="verbose name",
  4083. )
  4084. new_field.set_attributes_from_name("author")
  4085. with connection.schema_editor() as editor, self.assertNumQueries(0):
  4086. editor.alter_field(Book, old_field, new_field, strict=True)
  4087. with connection.schema_editor() as editor, self.assertNumQueries(0):
  4088. editor.alter_field(Book, new_field, old_field, strict=True)
  4089. def test_alter_field_choices_noop(self):
  4090. with connection.schema_editor() as editor:
  4091. editor.create_model(Author)
  4092. old_field = Author._meta.get_field("name")
  4093. new_field = CharField(
  4094. choices=(("Jane", "Jane"), ("Joe", "Joe")),
  4095. max_length=255,
  4096. )
  4097. new_field.set_attributes_from_name("name")
  4098. with connection.schema_editor() as editor, self.assertNumQueries(0):
  4099. editor.alter_field(Author, old_field, new_field, strict=True)
  4100. with connection.schema_editor() as editor, self.assertNumQueries(0):
  4101. editor.alter_field(Author, new_field, old_field, strict=True)
  4102. def test_add_textfield_unhashable_default(self):
  4103. # Create the table
  4104. with connection.schema_editor() as editor:
  4105. editor.create_model(Author)
  4106. # Create a row
  4107. Author.objects.create(name="Anonymous1")
  4108. # Create a field that has an unhashable default
  4109. new_field = TextField(default={})
  4110. new_field.set_attributes_from_name("info")
  4111. with connection.schema_editor() as editor:
  4112. editor.add_field(Author, new_field)
  4113. @unittest.skipUnless(connection.vendor == "postgresql", "PostgreSQL specific")
  4114. def test_add_indexed_charfield(self):
  4115. field = CharField(max_length=255, db_index=True)
  4116. field.set_attributes_from_name("nom_de_plume")
  4117. with connection.schema_editor() as editor:
  4118. editor.create_model(Author)
  4119. editor.add_field(Author, field)
  4120. # Should create two indexes; one for like operator.
  4121. self.assertEqual(
  4122. self.get_constraints_for_column(Author, "nom_de_plume"),
  4123. [
  4124. "schema_author_nom_de_plume_7570a851",
  4125. "schema_author_nom_de_plume_7570a851_like",
  4126. ],
  4127. )
  4128. @unittest.skipUnless(connection.vendor == "postgresql", "PostgreSQL specific")
  4129. def test_add_unique_charfield(self):
  4130. field = CharField(max_length=255, unique=True)
  4131. field.set_attributes_from_name("nom_de_plume")
  4132. with connection.schema_editor() as editor:
  4133. editor.create_model(Author)
  4134. editor.add_field(Author, field)
  4135. # Should create two indexes; one for like operator.
  4136. self.assertEqual(
  4137. self.get_constraints_for_column(Author, "nom_de_plume"),
  4138. [
  4139. "schema_author_nom_de_plume_7570a851_like",
  4140. "schema_author_nom_de_plume_key",
  4141. ],
  4142. )
  4143. @skipUnlessDBFeature("supports_comments")
  4144. def test_add_db_comment_charfield(self):
  4145. comment = "Custom comment"
  4146. field = CharField(max_length=255, db_comment=comment)
  4147. field.set_attributes_from_name("name_with_comment")
  4148. with connection.schema_editor() as editor:
  4149. editor.create_model(Author)
  4150. editor.add_field(Author, field)
  4151. self.assertEqual(
  4152. self.get_column_comment(Author._meta.db_table, "name_with_comment"),
  4153. comment,
  4154. )
  4155. @skipUnlessDBFeature("supports_comments")
  4156. def test_add_db_comment_and_default_charfield(self):
  4157. comment = "Custom comment with default"
  4158. field = CharField(max_length=255, default="Joe Doe", db_comment=comment)
  4159. field.set_attributes_from_name("name_with_comment_default")
  4160. with connection.schema_editor() as editor:
  4161. editor.create_model(Author)
  4162. Author.objects.create(name="Before adding a new field")
  4163. editor.add_field(Author, field)
  4164. self.assertEqual(
  4165. self.get_column_comment(Author._meta.db_table, "name_with_comment_default"),
  4166. comment,
  4167. )
  4168. with connection.cursor() as cursor:
  4169. cursor.execute(
  4170. f"SELECT name_with_comment_default FROM {Author._meta.db_table};"
  4171. )
  4172. for row in cursor.fetchall():
  4173. self.assertEqual(row[0], "Joe Doe")
  4174. @skipUnlessDBFeature("supports_comments")
  4175. def test_alter_db_comment(self):
  4176. with connection.schema_editor() as editor:
  4177. editor.create_model(Author)
  4178. # Add comment.
  4179. old_field = Author._meta.get_field("name")
  4180. new_field = CharField(max_length=255, db_comment="Custom comment")
  4181. new_field.set_attributes_from_name("name")
  4182. with connection.schema_editor() as editor:
  4183. editor.alter_field(Author, old_field, new_field, strict=True)
  4184. self.assertEqual(
  4185. self.get_column_comment(Author._meta.db_table, "name"),
  4186. "Custom comment",
  4187. )
  4188. # Alter comment.
  4189. old_field = new_field
  4190. new_field = CharField(max_length=255, db_comment="New custom comment")
  4191. new_field.set_attributes_from_name("name")
  4192. with connection.schema_editor() as editor:
  4193. editor.alter_field(Author, old_field, new_field, strict=True)
  4194. self.assertEqual(
  4195. self.get_column_comment(Author._meta.db_table, "name"),
  4196. "New custom comment",
  4197. )
  4198. # Remove comment.
  4199. old_field = new_field
  4200. new_field = CharField(max_length=255)
  4201. new_field.set_attributes_from_name("name")
  4202. with connection.schema_editor() as editor:
  4203. editor.alter_field(Author, old_field, new_field, strict=True)
  4204. self.assertIn(
  4205. self.get_column_comment(Author._meta.db_table, "name"),
  4206. [None, ""],
  4207. )
  4208. @skipUnlessDBFeature("supports_comments", "supports_foreign_keys")
  4209. def test_alter_db_comment_foreign_key(self):
  4210. with connection.schema_editor() as editor:
  4211. editor.create_model(Author)
  4212. editor.create_model(Book)
  4213. comment = "FK custom comment"
  4214. old_field = Book._meta.get_field("author")
  4215. new_field = ForeignKey(Author, CASCADE, db_comment=comment)
  4216. new_field.set_attributes_from_name("author")
  4217. with connection.schema_editor() as editor:
  4218. editor.alter_field(Book, old_field, new_field, strict=True)
  4219. self.assertEqual(
  4220. self.get_column_comment(Book._meta.db_table, "author_id"),
  4221. comment,
  4222. )
  4223. @skipUnlessDBFeature("supports_comments")
  4224. def test_alter_field_type_preserve_comment(self):
  4225. with connection.schema_editor() as editor:
  4226. editor.create_model(Author)
  4227. comment = "This is the name."
  4228. old_field = Author._meta.get_field("name")
  4229. new_field = CharField(max_length=255, db_comment=comment)
  4230. new_field.set_attributes_from_name("name")
  4231. new_field.model = Author
  4232. with connection.schema_editor() as editor:
  4233. editor.alter_field(Author, old_field, new_field, strict=True)
  4234. self.assertEqual(
  4235. self.get_column_comment(Author._meta.db_table, "name"),
  4236. comment,
  4237. )
  4238. # Changing a field type should preserve the comment.
  4239. old_field = new_field
  4240. new_field = CharField(max_length=511, db_comment=comment)
  4241. new_field.set_attributes_from_name("name")
  4242. new_field.model = Author
  4243. with connection.schema_editor() as editor:
  4244. editor.alter_field(Author, new_field, old_field, strict=True)
  4245. # Comment is preserved.
  4246. self.assertEqual(
  4247. self.get_column_comment(Author._meta.db_table, "name"),
  4248. comment,
  4249. )
  4250. @isolate_apps("schema")
  4251. @skipUnlessDBFeature("supports_comments")
  4252. def test_db_comment_table(self):
  4253. class ModelWithDbTableComment(Model):
  4254. class Meta:
  4255. app_label = "schema"
  4256. db_table_comment = "Custom table comment"
  4257. with connection.schema_editor() as editor:
  4258. editor.create_model(ModelWithDbTableComment)
  4259. self.isolated_local_models = [ModelWithDbTableComment]
  4260. self.assertEqual(
  4261. self.get_table_comment(ModelWithDbTableComment._meta.db_table),
  4262. "Custom table comment",
  4263. )
  4264. # Alter table comment.
  4265. old_db_table_comment = ModelWithDbTableComment._meta.db_table_comment
  4266. with connection.schema_editor() as editor:
  4267. editor.alter_db_table_comment(
  4268. ModelWithDbTableComment, old_db_table_comment, "New table comment"
  4269. )
  4270. self.assertEqual(
  4271. self.get_table_comment(ModelWithDbTableComment._meta.db_table),
  4272. "New table comment",
  4273. )
  4274. # Remove table comment.
  4275. old_db_table_comment = ModelWithDbTableComment._meta.db_table_comment
  4276. with connection.schema_editor() as editor:
  4277. editor.alter_db_table_comment(
  4278. ModelWithDbTableComment, old_db_table_comment, None
  4279. )
  4280. self.assertIn(
  4281. self.get_table_comment(ModelWithDbTableComment._meta.db_table),
  4282. [None, ""],
  4283. )
  4284. @isolate_apps("schema")
  4285. @skipUnlessDBFeature("supports_comments", "supports_foreign_keys")
  4286. def test_db_comments_from_abstract_model(self):
  4287. class AbstractModelWithDbComments(Model):
  4288. name = CharField(
  4289. max_length=255, db_comment="Custom comment", null=True, blank=True
  4290. )
  4291. class Meta:
  4292. app_label = "schema"
  4293. abstract = True
  4294. db_table_comment = "Custom table comment"
  4295. class ModelWithDbComments(AbstractModelWithDbComments):
  4296. pass
  4297. with connection.schema_editor() as editor:
  4298. editor.create_model(ModelWithDbComments)
  4299. self.isolated_local_models = [ModelWithDbComments]
  4300. self.assertEqual(
  4301. self.get_column_comment(ModelWithDbComments._meta.db_table, "name"),
  4302. "Custom comment",
  4303. )
  4304. self.assertEqual(
  4305. self.get_table_comment(ModelWithDbComments._meta.db_table),
  4306. "Custom table comment",
  4307. )
  4308. @unittest.skipUnless(connection.vendor == "postgresql", "PostgreSQL specific")
  4309. def test_alter_field_add_index_to_charfield(self):
  4310. # Create the table and verify no initial indexes.
  4311. with connection.schema_editor() as editor:
  4312. editor.create_model(Author)
  4313. self.assertEqual(self.get_constraints_for_column(Author, "name"), [])
  4314. # Alter to add db_index=True and create 2 indexes.
  4315. old_field = Author._meta.get_field("name")
  4316. new_field = CharField(max_length=255, db_index=True)
  4317. new_field.set_attributes_from_name("name")
  4318. with connection.schema_editor() as editor:
  4319. editor.alter_field(Author, old_field, new_field, strict=True)
  4320. self.assertEqual(
  4321. self.get_constraints_for_column(Author, "name"),
  4322. ["schema_author_name_1fbc5617", "schema_author_name_1fbc5617_like"],
  4323. )
  4324. # Remove db_index=True to drop both indexes.
  4325. with connection.schema_editor() as editor:
  4326. editor.alter_field(Author, new_field, old_field, strict=True)
  4327. self.assertEqual(self.get_constraints_for_column(Author, "name"), [])
  4328. @unittest.skipUnless(connection.vendor == "postgresql", "PostgreSQL specific")
  4329. def test_alter_field_add_unique_to_charfield(self):
  4330. # Create the table and verify no initial indexes.
  4331. with connection.schema_editor() as editor:
  4332. editor.create_model(Author)
  4333. self.assertEqual(self.get_constraints_for_column(Author, "name"), [])
  4334. # Alter to add unique=True and create 2 indexes.
  4335. old_field = Author._meta.get_field("name")
  4336. new_field = CharField(max_length=255, unique=True)
  4337. new_field.set_attributes_from_name("name")
  4338. with connection.schema_editor() as editor:
  4339. editor.alter_field(Author, old_field, new_field, strict=True)
  4340. self.assertEqual(
  4341. self.get_constraints_for_column(Author, "name"),
  4342. ["schema_author_name_1fbc5617_like", "schema_author_name_1fbc5617_uniq"],
  4343. )
  4344. # Remove unique=True to drop both indexes.
  4345. with connection.schema_editor() as editor:
  4346. editor.alter_field(Author, new_field, old_field, strict=True)
  4347. self.assertEqual(self.get_constraints_for_column(Author, "name"), [])
  4348. @unittest.skipUnless(connection.vendor == "postgresql", "PostgreSQL specific")
  4349. def test_alter_field_add_index_to_textfield(self):
  4350. # Create the table and verify no initial indexes.
  4351. with connection.schema_editor() as editor:
  4352. editor.create_model(Note)
  4353. self.assertEqual(self.get_constraints_for_column(Note, "info"), [])
  4354. # Alter to add db_index=True and create 2 indexes.
  4355. old_field = Note._meta.get_field("info")
  4356. new_field = TextField(db_index=True)
  4357. new_field.set_attributes_from_name("info")
  4358. with connection.schema_editor() as editor:
  4359. editor.alter_field(Note, old_field, new_field, strict=True)
  4360. self.assertEqual(
  4361. self.get_constraints_for_column(Note, "info"),
  4362. ["schema_note_info_4b0ea695", "schema_note_info_4b0ea695_like"],
  4363. )
  4364. # Remove db_index=True to drop both indexes.
  4365. with connection.schema_editor() as editor:
  4366. editor.alter_field(Note, new_field, old_field, strict=True)
  4367. self.assertEqual(self.get_constraints_for_column(Note, "info"), [])
  4368. @unittest.skipUnless(connection.vendor == "postgresql", "PostgreSQL specific")
  4369. def test_alter_field_add_unique_to_charfield_with_db_index(self):
  4370. # Create the table and verify initial indexes.
  4371. with connection.schema_editor() as editor:
  4372. editor.create_model(BookWithoutAuthor)
  4373. self.assertEqual(
  4374. self.get_constraints_for_column(BookWithoutAuthor, "title"),
  4375. ["schema_book_title_2dfb2dff", "schema_book_title_2dfb2dff_like"],
  4376. )
  4377. # Alter to add unique=True (should replace the index)
  4378. old_field = BookWithoutAuthor._meta.get_field("title")
  4379. new_field = CharField(max_length=100, db_index=True, unique=True)
  4380. new_field.set_attributes_from_name("title")
  4381. with connection.schema_editor() as editor:
  4382. editor.alter_field(BookWithoutAuthor, old_field, new_field, strict=True)
  4383. self.assertEqual(
  4384. self.get_constraints_for_column(BookWithoutAuthor, "title"),
  4385. ["schema_book_title_2dfb2dff_like", "schema_book_title_2dfb2dff_uniq"],
  4386. )
  4387. # Alter to remove unique=True (should drop unique index)
  4388. new_field2 = CharField(max_length=100, db_index=True)
  4389. new_field2.set_attributes_from_name("title")
  4390. with connection.schema_editor() as editor:
  4391. editor.alter_field(BookWithoutAuthor, new_field, new_field2, strict=True)
  4392. self.assertEqual(
  4393. self.get_constraints_for_column(BookWithoutAuthor, "title"),
  4394. ["schema_book_title_2dfb2dff", "schema_book_title_2dfb2dff_like"],
  4395. )
  4396. @unittest.skipUnless(connection.vendor == "postgresql", "PostgreSQL specific")
  4397. def test_alter_field_remove_unique_and_db_index_from_charfield(self):
  4398. # Create the table and verify initial indexes.
  4399. with connection.schema_editor() as editor:
  4400. editor.create_model(BookWithoutAuthor)
  4401. self.assertEqual(
  4402. self.get_constraints_for_column(BookWithoutAuthor, "title"),
  4403. ["schema_book_title_2dfb2dff", "schema_book_title_2dfb2dff_like"],
  4404. )
  4405. # Alter to add unique=True (should replace the index)
  4406. old_field = BookWithoutAuthor._meta.get_field("title")
  4407. new_field = CharField(max_length=100, db_index=True, unique=True)
  4408. new_field.set_attributes_from_name("title")
  4409. with connection.schema_editor() as editor:
  4410. editor.alter_field(BookWithoutAuthor, old_field, new_field, strict=True)
  4411. self.assertEqual(
  4412. self.get_constraints_for_column(BookWithoutAuthor, "title"),
  4413. ["schema_book_title_2dfb2dff_like", "schema_book_title_2dfb2dff_uniq"],
  4414. )
  4415. # Alter to remove both unique=True and db_index=True (should drop all indexes)
  4416. new_field2 = CharField(max_length=100)
  4417. new_field2.set_attributes_from_name("title")
  4418. with connection.schema_editor() as editor:
  4419. editor.alter_field(BookWithoutAuthor, new_field, new_field2, strict=True)
  4420. self.assertEqual(
  4421. self.get_constraints_for_column(BookWithoutAuthor, "title"), []
  4422. )
  4423. @unittest.skipUnless(connection.vendor == "postgresql", "PostgreSQL specific")
  4424. def test_alter_field_swap_unique_and_db_index_with_charfield(self):
  4425. # Create the table and verify initial indexes.
  4426. with connection.schema_editor() as editor:
  4427. editor.create_model(BookWithoutAuthor)
  4428. self.assertEqual(
  4429. self.get_constraints_for_column(BookWithoutAuthor, "title"),
  4430. ["schema_book_title_2dfb2dff", "schema_book_title_2dfb2dff_like"],
  4431. )
  4432. # Alter to set unique=True and remove db_index=True (should replace the index)
  4433. old_field = BookWithoutAuthor._meta.get_field("title")
  4434. new_field = CharField(max_length=100, unique=True)
  4435. new_field.set_attributes_from_name("title")
  4436. with connection.schema_editor() as editor:
  4437. editor.alter_field(BookWithoutAuthor, old_field, new_field, strict=True)
  4438. self.assertEqual(
  4439. self.get_constraints_for_column(BookWithoutAuthor, "title"),
  4440. ["schema_book_title_2dfb2dff_like", "schema_book_title_2dfb2dff_uniq"],
  4441. )
  4442. # Alter to set db_index=True and remove unique=True (should restore index)
  4443. new_field2 = CharField(max_length=100, db_index=True)
  4444. new_field2.set_attributes_from_name("title")
  4445. with connection.schema_editor() as editor:
  4446. editor.alter_field(BookWithoutAuthor, new_field, new_field2, strict=True)
  4447. self.assertEqual(
  4448. self.get_constraints_for_column(BookWithoutAuthor, "title"),
  4449. ["schema_book_title_2dfb2dff", "schema_book_title_2dfb2dff_like"],
  4450. )
  4451. @unittest.skipUnless(connection.vendor == "postgresql", "PostgreSQL specific")
  4452. def test_alter_field_add_db_index_to_charfield_with_unique(self):
  4453. # Create the table and verify initial indexes.
  4454. with connection.schema_editor() as editor:
  4455. editor.create_model(Tag)
  4456. self.assertEqual(
  4457. self.get_constraints_for_column(Tag, "slug"),
  4458. ["schema_tag_slug_2c418ba3_like", "schema_tag_slug_key"],
  4459. )
  4460. # Alter to add db_index=True
  4461. old_field = Tag._meta.get_field("slug")
  4462. new_field = SlugField(db_index=True, unique=True)
  4463. new_field.set_attributes_from_name("slug")
  4464. with connection.schema_editor() as editor:
  4465. editor.alter_field(Tag, old_field, new_field, strict=True)
  4466. self.assertEqual(
  4467. self.get_constraints_for_column(Tag, "slug"),
  4468. ["schema_tag_slug_2c418ba3_like", "schema_tag_slug_key"],
  4469. )
  4470. # Alter to remove db_index=True
  4471. new_field2 = SlugField(unique=True)
  4472. new_field2.set_attributes_from_name("slug")
  4473. with connection.schema_editor() as editor:
  4474. editor.alter_field(Tag, new_field, new_field2, strict=True)
  4475. self.assertEqual(
  4476. self.get_constraints_for_column(Tag, "slug"),
  4477. ["schema_tag_slug_2c418ba3_like", "schema_tag_slug_key"],
  4478. )
  4479. def test_alter_field_add_index_to_integerfield(self):
  4480. # Create the table and verify no initial indexes.
  4481. with connection.schema_editor() as editor:
  4482. editor.create_model(Author)
  4483. self.assertEqual(self.get_constraints_for_column(Author, "weight"), [])
  4484. # Alter to add db_index=True and create index.
  4485. old_field = Author._meta.get_field("weight")
  4486. new_field = IntegerField(null=True, db_index=True)
  4487. new_field.set_attributes_from_name("weight")
  4488. with connection.schema_editor() as editor:
  4489. editor.alter_field(Author, old_field, new_field, strict=True)
  4490. self.assertEqual(
  4491. self.get_constraints_for_column(Author, "weight"),
  4492. ["schema_author_weight_587740f9"],
  4493. )
  4494. # Remove db_index=True to drop index.
  4495. with connection.schema_editor() as editor:
  4496. editor.alter_field(Author, new_field, old_field, strict=True)
  4497. self.assertEqual(self.get_constraints_for_column(Author, "weight"), [])
  4498. def test_alter_pk_with_self_referential_field(self):
  4499. """
  4500. Changing the primary key field name of a model with a self-referential
  4501. foreign key (#26384).
  4502. """
  4503. with connection.schema_editor() as editor:
  4504. editor.create_model(Node)
  4505. old_field = Node._meta.get_field("node_id")
  4506. new_field = AutoField(primary_key=True)
  4507. new_field.set_attributes_from_name("id")
  4508. with connection.schema_editor() as editor:
  4509. editor.alter_field(Node, old_field, new_field, strict=True)
  4510. self.assertForeignKeyExists(Node, "parent_id", Node._meta.db_table)
  4511. @mock.patch("django.db.backends.base.schema.datetime")
  4512. @mock.patch("django.db.backends.base.schema.timezone")
  4513. def test_add_datefield_and_datetimefield_use_effective_default(
  4514. self, mocked_datetime, mocked_tz
  4515. ):
  4516. """
  4517. effective_default() should be used for DateField, DateTimeField, and
  4518. TimeField if auto_now or auto_now_add is set (#25005).
  4519. """
  4520. now = datetime.datetime(month=1, day=1, year=2000, hour=1, minute=1)
  4521. now_tz = datetime.datetime(
  4522. month=1, day=1, year=2000, hour=1, minute=1, tzinfo=datetime.timezone.utc
  4523. )
  4524. mocked_datetime.now = mock.MagicMock(return_value=now)
  4525. mocked_tz.now = mock.MagicMock(return_value=now_tz)
  4526. # Create the table
  4527. with connection.schema_editor() as editor:
  4528. editor.create_model(Author)
  4529. # Check auto_now/auto_now_add attributes are not defined
  4530. columns = self.column_classes(Author)
  4531. self.assertNotIn("dob_auto_now", columns)
  4532. self.assertNotIn("dob_auto_now_add", columns)
  4533. self.assertNotIn("dtob_auto_now", columns)
  4534. self.assertNotIn("dtob_auto_now_add", columns)
  4535. self.assertNotIn("tob_auto_now", columns)
  4536. self.assertNotIn("tob_auto_now_add", columns)
  4537. # Create a row
  4538. Author.objects.create(name="Anonymous1")
  4539. # Ensure fields were added with the correct defaults
  4540. dob_auto_now = DateField(auto_now=True)
  4541. dob_auto_now.set_attributes_from_name("dob_auto_now")
  4542. self.check_added_field_default(
  4543. editor,
  4544. Author,
  4545. dob_auto_now,
  4546. "dob_auto_now",
  4547. now.date(),
  4548. cast_function=lambda x: x.date(),
  4549. )
  4550. dob_auto_now_add = DateField(auto_now_add=True)
  4551. dob_auto_now_add.set_attributes_from_name("dob_auto_now_add")
  4552. self.check_added_field_default(
  4553. editor,
  4554. Author,
  4555. dob_auto_now_add,
  4556. "dob_auto_now_add",
  4557. now.date(),
  4558. cast_function=lambda x: x.date(),
  4559. )
  4560. dtob_auto_now = DateTimeField(auto_now=True)
  4561. dtob_auto_now.set_attributes_from_name("dtob_auto_now")
  4562. self.check_added_field_default(
  4563. editor,
  4564. Author,
  4565. dtob_auto_now,
  4566. "dtob_auto_now",
  4567. now,
  4568. )
  4569. dt_tm_of_birth_auto_now_add = DateTimeField(auto_now_add=True)
  4570. dt_tm_of_birth_auto_now_add.set_attributes_from_name("dtob_auto_now_add")
  4571. self.check_added_field_default(
  4572. editor,
  4573. Author,
  4574. dt_tm_of_birth_auto_now_add,
  4575. "dtob_auto_now_add",
  4576. now,
  4577. )
  4578. tob_auto_now = TimeField(auto_now=True)
  4579. tob_auto_now.set_attributes_from_name("tob_auto_now")
  4580. self.check_added_field_default(
  4581. editor,
  4582. Author,
  4583. tob_auto_now,
  4584. "tob_auto_now",
  4585. now.time(),
  4586. cast_function=lambda x: x.time(),
  4587. )
  4588. tob_auto_now_add = TimeField(auto_now_add=True)
  4589. tob_auto_now_add.set_attributes_from_name("tob_auto_now_add")
  4590. self.check_added_field_default(
  4591. editor,
  4592. Author,
  4593. tob_auto_now_add,
  4594. "tob_auto_now_add",
  4595. now.time(),
  4596. cast_function=lambda x: x.time(),
  4597. )
  4598. def test_namespaced_db_table_create_index_name(self):
  4599. """
  4600. Table names are stripped of their namespace/schema before being used to
  4601. generate index names.
  4602. """
  4603. with connection.schema_editor() as editor:
  4604. max_name_length = connection.ops.max_name_length() or 200
  4605. namespace = "n" * max_name_length
  4606. table_name = "t" * max_name_length
  4607. namespaced_table_name = '"%s"."%s"' % (namespace, table_name)
  4608. self.assertEqual(
  4609. editor._create_index_name(table_name, []),
  4610. editor._create_index_name(namespaced_table_name, []),
  4611. )
  4612. @unittest.skipUnless(
  4613. connection.vendor == "oracle", "Oracle specific db_table syntax"
  4614. )
  4615. def test_creation_with_db_table_double_quotes(self):
  4616. oracle_user = connection.creation._test_database_user()
  4617. class Student(Model):
  4618. name = CharField(max_length=30)
  4619. class Meta:
  4620. app_label = "schema"
  4621. apps = new_apps
  4622. db_table = '"%s"."DJANGO_STUDENT_TABLE"' % oracle_user
  4623. class Document(Model):
  4624. name = CharField(max_length=30)
  4625. students = ManyToManyField(Student)
  4626. class Meta:
  4627. app_label = "schema"
  4628. apps = new_apps
  4629. db_table = '"%s"."DJANGO_DOCUMENT_TABLE"' % oracle_user
  4630. self.isolated_local_models = [Student, Document]
  4631. with connection.schema_editor() as editor:
  4632. editor.create_model(Student)
  4633. editor.create_model(Document)
  4634. doc = Document.objects.create(name="Test Name")
  4635. student = Student.objects.create(name="Some man")
  4636. doc.students.add(student)
  4637. @isolate_apps("schema")
  4638. @unittest.skipUnless(
  4639. connection.vendor == "postgresql", "PostgreSQL specific db_table syntax."
  4640. )
  4641. def test_namespaced_db_table_foreign_key_reference(self):
  4642. with connection.cursor() as cursor:
  4643. cursor.execute("CREATE SCHEMA django_schema_tests")
  4644. def delete_schema():
  4645. with connection.cursor() as cursor:
  4646. cursor.execute("DROP SCHEMA django_schema_tests CASCADE")
  4647. self.addCleanup(delete_schema)
  4648. class Author(Model):
  4649. class Meta:
  4650. app_label = "schema"
  4651. class Book(Model):
  4652. class Meta:
  4653. app_label = "schema"
  4654. db_table = '"django_schema_tests"."schema_book"'
  4655. author = ForeignKey(Author, CASCADE)
  4656. author.set_attributes_from_name("author")
  4657. with connection.schema_editor() as editor:
  4658. editor.create_model(Author)
  4659. editor.create_model(Book)
  4660. editor.add_field(Book, author)
  4661. def test_rename_table_renames_deferred_sql_references(self):
  4662. atomic_rename = connection.features.supports_atomic_references_rename
  4663. with connection.schema_editor(atomic=atomic_rename) as editor:
  4664. editor.create_model(Author)
  4665. editor.create_model(Book)
  4666. editor.alter_db_table(Author, "schema_author", "schema_renamed_author")
  4667. editor.alter_db_table(Author, "schema_book", "schema_renamed_book")
  4668. try:
  4669. self.assertGreater(len(editor.deferred_sql), 0)
  4670. for statement in editor.deferred_sql:
  4671. self.assertIs(statement.references_table("schema_author"), False)
  4672. self.assertIs(statement.references_table("schema_book"), False)
  4673. finally:
  4674. editor.alter_db_table(Author, "schema_renamed_author", "schema_author")
  4675. editor.alter_db_table(Author, "schema_renamed_book", "schema_book")
  4676. def test_rename_column_renames_deferred_sql_references(self):
  4677. with connection.schema_editor() as editor:
  4678. editor.create_model(Author)
  4679. editor.create_model(Book)
  4680. old_title = Book._meta.get_field("title")
  4681. new_title = CharField(max_length=100, db_index=True)
  4682. new_title.set_attributes_from_name("renamed_title")
  4683. editor.alter_field(Book, old_title, new_title)
  4684. old_author = Book._meta.get_field("author")
  4685. new_author = ForeignKey(Author, CASCADE)
  4686. new_author.set_attributes_from_name("renamed_author")
  4687. editor.alter_field(Book, old_author, new_author)
  4688. self.assertGreater(len(editor.deferred_sql), 0)
  4689. for statement in editor.deferred_sql:
  4690. self.assertIs(statement.references_column("book", "title"), False)
  4691. self.assertIs(statement.references_column("book", "author_id"), False)
  4692. @isolate_apps("schema")
  4693. def test_referenced_field_without_constraint_rename_inside_atomic_block(self):
  4694. """
  4695. Foreign keys without database level constraint don't prevent the field
  4696. they reference from being renamed in an atomic block.
  4697. """
  4698. class Foo(Model):
  4699. field = CharField(max_length=255, unique=True)
  4700. class Meta:
  4701. app_label = "schema"
  4702. class Bar(Model):
  4703. foo = ForeignKey(Foo, CASCADE, to_field="field", db_constraint=False)
  4704. class Meta:
  4705. app_label = "schema"
  4706. self.isolated_local_models = [Foo, Bar]
  4707. with connection.schema_editor() as editor:
  4708. editor.create_model(Foo)
  4709. editor.create_model(Bar)
  4710. new_field = CharField(max_length=255, unique=True)
  4711. new_field.set_attributes_from_name("renamed")
  4712. with connection.schema_editor(atomic=True) as editor:
  4713. editor.alter_field(Foo, Foo._meta.get_field("field"), new_field)
  4714. @isolate_apps("schema")
  4715. def test_referenced_table_without_constraint_rename_inside_atomic_block(self):
  4716. """
  4717. Foreign keys without database level constraint don't prevent the table
  4718. they reference from being renamed in an atomic block.
  4719. """
  4720. class Foo(Model):
  4721. field = CharField(max_length=255, unique=True)
  4722. class Meta:
  4723. app_label = "schema"
  4724. class Bar(Model):
  4725. foo = ForeignKey(Foo, CASCADE, to_field="field", db_constraint=False)
  4726. class Meta:
  4727. app_label = "schema"
  4728. self.isolated_local_models = [Foo, Bar]
  4729. with connection.schema_editor() as editor:
  4730. editor.create_model(Foo)
  4731. editor.create_model(Bar)
  4732. new_field = CharField(max_length=255, unique=True)
  4733. new_field.set_attributes_from_name("renamed")
  4734. with connection.schema_editor(atomic=True) as editor:
  4735. editor.alter_db_table(Foo, Foo._meta.db_table, "renamed_table")
  4736. Foo._meta.db_table = "renamed_table"
  4737. @isolate_apps("schema")
  4738. @skipUnlessDBFeature("supports_collation_on_charfield")
  4739. def test_db_collation_charfield(self):
  4740. collation = connection.features.test_collations.get("non_default")
  4741. if not collation:
  4742. self.skipTest("Language collations are not supported.")
  4743. class Foo(Model):
  4744. field = CharField(max_length=255, db_collation=collation)
  4745. class Meta:
  4746. app_label = "schema"
  4747. self.isolated_local_models = [Foo]
  4748. with connection.schema_editor() as editor:
  4749. editor.create_model(Foo)
  4750. self.assertEqual(
  4751. self.get_column_collation(Foo._meta.db_table, "field"),
  4752. collation,
  4753. )
  4754. @isolate_apps("schema")
  4755. @skipUnlessDBFeature("supports_collation_on_textfield")
  4756. def test_db_collation_textfield(self):
  4757. collation = connection.features.test_collations.get("non_default")
  4758. if not collation:
  4759. self.skipTest("Language collations are not supported.")
  4760. class Foo(Model):
  4761. field = TextField(db_collation=collation)
  4762. class Meta:
  4763. app_label = "schema"
  4764. self.isolated_local_models = [Foo]
  4765. with connection.schema_editor() as editor:
  4766. editor.create_model(Foo)
  4767. self.assertEqual(
  4768. self.get_column_collation(Foo._meta.db_table, "field"),
  4769. collation,
  4770. )
  4771. @skipUnlessDBFeature("supports_collation_on_charfield")
  4772. def test_add_field_db_collation(self):
  4773. collation = connection.features.test_collations.get("non_default")
  4774. if not collation:
  4775. self.skipTest("Language collations are not supported.")
  4776. with connection.schema_editor() as editor:
  4777. editor.create_model(Author)
  4778. new_field = CharField(max_length=255, db_collation=collation)
  4779. new_field.set_attributes_from_name("alias")
  4780. with connection.schema_editor() as editor:
  4781. editor.add_field(Author, new_field)
  4782. columns = self.column_classes(Author)
  4783. self.assertEqual(
  4784. columns["alias"][0],
  4785. connection.features.introspected_field_types["CharField"],
  4786. )
  4787. self.assertEqual(columns["alias"][1][8], collation)
  4788. @skipUnlessDBFeature("supports_collation_on_charfield")
  4789. def test_alter_field_db_collation(self):
  4790. collation = connection.features.test_collations.get("non_default")
  4791. if not collation:
  4792. self.skipTest("Language collations are not supported.")
  4793. with connection.schema_editor() as editor:
  4794. editor.create_model(Author)
  4795. old_field = Author._meta.get_field("name")
  4796. new_field = CharField(max_length=255, db_collation=collation)
  4797. new_field.set_attributes_from_name("name")
  4798. new_field.model = Author
  4799. with connection.schema_editor() as editor:
  4800. editor.alter_field(Author, old_field, new_field, strict=True)
  4801. self.assertEqual(
  4802. self.get_column_collation(Author._meta.db_table, "name"),
  4803. collation,
  4804. )
  4805. with connection.schema_editor() as editor:
  4806. editor.alter_field(Author, new_field, old_field, strict=True)
  4807. self.assertIsNone(self.get_column_collation(Author._meta.db_table, "name"))
  4808. @skipUnlessDBFeature("supports_collation_on_charfield")
  4809. def test_alter_field_type_preserve_db_collation(self):
  4810. collation = connection.features.test_collations.get("non_default")
  4811. if not collation:
  4812. self.skipTest("Language collations are not supported.")
  4813. with connection.schema_editor() as editor:
  4814. editor.create_model(Author)
  4815. old_field = Author._meta.get_field("name")
  4816. new_field = CharField(max_length=255, db_collation=collation)
  4817. new_field.set_attributes_from_name("name")
  4818. new_field.model = Author
  4819. with connection.schema_editor() as editor:
  4820. editor.alter_field(Author, old_field, new_field, strict=True)
  4821. self.assertEqual(
  4822. self.get_column_collation(Author._meta.db_table, "name"),
  4823. collation,
  4824. )
  4825. # Changing a field type should preserve the collation.
  4826. old_field = new_field
  4827. new_field = CharField(max_length=511, db_collation=collation)
  4828. new_field.set_attributes_from_name("name")
  4829. new_field.model = Author
  4830. with connection.schema_editor() as editor:
  4831. editor.alter_field(Author, new_field, old_field, strict=True)
  4832. # Collation is preserved.
  4833. self.assertEqual(
  4834. self.get_column_collation(Author._meta.db_table, "name"),
  4835. collation,
  4836. )
  4837. @skipUnlessDBFeature("supports_collation_on_charfield")
  4838. def test_alter_primary_key_db_collation(self):
  4839. collation = connection.features.test_collations.get("non_default")
  4840. if not collation:
  4841. self.skipTest("Language collations are not supported.")
  4842. with connection.schema_editor() as editor:
  4843. editor.create_model(Thing)
  4844. old_field = Thing._meta.get_field("when")
  4845. new_field = CharField(max_length=1, db_collation=collation, primary_key=True)
  4846. new_field.set_attributes_from_name("when")
  4847. new_field.model = Thing
  4848. with connection.schema_editor() as editor:
  4849. editor.alter_field(Thing, old_field, new_field, strict=True)
  4850. self.assertEqual(self.get_primary_key(Thing._meta.db_table), "when")
  4851. self.assertEqual(
  4852. self.get_column_collation(Thing._meta.db_table, "when"),
  4853. collation,
  4854. )
  4855. with connection.schema_editor() as editor:
  4856. editor.alter_field(Thing, new_field, old_field, strict=True)
  4857. self.assertEqual(self.get_primary_key(Thing._meta.db_table), "when")
  4858. self.assertIsNone(self.get_column_collation(Thing._meta.db_table, "when"))
  4859. @skipUnlessDBFeature(
  4860. "supports_collation_on_charfield", "supports_collation_on_textfield"
  4861. )
  4862. def test_alter_field_type_and_db_collation(self):
  4863. collation = connection.features.test_collations.get("non_default")
  4864. if not collation:
  4865. self.skipTest("Language collations are not supported.")
  4866. with connection.schema_editor() as editor:
  4867. editor.create_model(Note)
  4868. old_field = Note._meta.get_field("info")
  4869. new_field = CharField(max_length=255, db_collation=collation)
  4870. new_field.set_attributes_from_name("info")
  4871. new_field.model = Note
  4872. with connection.schema_editor() as editor:
  4873. editor.alter_field(Note, old_field, new_field, strict=True)
  4874. columns = self.column_classes(Note)
  4875. self.assertEqual(
  4876. columns["info"][0],
  4877. connection.features.introspected_field_types["CharField"],
  4878. )
  4879. self.assertEqual(columns["info"][1][8], collation)
  4880. with connection.schema_editor() as editor:
  4881. editor.alter_field(Note, new_field, old_field, strict=True)
  4882. columns = self.column_classes(Note)
  4883. self.assertEqual(columns["info"][0], "TextField")
  4884. self.assertIsNone(columns["info"][1][8])
  4885. @skipUnlessDBFeature(
  4886. "supports_collation_on_charfield",
  4887. "supports_non_deterministic_collations",
  4888. )
  4889. def test_ci_cs_db_collation(self):
  4890. cs_collation = connection.features.test_collations.get("cs")
  4891. ci_collation = connection.features.test_collations.get("ci")
  4892. try:
  4893. if connection.vendor == "mysql":
  4894. cs_collation = "latin1_general_cs"
  4895. elif connection.vendor == "postgresql":
  4896. cs_collation = "en-x-icu"
  4897. with connection.cursor() as cursor:
  4898. cursor.execute(
  4899. "CREATE COLLATION IF NOT EXISTS case_insensitive "
  4900. "(provider = icu, locale = 'und-u-ks-level2', "
  4901. "deterministic = false)"
  4902. )
  4903. ci_collation = "case_insensitive"
  4904. # Create the table.
  4905. with connection.schema_editor() as editor:
  4906. editor.create_model(Author)
  4907. # Case-insensitive collation.
  4908. old_field = Author._meta.get_field("name")
  4909. new_field_ci = CharField(max_length=255, db_collation=ci_collation)
  4910. new_field_ci.set_attributes_from_name("name")
  4911. new_field_ci.model = Author
  4912. with connection.schema_editor() as editor:
  4913. editor.alter_field(Author, old_field, new_field_ci, strict=True)
  4914. Author.objects.create(name="ANDREW")
  4915. self.assertIs(Author.objects.filter(name="Andrew").exists(), True)
  4916. # Case-sensitive collation.
  4917. new_field_cs = CharField(max_length=255, db_collation=cs_collation)
  4918. new_field_cs.set_attributes_from_name("name")
  4919. new_field_cs.model = Author
  4920. with connection.schema_editor() as editor:
  4921. editor.alter_field(Author, new_field_ci, new_field_cs, strict=True)
  4922. self.assertIs(Author.objects.filter(name="Andrew").exists(), False)
  4923. finally:
  4924. if connection.vendor == "postgresql":
  4925. with connection.cursor() as cursor:
  4926. cursor.execute("DROP COLLATION IF EXISTS case_insensitive")