test_autodetector.py 192 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306
  1. import copy
  2. import functools
  3. import re
  4. from unittest import mock
  5. from django.apps import apps
  6. from django.conf import settings
  7. from django.contrib.auth.models import AbstractBaseUser
  8. from django.core.validators import RegexValidator, validate_slug
  9. from django.db import connection, migrations, models
  10. from django.db.migrations.autodetector import MigrationAutodetector
  11. from django.db.migrations.graph import MigrationGraph
  12. from django.db.migrations.loader import MigrationLoader
  13. from django.db.migrations.questioner import MigrationQuestioner
  14. from django.db.migrations.state import ModelState, ProjectState
  15. from django.db.models.functions import Concat, Lower
  16. from django.test import SimpleTestCase, TestCase, override_settings
  17. from django.test.utils import isolate_lru_cache
  18. from .models import FoodManager, FoodQuerySet
  19. class DeconstructibleObject:
  20. """
  21. A custom deconstructible object.
  22. """
  23. def __init__(self, *args, **kwargs):
  24. self.args = args
  25. self.kwargs = kwargs
  26. def deconstruct(self):
  27. return (self.__module__ + "." + self.__class__.__name__, self.args, self.kwargs)
  28. class BaseAutodetectorTests(TestCase):
  29. def repr_changes(self, changes, include_dependencies=False):
  30. output = ""
  31. for app_label, migrations_ in sorted(changes.items()):
  32. output += " %s:\n" % app_label
  33. for migration in migrations_:
  34. output += " %s\n" % migration.name
  35. for operation in migration.operations:
  36. output += " %s\n" % operation
  37. if include_dependencies:
  38. output += " Dependencies:\n"
  39. if migration.dependencies:
  40. for dep in migration.dependencies:
  41. output += " %s\n" % (dep,)
  42. else:
  43. output += " None\n"
  44. return output
  45. def assertNumberMigrations(self, changes, app_label, number):
  46. if len(changes.get(app_label, [])) != number:
  47. self.fail(
  48. "Incorrect number of migrations (%s) for %s (expected %s)\n%s"
  49. % (
  50. len(changes.get(app_label, [])),
  51. app_label,
  52. number,
  53. self.repr_changes(changes),
  54. )
  55. )
  56. def assertMigrationDependencies(self, changes, app_label, position, dependencies):
  57. if not changes.get(app_label):
  58. self.fail(
  59. "No migrations found for %s\n%s"
  60. % (app_label, self.repr_changes(changes))
  61. )
  62. if len(changes[app_label]) < position + 1:
  63. self.fail(
  64. "No migration at index %s for %s\n%s"
  65. % (position, app_label, self.repr_changes(changes))
  66. )
  67. migration = changes[app_label][position]
  68. if set(migration.dependencies) != set(dependencies):
  69. self.fail(
  70. "Migration dependencies mismatch for %s.%s (expected %s):\n%s"
  71. % (
  72. app_label,
  73. migration.name,
  74. dependencies,
  75. self.repr_changes(changes, include_dependencies=True),
  76. )
  77. )
  78. def assertOperationTypes(self, changes, app_label, position, types):
  79. if not changes.get(app_label):
  80. self.fail(
  81. "No migrations found for %s\n%s"
  82. % (app_label, self.repr_changes(changes))
  83. )
  84. if len(changes[app_label]) < position + 1:
  85. self.fail(
  86. "No migration at index %s for %s\n%s"
  87. % (position, app_label, self.repr_changes(changes))
  88. )
  89. migration = changes[app_label][position]
  90. real_types = [
  91. operation.__class__.__name__ for operation in migration.operations
  92. ]
  93. if types != real_types:
  94. self.fail(
  95. "Operation type mismatch for %s.%s (expected %s):\n%s"
  96. % (
  97. app_label,
  98. migration.name,
  99. types,
  100. self.repr_changes(changes),
  101. )
  102. )
  103. def assertOperationAttributes(
  104. self, changes, app_label, position, operation_position, **attrs
  105. ):
  106. if not changes.get(app_label):
  107. self.fail(
  108. "No migrations found for %s\n%s"
  109. % (app_label, self.repr_changes(changes))
  110. )
  111. if len(changes[app_label]) < position + 1:
  112. self.fail(
  113. "No migration at index %s for %s\n%s"
  114. % (position, app_label, self.repr_changes(changes))
  115. )
  116. migration = changes[app_label][position]
  117. if len(changes[app_label]) < position + 1:
  118. self.fail(
  119. "No operation at index %s for %s.%s\n%s"
  120. % (
  121. operation_position,
  122. app_label,
  123. migration.name,
  124. self.repr_changes(changes),
  125. )
  126. )
  127. operation = migration.operations[operation_position]
  128. for attr, value in attrs.items():
  129. if getattr(operation, attr, None) != value:
  130. self.fail(
  131. "Attribute mismatch for %s.%s op #%s, %s (expected %r, got %r):\n%s"
  132. % (
  133. app_label,
  134. migration.name,
  135. operation_position,
  136. attr,
  137. value,
  138. getattr(operation, attr, None),
  139. self.repr_changes(changes),
  140. )
  141. )
  142. def assertOperationFieldAttributes(
  143. self, changes, app_label, position, operation_position, **attrs
  144. ):
  145. if not changes.get(app_label):
  146. self.fail(
  147. "No migrations found for %s\n%s"
  148. % (app_label, self.repr_changes(changes))
  149. )
  150. if len(changes[app_label]) < position + 1:
  151. self.fail(
  152. "No migration at index %s for %s\n%s"
  153. % (position, app_label, self.repr_changes(changes))
  154. )
  155. migration = changes[app_label][position]
  156. if len(changes[app_label]) < position + 1:
  157. self.fail(
  158. "No operation at index %s for %s.%s\n%s"
  159. % (
  160. operation_position,
  161. app_label,
  162. migration.name,
  163. self.repr_changes(changes),
  164. )
  165. )
  166. operation = migration.operations[operation_position]
  167. if not hasattr(operation, "field"):
  168. self.fail(
  169. "No field attribute for %s.%s op #%s."
  170. % (
  171. app_label,
  172. migration.name,
  173. operation_position,
  174. )
  175. )
  176. field = operation.field
  177. for attr, value in attrs.items():
  178. if getattr(field, attr, None) != value:
  179. self.fail(
  180. "Field attribute mismatch for %s.%s op #%s, field.%s (expected %r, "
  181. "got %r):\n%s"
  182. % (
  183. app_label,
  184. migration.name,
  185. operation_position,
  186. attr,
  187. value,
  188. getattr(field, attr, None),
  189. self.repr_changes(changes),
  190. )
  191. )
  192. def make_project_state(self, model_states):
  193. "Shortcut to make ProjectStates from lists of predefined models"
  194. project_state = ProjectState()
  195. for model_state in model_states:
  196. project_state.add_model(model_state.clone())
  197. return project_state
  198. def get_changes(self, before_states, after_states, questioner=None):
  199. if not isinstance(before_states, ProjectState):
  200. before_states = self.make_project_state(before_states)
  201. if not isinstance(after_states, ProjectState):
  202. after_states = self.make_project_state(after_states)
  203. return MigrationAutodetector(
  204. before_states,
  205. after_states,
  206. questioner,
  207. )._detect_changes()
  208. class AutodetectorTests(BaseAutodetectorTests):
  209. """
  210. Tests the migration autodetector.
  211. """
  212. author_empty = ModelState(
  213. "testapp", "Author", [("id", models.AutoField(primary_key=True))]
  214. )
  215. author_name = ModelState(
  216. "testapp",
  217. "Author",
  218. [
  219. ("id", models.AutoField(primary_key=True)),
  220. ("name", models.CharField(max_length=200)),
  221. ],
  222. )
  223. author_name_null = ModelState(
  224. "testapp",
  225. "Author",
  226. [
  227. ("id", models.AutoField(primary_key=True)),
  228. ("name", models.CharField(max_length=200, null=True)),
  229. ],
  230. )
  231. author_name_longer = ModelState(
  232. "testapp",
  233. "Author",
  234. [
  235. ("id", models.AutoField(primary_key=True)),
  236. ("name", models.CharField(max_length=400)),
  237. ],
  238. )
  239. author_name_renamed = ModelState(
  240. "testapp",
  241. "Author",
  242. [
  243. ("id", models.AutoField(primary_key=True)),
  244. ("names", models.CharField(max_length=200)),
  245. ],
  246. )
  247. author_name_default = ModelState(
  248. "testapp",
  249. "Author",
  250. [
  251. ("id", models.AutoField(primary_key=True)),
  252. ("name", models.CharField(max_length=200, default="Ada Lovelace")),
  253. ],
  254. )
  255. author_name_db_default = ModelState(
  256. "testapp",
  257. "Author",
  258. [
  259. ("id", models.AutoField(primary_key=True)),
  260. ("name", models.CharField(max_length=200, db_default="Ada Lovelace")),
  261. ],
  262. )
  263. author_name_check_constraint = ModelState(
  264. "testapp",
  265. "Author",
  266. [
  267. ("id", models.AutoField(primary_key=True)),
  268. ("name", models.CharField(max_length=200)),
  269. ],
  270. {
  271. "constraints": [
  272. models.CheckConstraint(
  273. condition=models.Q(name__contains="Bob"), name="name_contains_bob"
  274. )
  275. ]
  276. },
  277. )
  278. author_dates_of_birth_auto_now = ModelState(
  279. "testapp",
  280. "Author",
  281. [
  282. ("id", models.AutoField(primary_key=True)),
  283. ("date_of_birth", models.DateField(auto_now=True)),
  284. ("date_time_of_birth", models.DateTimeField(auto_now=True)),
  285. ("time_of_birth", models.TimeField(auto_now=True)),
  286. ],
  287. )
  288. author_dates_of_birth_auto_now_add = ModelState(
  289. "testapp",
  290. "Author",
  291. [
  292. ("id", models.AutoField(primary_key=True)),
  293. ("date_of_birth", models.DateField(auto_now_add=True)),
  294. ("date_time_of_birth", models.DateTimeField(auto_now_add=True)),
  295. ("time_of_birth", models.TimeField(auto_now_add=True)),
  296. ],
  297. )
  298. author_name_deconstructible_1 = ModelState(
  299. "testapp",
  300. "Author",
  301. [
  302. ("id", models.AutoField(primary_key=True)),
  303. ("name", models.CharField(max_length=200, default=DeconstructibleObject())),
  304. ],
  305. )
  306. author_name_deconstructible_2 = ModelState(
  307. "testapp",
  308. "Author",
  309. [
  310. ("id", models.AutoField(primary_key=True)),
  311. ("name", models.CharField(max_length=200, default=DeconstructibleObject())),
  312. ],
  313. )
  314. author_name_deconstructible_3 = ModelState(
  315. "testapp",
  316. "Author",
  317. [
  318. ("id", models.AutoField(primary_key=True)),
  319. ("name", models.CharField(max_length=200, default=models.IntegerField())),
  320. ],
  321. )
  322. author_name_deconstructible_4 = ModelState(
  323. "testapp",
  324. "Author",
  325. [
  326. ("id", models.AutoField(primary_key=True)),
  327. ("name", models.CharField(max_length=200, default=models.IntegerField())),
  328. ],
  329. )
  330. author_name_deconstructible_list_1 = ModelState(
  331. "testapp",
  332. "Author",
  333. [
  334. ("id", models.AutoField(primary_key=True)),
  335. (
  336. "name",
  337. models.CharField(
  338. max_length=200, default=[DeconstructibleObject(), 123]
  339. ),
  340. ),
  341. ],
  342. )
  343. author_name_deconstructible_list_2 = ModelState(
  344. "testapp",
  345. "Author",
  346. [
  347. ("id", models.AutoField(primary_key=True)),
  348. (
  349. "name",
  350. models.CharField(
  351. max_length=200, default=[DeconstructibleObject(), 123]
  352. ),
  353. ),
  354. ],
  355. )
  356. author_name_deconstructible_list_3 = ModelState(
  357. "testapp",
  358. "Author",
  359. [
  360. ("id", models.AutoField(primary_key=True)),
  361. (
  362. "name",
  363. models.CharField(
  364. max_length=200, default=[DeconstructibleObject(), 999]
  365. ),
  366. ),
  367. ],
  368. )
  369. author_name_deconstructible_tuple_1 = ModelState(
  370. "testapp",
  371. "Author",
  372. [
  373. ("id", models.AutoField(primary_key=True)),
  374. (
  375. "name",
  376. models.CharField(
  377. max_length=200, default=(DeconstructibleObject(), 123)
  378. ),
  379. ),
  380. ],
  381. )
  382. author_name_deconstructible_tuple_2 = ModelState(
  383. "testapp",
  384. "Author",
  385. [
  386. ("id", models.AutoField(primary_key=True)),
  387. (
  388. "name",
  389. models.CharField(
  390. max_length=200, default=(DeconstructibleObject(), 123)
  391. ),
  392. ),
  393. ],
  394. )
  395. author_name_deconstructible_tuple_3 = ModelState(
  396. "testapp",
  397. "Author",
  398. [
  399. ("id", models.AutoField(primary_key=True)),
  400. (
  401. "name",
  402. models.CharField(
  403. max_length=200, default=(DeconstructibleObject(), 999)
  404. ),
  405. ),
  406. ],
  407. )
  408. author_name_deconstructible_dict_1 = ModelState(
  409. "testapp",
  410. "Author",
  411. [
  412. ("id", models.AutoField(primary_key=True)),
  413. (
  414. "name",
  415. models.CharField(
  416. max_length=200,
  417. default={"item": DeconstructibleObject(), "otheritem": 123},
  418. ),
  419. ),
  420. ],
  421. )
  422. author_name_deconstructible_dict_2 = ModelState(
  423. "testapp",
  424. "Author",
  425. [
  426. ("id", models.AutoField(primary_key=True)),
  427. (
  428. "name",
  429. models.CharField(
  430. max_length=200,
  431. default={"item": DeconstructibleObject(), "otheritem": 123},
  432. ),
  433. ),
  434. ],
  435. )
  436. author_name_deconstructible_dict_3 = ModelState(
  437. "testapp",
  438. "Author",
  439. [
  440. ("id", models.AutoField(primary_key=True)),
  441. (
  442. "name",
  443. models.CharField(
  444. max_length=200,
  445. default={"item": DeconstructibleObject(), "otheritem": 999},
  446. ),
  447. ),
  448. ],
  449. )
  450. author_name_nested_deconstructible_1 = ModelState(
  451. "testapp",
  452. "Author",
  453. [
  454. ("id", models.AutoField(primary_key=True)),
  455. (
  456. "name",
  457. models.CharField(
  458. max_length=200,
  459. default=DeconstructibleObject(
  460. DeconstructibleObject(1),
  461. (
  462. DeconstructibleObject("t1"),
  463. DeconstructibleObject("t2"),
  464. ),
  465. a=DeconstructibleObject("A"),
  466. b=DeconstructibleObject(B=DeconstructibleObject("c")),
  467. ),
  468. ),
  469. ),
  470. ],
  471. )
  472. author_name_nested_deconstructible_2 = ModelState(
  473. "testapp",
  474. "Author",
  475. [
  476. ("id", models.AutoField(primary_key=True)),
  477. (
  478. "name",
  479. models.CharField(
  480. max_length=200,
  481. default=DeconstructibleObject(
  482. DeconstructibleObject(1),
  483. (
  484. DeconstructibleObject("t1"),
  485. DeconstructibleObject("t2"),
  486. ),
  487. a=DeconstructibleObject("A"),
  488. b=DeconstructibleObject(B=DeconstructibleObject("c")),
  489. ),
  490. ),
  491. ),
  492. ],
  493. )
  494. author_name_nested_deconstructible_changed_arg = ModelState(
  495. "testapp",
  496. "Author",
  497. [
  498. ("id", models.AutoField(primary_key=True)),
  499. (
  500. "name",
  501. models.CharField(
  502. max_length=200,
  503. default=DeconstructibleObject(
  504. DeconstructibleObject(1),
  505. (
  506. DeconstructibleObject("t1"),
  507. DeconstructibleObject("t2-changed"),
  508. ),
  509. a=DeconstructibleObject("A"),
  510. b=DeconstructibleObject(B=DeconstructibleObject("c")),
  511. ),
  512. ),
  513. ),
  514. ],
  515. )
  516. author_name_nested_deconstructible_extra_arg = ModelState(
  517. "testapp",
  518. "Author",
  519. [
  520. ("id", models.AutoField(primary_key=True)),
  521. (
  522. "name",
  523. models.CharField(
  524. max_length=200,
  525. default=DeconstructibleObject(
  526. DeconstructibleObject(1),
  527. (
  528. DeconstructibleObject("t1"),
  529. DeconstructibleObject("t2"),
  530. ),
  531. None,
  532. a=DeconstructibleObject("A"),
  533. b=DeconstructibleObject(B=DeconstructibleObject("c")),
  534. ),
  535. ),
  536. ),
  537. ],
  538. )
  539. author_name_nested_deconstructible_changed_kwarg = ModelState(
  540. "testapp",
  541. "Author",
  542. [
  543. ("id", models.AutoField(primary_key=True)),
  544. (
  545. "name",
  546. models.CharField(
  547. max_length=200,
  548. default=DeconstructibleObject(
  549. DeconstructibleObject(1),
  550. (
  551. DeconstructibleObject("t1"),
  552. DeconstructibleObject("t2"),
  553. ),
  554. a=DeconstructibleObject("A"),
  555. b=DeconstructibleObject(B=DeconstructibleObject("c-changed")),
  556. ),
  557. ),
  558. ),
  559. ],
  560. )
  561. author_name_nested_deconstructible_extra_kwarg = ModelState(
  562. "testapp",
  563. "Author",
  564. [
  565. ("id", models.AutoField(primary_key=True)),
  566. (
  567. "name",
  568. models.CharField(
  569. max_length=200,
  570. default=DeconstructibleObject(
  571. DeconstructibleObject(1),
  572. (
  573. DeconstructibleObject("t1"),
  574. DeconstructibleObject("t2"),
  575. ),
  576. a=DeconstructibleObject("A"),
  577. b=DeconstructibleObject(B=DeconstructibleObject("c")),
  578. c=None,
  579. ),
  580. ),
  581. ),
  582. ],
  583. )
  584. author_custom_pk = ModelState(
  585. "testapp", "Author", [("pk_field", models.IntegerField(primary_key=True))]
  586. )
  587. author_with_biography_non_blank = ModelState(
  588. "testapp",
  589. "Author",
  590. [
  591. ("id", models.AutoField(primary_key=True)),
  592. ("name", models.CharField()),
  593. ("biography", models.TextField()),
  594. ],
  595. )
  596. author_with_biography_blank = ModelState(
  597. "testapp",
  598. "Author",
  599. [
  600. ("id", models.AutoField(primary_key=True)),
  601. ("name", models.CharField(blank=True)),
  602. ("biography", models.TextField(blank=True)),
  603. ],
  604. )
  605. author_with_book = ModelState(
  606. "testapp",
  607. "Author",
  608. [
  609. ("id", models.AutoField(primary_key=True)),
  610. ("name", models.CharField(max_length=200)),
  611. ("book", models.ForeignKey("otherapp.Book", models.CASCADE)),
  612. ],
  613. )
  614. author_with_book_order_wrt = ModelState(
  615. "testapp",
  616. "Author",
  617. [
  618. ("id", models.AutoField(primary_key=True)),
  619. ("name", models.CharField(max_length=200)),
  620. ("book", models.ForeignKey("otherapp.Book", models.CASCADE)),
  621. ],
  622. options={"order_with_respect_to": "book"},
  623. )
  624. author_renamed_with_book = ModelState(
  625. "testapp",
  626. "Writer",
  627. [
  628. ("id", models.AutoField(primary_key=True)),
  629. ("name", models.CharField(max_length=200)),
  630. ("book", models.ForeignKey("otherapp.Book", models.CASCADE)),
  631. ],
  632. )
  633. author_with_publisher_string = ModelState(
  634. "testapp",
  635. "Author",
  636. [
  637. ("id", models.AutoField(primary_key=True)),
  638. ("name", models.CharField(max_length=200)),
  639. ("publisher_name", models.CharField(max_length=200)),
  640. ],
  641. )
  642. author_with_publisher = ModelState(
  643. "testapp",
  644. "Author",
  645. [
  646. ("id", models.AutoField(primary_key=True)),
  647. ("name", models.CharField(max_length=200)),
  648. ("publisher", models.ForeignKey("testapp.Publisher", models.CASCADE)),
  649. ],
  650. )
  651. author_with_user = ModelState(
  652. "testapp",
  653. "Author",
  654. [
  655. ("id", models.AutoField(primary_key=True)),
  656. ("name", models.CharField(max_length=200)),
  657. ("user", models.ForeignKey("auth.User", models.CASCADE)),
  658. ],
  659. )
  660. author_with_custom_user = ModelState(
  661. "testapp",
  662. "Author",
  663. [
  664. ("id", models.AutoField(primary_key=True)),
  665. ("name", models.CharField(max_length=200)),
  666. ("user", models.ForeignKey("thirdapp.CustomUser", models.CASCADE)),
  667. ],
  668. )
  669. author_proxy = ModelState(
  670. "testapp", "AuthorProxy", [], {"proxy": True}, ("testapp.author",)
  671. )
  672. author_proxy_options = ModelState(
  673. "testapp",
  674. "AuthorProxy",
  675. [],
  676. {
  677. "proxy": True,
  678. "verbose_name": "Super Author",
  679. },
  680. ("testapp.author",),
  681. )
  682. author_proxy_notproxy = ModelState(
  683. "testapp", "AuthorProxy", [], {}, ("testapp.author",)
  684. )
  685. author_proxy_third = ModelState(
  686. "thirdapp", "AuthorProxy", [], {"proxy": True}, ("testapp.author",)
  687. )
  688. author_proxy_third_notproxy = ModelState(
  689. "thirdapp", "AuthorProxy", [], {}, ("testapp.author",)
  690. )
  691. author_proxy_proxy = ModelState(
  692. "testapp", "AAuthorProxyProxy", [], {"proxy": True}, ("testapp.authorproxy",)
  693. )
  694. author_unmanaged = ModelState(
  695. "testapp", "AuthorUnmanaged", [], {"managed": False}, ("testapp.author",)
  696. )
  697. author_unmanaged_managed = ModelState(
  698. "testapp", "AuthorUnmanaged", [], {}, ("testapp.author",)
  699. )
  700. author_unmanaged_default_pk = ModelState(
  701. "testapp", "Author", [("id", models.AutoField(primary_key=True))]
  702. )
  703. author_unmanaged_custom_pk = ModelState(
  704. "testapp",
  705. "Author",
  706. [
  707. ("pk_field", models.IntegerField(primary_key=True)),
  708. ],
  709. )
  710. author_with_m2m = ModelState(
  711. "testapp",
  712. "Author",
  713. [
  714. ("id", models.AutoField(primary_key=True)),
  715. ("publishers", models.ManyToManyField("testapp.Publisher")),
  716. ],
  717. )
  718. author_with_m2m_blank = ModelState(
  719. "testapp",
  720. "Author",
  721. [
  722. ("id", models.AutoField(primary_key=True)),
  723. ("publishers", models.ManyToManyField("testapp.Publisher", blank=True)),
  724. ],
  725. )
  726. author_with_m2m_through = ModelState(
  727. "testapp",
  728. "Author",
  729. [
  730. ("id", models.AutoField(primary_key=True)),
  731. (
  732. "publishers",
  733. models.ManyToManyField("testapp.Publisher", through="testapp.Contract"),
  734. ),
  735. ],
  736. )
  737. author_with_renamed_m2m_through = ModelState(
  738. "testapp",
  739. "Author",
  740. [
  741. ("id", models.AutoField(primary_key=True)),
  742. (
  743. "publishers",
  744. models.ManyToManyField("testapp.Publisher", through="testapp.Deal"),
  745. ),
  746. ],
  747. )
  748. author_with_former_m2m = ModelState(
  749. "testapp",
  750. "Author",
  751. [
  752. ("id", models.AutoField(primary_key=True)),
  753. ("publishers", models.CharField(max_length=100)),
  754. ],
  755. )
  756. author_with_options = ModelState(
  757. "testapp",
  758. "Author",
  759. [
  760. ("id", models.AutoField(primary_key=True)),
  761. ],
  762. {
  763. "permissions": [("can_hire", "Can hire")],
  764. "verbose_name": "Authi",
  765. },
  766. )
  767. author_with_db_table_comment = ModelState(
  768. "testapp",
  769. "Author",
  770. [
  771. ("id", models.AutoField(primary_key=True)),
  772. ],
  773. {"db_table_comment": "Table comment"},
  774. )
  775. author_with_db_table_options = ModelState(
  776. "testapp",
  777. "Author",
  778. [
  779. ("id", models.AutoField(primary_key=True)),
  780. ],
  781. {"db_table": "author_one"},
  782. )
  783. author_with_new_db_table_options = ModelState(
  784. "testapp",
  785. "Author",
  786. [
  787. ("id", models.AutoField(primary_key=True)),
  788. ],
  789. {"db_table": "author_two"},
  790. )
  791. author_renamed_with_db_table_options = ModelState(
  792. "testapp",
  793. "NewAuthor",
  794. [
  795. ("id", models.AutoField(primary_key=True)),
  796. ],
  797. {"db_table": "author_one"},
  798. )
  799. author_renamed_with_new_db_table_options = ModelState(
  800. "testapp",
  801. "NewAuthor",
  802. [
  803. ("id", models.AutoField(primary_key=True)),
  804. ],
  805. {"db_table": "author_three"},
  806. )
  807. contract = ModelState(
  808. "testapp",
  809. "Contract",
  810. [
  811. ("id", models.AutoField(primary_key=True)),
  812. ("author", models.ForeignKey("testapp.Author", models.CASCADE)),
  813. ("publisher", models.ForeignKey("testapp.Publisher", models.CASCADE)),
  814. ],
  815. )
  816. contract_renamed = ModelState(
  817. "testapp",
  818. "Deal",
  819. [
  820. ("id", models.AutoField(primary_key=True)),
  821. ("author", models.ForeignKey("testapp.Author", models.CASCADE)),
  822. ("publisher", models.ForeignKey("testapp.Publisher", models.CASCADE)),
  823. ],
  824. )
  825. publisher = ModelState(
  826. "testapp",
  827. "Publisher",
  828. [
  829. ("id", models.AutoField(primary_key=True)),
  830. ("name", models.CharField(max_length=100)),
  831. ],
  832. )
  833. publisher_with_author = ModelState(
  834. "testapp",
  835. "Publisher",
  836. [
  837. ("id", models.AutoField(primary_key=True)),
  838. ("author", models.ForeignKey("testapp.Author", models.CASCADE)),
  839. ("name", models.CharField(max_length=100)),
  840. ],
  841. )
  842. publisher_with_aardvark_author = ModelState(
  843. "testapp",
  844. "Publisher",
  845. [
  846. ("id", models.AutoField(primary_key=True)),
  847. ("author", models.ForeignKey("testapp.Aardvark", models.CASCADE)),
  848. ("name", models.CharField(max_length=100)),
  849. ],
  850. )
  851. publisher_with_book = ModelState(
  852. "testapp",
  853. "Publisher",
  854. [
  855. ("id", models.AutoField(primary_key=True)),
  856. ("author", models.ForeignKey("otherapp.Book", models.CASCADE)),
  857. ("name", models.CharField(max_length=100)),
  858. ],
  859. )
  860. other_pony = ModelState(
  861. "otherapp",
  862. "Pony",
  863. [
  864. ("id", models.AutoField(primary_key=True)),
  865. ],
  866. )
  867. other_pony_food = ModelState(
  868. "otherapp",
  869. "Pony",
  870. [
  871. ("id", models.AutoField(primary_key=True)),
  872. ],
  873. managers=[
  874. ("food_qs", FoodQuerySet.as_manager()),
  875. ("food_mgr", FoodManager("a", "b")),
  876. ("food_mgr_kwargs", FoodManager("x", "y", 3, 4)),
  877. ],
  878. )
  879. other_stable = ModelState(
  880. "otherapp", "Stable", [("id", models.AutoField(primary_key=True))]
  881. )
  882. third_thing = ModelState(
  883. "thirdapp", "Thing", [("id", models.AutoField(primary_key=True))]
  884. )
  885. book = ModelState(
  886. "otherapp",
  887. "Book",
  888. [
  889. ("id", models.AutoField(primary_key=True)),
  890. ("author", models.ForeignKey("testapp.Author", models.CASCADE)),
  891. ("title", models.CharField(max_length=200)),
  892. ],
  893. )
  894. book_proxy_fk = ModelState(
  895. "otherapp",
  896. "Book",
  897. [
  898. ("id", models.AutoField(primary_key=True)),
  899. ("author", models.ForeignKey("thirdapp.AuthorProxy", models.CASCADE)),
  900. ("title", models.CharField(max_length=200)),
  901. ],
  902. )
  903. book_proxy_proxy_fk = ModelState(
  904. "otherapp",
  905. "Book",
  906. [
  907. ("id", models.AutoField(primary_key=True)),
  908. ("author", models.ForeignKey("testapp.AAuthorProxyProxy", models.CASCADE)),
  909. ],
  910. )
  911. book_migrations_fk = ModelState(
  912. "otherapp",
  913. "Book",
  914. [
  915. ("id", models.AutoField(primary_key=True)),
  916. ("author", models.ForeignKey("migrations.UnmigratedModel", models.CASCADE)),
  917. ("title", models.CharField(max_length=200)),
  918. ],
  919. )
  920. book_with_no_author_fk = ModelState(
  921. "otherapp",
  922. "Book",
  923. [
  924. ("id", models.AutoField(primary_key=True)),
  925. ("author", models.IntegerField()),
  926. ("title", models.CharField(max_length=200)),
  927. ],
  928. )
  929. book_with_no_author = ModelState(
  930. "otherapp",
  931. "Book",
  932. [
  933. ("id", models.AutoField(primary_key=True)),
  934. ("title", models.CharField(max_length=200)),
  935. ],
  936. )
  937. book_with_author_renamed = ModelState(
  938. "otherapp",
  939. "Book",
  940. [
  941. ("id", models.AutoField(primary_key=True)),
  942. ("author", models.ForeignKey("testapp.Writer", models.CASCADE)),
  943. ("title", models.CharField(max_length=200)),
  944. ],
  945. )
  946. book_with_field_and_author_renamed = ModelState(
  947. "otherapp",
  948. "Book",
  949. [
  950. ("id", models.AutoField(primary_key=True)),
  951. ("writer", models.ForeignKey("testapp.Writer", models.CASCADE)),
  952. ("title", models.CharField(max_length=200)),
  953. ],
  954. )
  955. book_with_multiple_authors = ModelState(
  956. "otherapp",
  957. "Book",
  958. [
  959. ("id", models.AutoField(primary_key=True)),
  960. ("authors", models.ManyToManyField("testapp.Author")),
  961. ("title", models.CharField(max_length=200)),
  962. ],
  963. )
  964. book_with_multiple_authors_through_attribution = ModelState(
  965. "otherapp",
  966. "Book",
  967. [
  968. ("id", models.AutoField(primary_key=True)),
  969. (
  970. "authors",
  971. models.ManyToManyField(
  972. "testapp.Author", through="otherapp.Attribution"
  973. ),
  974. ),
  975. ("title", models.CharField(max_length=200)),
  976. ],
  977. )
  978. book_indexes = ModelState(
  979. "otherapp",
  980. "Book",
  981. [
  982. ("id", models.AutoField(primary_key=True)),
  983. ("author", models.ForeignKey("testapp.Author", models.CASCADE)),
  984. ("title", models.CharField(max_length=200)),
  985. ],
  986. {
  987. "indexes": [
  988. models.Index(fields=["author", "title"], name="book_title_author_idx")
  989. ],
  990. },
  991. )
  992. book_unordered_indexes = ModelState(
  993. "otherapp",
  994. "Book",
  995. [
  996. ("id", models.AutoField(primary_key=True)),
  997. ("author", models.ForeignKey("testapp.Author", models.CASCADE)),
  998. ("title", models.CharField(max_length=200)),
  999. ],
  1000. {
  1001. "indexes": [
  1002. models.Index(fields=["title", "author"], name="book_author_title_idx")
  1003. ],
  1004. },
  1005. )
  1006. book_unique_together = ModelState(
  1007. "otherapp",
  1008. "Book",
  1009. [
  1010. ("id", models.AutoField(primary_key=True)),
  1011. ("author", models.ForeignKey("testapp.Author", models.CASCADE)),
  1012. ("title", models.CharField(max_length=200)),
  1013. ],
  1014. {
  1015. "unique_together": {("author", "title")},
  1016. },
  1017. )
  1018. book_unique_together_2 = ModelState(
  1019. "otherapp",
  1020. "Book",
  1021. [
  1022. ("id", models.AutoField(primary_key=True)),
  1023. ("author", models.ForeignKey("testapp.Author", models.CASCADE)),
  1024. ("title", models.CharField(max_length=200)),
  1025. ],
  1026. {
  1027. "unique_together": {("title", "author")},
  1028. },
  1029. )
  1030. book_unique_together_3 = ModelState(
  1031. "otherapp",
  1032. "Book",
  1033. [
  1034. ("id", models.AutoField(primary_key=True)),
  1035. ("newfield", models.IntegerField()),
  1036. ("author", models.ForeignKey("testapp.Author", models.CASCADE)),
  1037. ("title", models.CharField(max_length=200)),
  1038. ],
  1039. {
  1040. "unique_together": {("title", "newfield")},
  1041. },
  1042. )
  1043. book_unique_together_4 = ModelState(
  1044. "otherapp",
  1045. "Book",
  1046. [
  1047. ("id", models.AutoField(primary_key=True)),
  1048. ("newfield2", models.IntegerField()),
  1049. ("author", models.ForeignKey("testapp.Author", models.CASCADE)),
  1050. ("title", models.CharField(max_length=200)),
  1051. ],
  1052. {
  1053. "unique_together": {("title", "newfield2")},
  1054. },
  1055. )
  1056. attribution = ModelState(
  1057. "otherapp",
  1058. "Attribution",
  1059. [
  1060. ("id", models.AutoField(primary_key=True)),
  1061. ("author", models.ForeignKey("testapp.Author", models.CASCADE)),
  1062. ("book", models.ForeignKey("otherapp.Book", models.CASCADE)),
  1063. ],
  1064. )
  1065. edition = ModelState(
  1066. "thirdapp",
  1067. "Edition",
  1068. [
  1069. ("id", models.AutoField(primary_key=True)),
  1070. ("book", models.ForeignKey("otherapp.Book", models.CASCADE)),
  1071. ],
  1072. )
  1073. custom_user = ModelState(
  1074. "thirdapp",
  1075. "CustomUser",
  1076. [
  1077. ("id", models.AutoField(primary_key=True)),
  1078. ("username", models.CharField(max_length=255)),
  1079. ],
  1080. bases=(AbstractBaseUser,),
  1081. )
  1082. custom_user_no_inherit = ModelState(
  1083. "thirdapp",
  1084. "CustomUser",
  1085. [
  1086. ("id", models.AutoField(primary_key=True)),
  1087. ("username", models.CharField(max_length=255)),
  1088. ],
  1089. )
  1090. aardvark = ModelState(
  1091. "thirdapp", "Aardvark", [("id", models.AutoField(primary_key=True))]
  1092. )
  1093. aardvark_testapp = ModelState(
  1094. "testapp", "Aardvark", [("id", models.AutoField(primary_key=True))]
  1095. )
  1096. aardvark_based_on_author = ModelState(
  1097. "testapp", "Aardvark", [], bases=("testapp.Author",)
  1098. )
  1099. aardvark_pk_fk_author = ModelState(
  1100. "testapp",
  1101. "Aardvark",
  1102. [
  1103. (
  1104. "id",
  1105. models.OneToOneField(
  1106. "testapp.Author", models.CASCADE, primary_key=True
  1107. ),
  1108. ),
  1109. ],
  1110. )
  1111. knight = ModelState("eggs", "Knight", [("id", models.AutoField(primary_key=True))])
  1112. rabbit = ModelState(
  1113. "eggs",
  1114. "Rabbit",
  1115. [
  1116. ("id", models.AutoField(primary_key=True)),
  1117. ("knight", models.ForeignKey("eggs.Knight", models.CASCADE)),
  1118. ("parent", models.ForeignKey("eggs.Rabbit", models.CASCADE)),
  1119. ],
  1120. {
  1121. "unique_together": {("parent", "knight")},
  1122. "indexes": [
  1123. models.Index(
  1124. fields=["parent", "knight"], name="rabbit_circular_fk_index"
  1125. )
  1126. ],
  1127. },
  1128. )
  1129. def test_arrange_for_graph(self):
  1130. """Tests auto-naming of migrations for graph matching."""
  1131. # Make a fake graph
  1132. graph = MigrationGraph()
  1133. graph.add_node(("testapp", "0001_initial"), None)
  1134. graph.add_node(("testapp", "0002_foobar"), None)
  1135. graph.add_node(("otherapp", "0001_initial"), None)
  1136. graph.add_dependency(
  1137. "testapp.0002_foobar",
  1138. ("testapp", "0002_foobar"),
  1139. ("testapp", "0001_initial"),
  1140. )
  1141. graph.add_dependency(
  1142. "testapp.0002_foobar",
  1143. ("testapp", "0002_foobar"),
  1144. ("otherapp", "0001_initial"),
  1145. )
  1146. # Use project state to make a new migration change set
  1147. before = self.make_project_state([self.publisher, self.other_pony])
  1148. after = self.make_project_state(
  1149. [
  1150. self.author_empty,
  1151. self.publisher,
  1152. self.other_pony,
  1153. self.other_stable,
  1154. ]
  1155. )
  1156. autodetector = MigrationAutodetector(before, after)
  1157. changes = autodetector._detect_changes()
  1158. # Run through arrange_for_graph
  1159. changes = autodetector.arrange_for_graph(changes, graph)
  1160. # Make sure there's a new name, deps match, etc.
  1161. self.assertEqual(changes["testapp"][0].name, "0003_author")
  1162. self.assertEqual(
  1163. changes["testapp"][0].dependencies, [("testapp", "0002_foobar")]
  1164. )
  1165. self.assertEqual(changes["otherapp"][0].name, "0002_stable")
  1166. self.assertEqual(
  1167. changes["otherapp"][0].dependencies, [("otherapp", "0001_initial")]
  1168. )
  1169. def test_arrange_for_graph_with_multiple_initial(self):
  1170. # Make a fake graph.
  1171. graph = MigrationGraph()
  1172. # Use project state to make a new migration change set.
  1173. before = self.make_project_state([])
  1174. after = self.make_project_state(
  1175. [self.author_with_book, self.book, self.attribution]
  1176. )
  1177. autodetector = MigrationAutodetector(
  1178. before, after, MigrationQuestioner({"ask_initial": True})
  1179. )
  1180. changes = autodetector._detect_changes()
  1181. changes = autodetector.arrange_for_graph(changes, graph)
  1182. self.assertEqual(changes["otherapp"][0].name, "0001_initial")
  1183. self.assertEqual(changes["otherapp"][0].dependencies, [])
  1184. self.assertEqual(changes["otherapp"][1].name, "0002_initial")
  1185. self.assertCountEqual(
  1186. changes["otherapp"][1].dependencies,
  1187. [("testapp", "0001_initial"), ("otherapp", "0001_initial")],
  1188. )
  1189. self.assertEqual(changes["testapp"][0].name, "0001_initial")
  1190. self.assertEqual(
  1191. changes["testapp"][0].dependencies, [("otherapp", "0001_initial")]
  1192. )
  1193. def test_trim_apps(self):
  1194. """
  1195. Trim does not remove dependencies but does remove unwanted apps.
  1196. """
  1197. # Use project state to make a new migration change set
  1198. before = self.make_project_state([])
  1199. after = self.make_project_state(
  1200. [self.author_empty, self.other_pony, self.other_stable, self.third_thing]
  1201. )
  1202. autodetector = MigrationAutodetector(
  1203. before, after, MigrationQuestioner({"ask_initial": True})
  1204. )
  1205. changes = autodetector._detect_changes()
  1206. # Run through arrange_for_graph
  1207. graph = MigrationGraph()
  1208. changes = autodetector.arrange_for_graph(changes, graph)
  1209. changes["testapp"][0].dependencies.append(("otherapp", "0001_initial"))
  1210. changes = autodetector._trim_to_apps(changes, {"testapp"})
  1211. # Make sure there's the right set of migrations
  1212. self.assertEqual(changes["testapp"][0].name, "0001_initial")
  1213. self.assertEqual(changes["otherapp"][0].name, "0001_initial")
  1214. self.assertNotIn("thirdapp", changes)
  1215. def test_custom_migration_name(self):
  1216. """Tests custom naming of migrations for graph matching."""
  1217. # Make a fake graph
  1218. graph = MigrationGraph()
  1219. graph.add_node(("testapp", "0001_initial"), None)
  1220. graph.add_node(("testapp", "0002_foobar"), None)
  1221. graph.add_node(("otherapp", "0001_initial"), None)
  1222. graph.add_dependency(
  1223. "testapp.0002_foobar",
  1224. ("testapp", "0002_foobar"),
  1225. ("testapp", "0001_initial"),
  1226. )
  1227. # Use project state to make a new migration change set
  1228. before = self.make_project_state([])
  1229. after = self.make_project_state(
  1230. [self.author_empty, self.other_pony, self.other_stable]
  1231. )
  1232. autodetector = MigrationAutodetector(before, after)
  1233. changes = autodetector._detect_changes()
  1234. # Run through arrange_for_graph
  1235. migration_name = "custom_name"
  1236. changes = autodetector.arrange_for_graph(changes, graph, migration_name)
  1237. # Make sure there's a new name, deps match, etc.
  1238. self.assertEqual(changes["testapp"][0].name, "0003_%s" % migration_name)
  1239. self.assertEqual(
  1240. changes["testapp"][0].dependencies, [("testapp", "0002_foobar")]
  1241. )
  1242. self.assertEqual(changes["otherapp"][0].name, "0002_%s" % migration_name)
  1243. self.assertEqual(
  1244. changes["otherapp"][0].dependencies, [("otherapp", "0001_initial")]
  1245. )
  1246. def test_new_model(self):
  1247. """Tests autodetection of new models."""
  1248. changes = self.get_changes([], [self.other_pony_food])
  1249. # Right number/type of migrations?
  1250. self.assertNumberMigrations(changes, "otherapp", 1)
  1251. self.assertOperationTypes(changes, "otherapp", 0, ["CreateModel"])
  1252. self.assertOperationAttributes(changes, "otherapp", 0, 0, name="Pony")
  1253. self.assertEqual(
  1254. [name for name, mgr in changes["otherapp"][0].operations[0].managers],
  1255. ["food_qs", "food_mgr", "food_mgr_kwargs"],
  1256. )
  1257. def test_old_model(self):
  1258. """Tests deletion of old models."""
  1259. changes = self.get_changes([self.author_empty], [])
  1260. # Right number/type of migrations?
  1261. self.assertNumberMigrations(changes, "testapp", 1)
  1262. self.assertOperationTypes(changes, "testapp", 0, ["DeleteModel"])
  1263. self.assertOperationAttributes(changes, "testapp", 0, 0, name="Author")
  1264. def test_add_field(self):
  1265. """Tests autodetection of new fields."""
  1266. changes = self.get_changes([self.author_empty], [self.author_name])
  1267. # Right number/type of migrations?
  1268. self.assertNumberMigrations(changes, "testapp", 1)
  1269. self.assertOperationTypes(changes, "testapp", 0, ["AddField"])
  1270. self.assertOperationAttributes(changes, "testapp", 0, 0, name="name")
  1271. @mock.patch(
  1272. "django.db.migrations.questioner.MigrationQuestioner.ask_not_null_addition",
  1273. side_effect=AssertionError("Should not have prompted for not null addition"),
  1274. )
  1275. def test_add_not_null_field_with_db_default(self, mocked_ask_method):
  1276. changes = self.get_changes([self.author_empty], [self.author_name_db_default])
  1277. self.assertNumberMigrations(changes, "testapp", 1)
  1278. self.assertOperationTypes(changes, "testapp", 0, ["AddField"])
  1279. self.assertOperationAttributes(
  1280. changes, "testapp", 0, 0, name="name", preserve_default=True
  1281. )
  1282. self.assertOperationFieldAttributes(
  1283. changes, "testapp", 0, 0, db_default="Ada Lovelace"
  1284. )
  1285. @mock.patch(
  1286. "django.db.migrations.questioner.MigrationQuestioner.ask_not_null_addition",
  1287. side_effect=AssertionError("Should not have prompted for not null addition"),
  1288. )
  1289. def test_add_date_fields_with_auto_now_not_asking_for_default(
  1290. self, mocked_ask_method
  1291. ):
  1292. changes = self.get_changes(
  1293. [self.author_empty], [self.author_dates_of_birth_auto_now]
  1294. )
  1295. # Right number/type of migrations?
  1296. self.assertNumberMigrations(changes, "testapp", 1)
  1297. self.assertOperationTypes(
  1298. changes, "testapp", 0, ["AddField", "AddField", "AddField"]
  1299. )
  1300. self.assertOperationFieldAttributes(changes, "testapp", 0, 0, auto_now=True)
  1301. self.assertOperationFieldAttributes(changes, "testapp", 0, 1, auto_now=True)
  1302. self.assertOperationFieldAttributes(changes, "testapp", 0, 2, auto_now=True)
  1303. @mock.patch(
  1304. "django.db.migrations.questioner.MigrationQuestioner.ask_not_null_addition",
  1305. side_effect=AssertionError("Should not have prompted for not null addition"),
  1306. )
  1307. def test_add_date_fields_with_auto_now_add_not_asking_for_null_addition(
  1308. self, mocked_ask_method
  1309. ):
  1310. changes = self.get_changes(
  1311. [self.author_empty], [self.author_dates_of_birth_auto_now_add]
  1312. )
  1313. # Right number/type of migrations?
  1314. self.assertNumberMigrations(changes, "testapp", 1)
  1315. self.assertOperationTypes(
  1316. changes, "testapp", 0, ["AddField", "AddField", "AddField"]
  1317. )
  1318. self.assertOperationFieldAttributes(changes, "testapp", 0, 0, auto_now_add=True)
  1319. self.assertOperationFieldAttributes(changes, "testapp", 0, 1, auto_now_add=True)
  1320. self.assertOperationFieldAttributes(changes, "testapp", 0, 2, auto_now_add=True)
  1321. @mock.patch(
  1322. "django.db.migrations.questioner.MigrationQuestioner.ask_auto_now_add_addition"
  1323. )
  1324. def test_add_date_fields_with_auto_now_add_asking_for_default(
  1325. self, mocked_ask_method
  1326. ):
  1327. changes = self.get_changes(
  1328. [self.author_empty], [self.author_dates_of_birth_auto_now_add]
  1329. )
  1330. # Right number/type of migrations?
  1331. self.assertNumberMigrations(changes, "testapp", 1)
  1332. self.assertOperationTypes(
  1333. changes, "testapp", 0, ["AddField", "AddField", "AddField"]
  1334. )
  1335. self.assertOperationFieldAttributes(changes, "testapp", 0, 0, auto_now_add=True)
  1336. self.assertOperationFieldAttributes(changes, "testapp", 0, 1, auto_now_add=True)
  1337. self.assertOperationFieldAttributes(changes, "testapp", 0, 2, auto_now_add=True)
  1338. self.assertEqual(mocked_ask_method.call_count, 3)
  1339. def test_add_field_before_generated_field(self):
  1340. initial_state = ModelState(
  1341. "testapp",
  1342. "Author",
  1343. [
  1344. ("name", models.CharField(max_length=20)),
  1345. ],
  1346. )
  1347. updated_state = ModelState(
  1348. "testapp",
  1349. "Author",
  1350. [
  1351. ("name", models.CharField(max_length=20)),
  1352. ("surname", models.CharField(max_length=20)),
  1353. (
  1354. "lower_full_name",
  1355. models.GeneratedField(
  1356. expression=Concat(Lower("name"), Lower("surname")),
  1357. output_field=models.CharField(max_length=30),
  1358. db_persist=True,
  1359. ),
  1360. ),
  1361. ],
  1362. )
  1363. changes = self.get_changes([initial_state], [updated_state])
  1364. self.assertNumberMigrations(changes, "testapp", 1)
  1365. self.assertOperationTypes(changes, "testapp", 0, ["AddField", "AddField"])
  1366. self.assertOperationFieldAttributes(
  1367. changes, "testapp", 0, 1, expression=Concat(Lower("name"), Lower("surname"))
  1368. )
  1369. def test_add_fk_before_generated_field(self):
  1370. initial_state = ModelState(
  1371. "testapp",
  1372. "Author",
  1373. [
  1374. ("name", models.CharField(max_length=20)),
  1375. ],
  1376. )
  1377. updated_state = [
  1378. ModelState(
  1379. "testapp",
  1380. "Publisher",
  1381. [
  1382. ("name", models.CharField(max_length=20)),
  1383. ],
  1384. ),
  1385. ModelState(
  1386. "testapp",
  1387. "Author",
  1388. [
  1389. ("name", models.CharField(max_length=20)),
  1390. (
  1391. "publisher",
  1392. models.ForeignKey("testapp.Publisher", models.CASCADE),
  1393. ),
  1394. (
  1395. "lower_full_name",
  1396. models.GeneratedField(
  1397. expression=Concat("name", "publisher_id"),
  1398. output_field=models.CharField(max_length=20),
  1399. db_persist=True,
  1400. ),
  1401. ),
  1402. ],
  1403. ),
  1404. ]
  1405. changes = self.get_changes([initial_state], updated_state)
  1406. self.assertNumberMigrations(changes, "testapp", 1)
  1407. self.assertOperationTypes(
  1408. changes, "testapp", 0, ["CreateModel", "AddField", "AddField"]
  1409. )
  1410. self.assertOperationFieldAttributes(
  1411. changes, "testapp", 0, 2, expression=Concat("name", "publisher_id")
  1412. )
  1413. def test_remove_field(self):
  1414. """Tests autodetection of removed fields."""
  1415. changes = self.get_changes([self.author_name], [self.author_empty])
  1416. # Right number/type of migrations?
  1417. self.assertNumberMigrations(changes, "testapp", 1)
  1418. self.assertOperationTypes(changes, "testapp", 0, ["RemoveField"])
  1419. self.assertOperationAttributes(changes, "testapp", 0, 0, name="name")
  1420. def test_alter_field(self):
  1421. """Tests autodetection of new fields."""
  1422. changes = self.get_changes([self.author_name], [self.author_name_longer])
  1423. # Right number/type of migrations?
  1424. self.assertNumberMigrations(changes, "testapp", 1)
  1425. self.assertOperationTypes(changes, "testapp", 0, ["AlterField"])
  1426. self.assertOperationAttributes(
  1427. changes, "testapp", 0, 0, name="name", preserve_default=True
  1428. )
  1429. def test_supports_functools_partial(self):
  1430. def _content_file_name(instance, filename, key, **kwargs):
  1431. return "{}/{}".format(instance, filename)
  1432. def content_file_name(key, **kwargs):
  1433. return functools.partial(_content_file_name, key, **kwargs)
  1434. # An unchanged partial reference.
  1435. before = [
  1436. ModelState(
  1437. "testapp",
  1438. "Author",
  1439. [
  1440. ("id", models.AutoField(primary_key=True)),
  1441. (
  1442. "file",
  1443. models.FileField(
  1444. max_length=200, upload_to=content_file_name("file")
  1445. ),
  1446. ),
  1447. ],
  1448. )
  1449. ]
  1450. after = [
  1451. ModelState(
  1452. "testapp",
  1453. "Author",
  1454. [
  1455. ("id", models.AutoField(primary_key=True)),
  1456. (
  1457. "file",
  1458. models.FileField(
  1459. max_length=200, upload_to=content_file_name("file")
  1460. ),
  1461. ),
  1462. ],
  1463. )
  1464. ]
  1465. changes = self.get_changes(before, after)
  1466. self.assertNumberMigrations(changes, "testapp", 0)
  1467. # A changed partial reference.
  1468. args_changed = [
  1469. ModelState(
  1470. "testapp",
  1471. "Author",
  1472. [
  1473. ("id", models.AutoField(primary_key=True)),
  1474. (
  1475. "file",
  1476. models.FileField(
  1477. max_length=200, upload_to=content_file_name("other-file")
  1478. ),
  1479. ),
  1480. ],
  1481. )
  1482. ]
  1483. changes = self.get_changes(before, args_changed)
  1484. self.assertNumberMigrations(changes, "testapp", 1)
  1485. self.assertOperationTypes(changes, "testapp", 0, ["AlterField"])
  1486. # Can't use assertOperationFieldAttributes because we need the
  1487. # deconstructed version, i.e., the exploded func/args/keywords rather
  1488. # than the partial: we don't care if it's not the same instance of the
  1489. # partial, only if it's the same source function, args, and keywords.
  1490. value = changes["testapp"][0].operations[0].field.upload_to
  1491. self.assertEqual(
  1492. (_content_file_name, ("other-file",), {}),
  1493. (value.func, value.args, value.keywords),
  1494. )
  1495. kwargs_changed = [
  1496. ModelState(
  1497. "testapp",
  1498. "Author",
  1499. [
  1500. ("id", models.AutoField(primary_key=True)),
  1501. (
  1502. "file",
  1503. models.FileField(
  1504. max_length=200,
  1505. upload_to=content_file_name("file", spam="eggs"),
  1506. ),
  1507. ),
  1508. ],
  1509. )
  1510. ]
  1511. changes = self.get_changes(before, kwargs_changed)
  1512. self.assertNumberMigrations(changes, "testapp", 1)
  1513. self.assertOperationTypes(changes, "testapp", 0, ["AlterField"])
  1514. value = changes["testapp"][0].operations[0].field.upload_to
  1515. self.assertEqual(
  1516. (_content_file_name, ("file",), {"spam": "eggs"}),
  1517. (value.func, value.args, value.keywords),
  1518. )
  1519. @mock.patch(
  1520. "django.db.migrations.questioner.MigrationQuestioner.ask_not_null_alteration",
  1521. side_effect=AssertionError("Should not have prompted for not null addition"),
  1522. )
  1523. def test_alter_field_to_not_null_with_default(self, mocked_ask_method):
  1524. """
  1525. #23609 - Tests autodetection of nullable to non-nullable alterations.
  1526. """
  1527. changes = self.get_changes([self.author_name_null], [self.author_name_default])
  1528. # Right number/type of migrations?
  1529. self.assertNumberMigrations(changes, "testapp", 1)
  1530. self.assertOperationTypes(changes, "testapp", 0, ["AlterField"])
  1531. self.assertOperationAttributes(
  1532. changes, "testapp", 0, 0, name="name", preserve_default=True
  1533. )
  1534. self.assertOperationFieldAttributes(
  1535. changes, "testapp", 0, 0, default="Ada Lovelace"
  1536. )
  1537. @mock.patch(
  1538. "django.db.migrations.questioner.MigrationQuestioner.ask_not_null_alteration",
  1539. side_effect=AssertionError("Should not have prompted for not null alteration"),
  1540. )
  1541. def test_alter_field_to_not_null_with_db_default(self, mocked_ask_method):
  1542. changes = self.get_changes(
  1543. [self.author_name_null], [self.author_name_db_default]
  1544. )
  1545. self.assertNumberMigrations(changes, "testapp", 1)
  1546. self.assertOperationTypes(changes, "testapp", 0, ["AlterField"])
  1547. self.assertOperationAttributes(
  1548. changes, "testapp", 0, 0, name="name", preserve_default=True
  1549. )
  1550. self.assertOperationFieldAttributes(
  1551. changes, "testapp", 0, 0, db_default="Ada Lovelace"
  1552. )
  1553. @mock.patch(
  1554. "django.db.migrations.questioner.MigrationQuestioner.ask_not_null_addition"
  1555. )
  1556. def test_add_auto_field_does_not_request_default(self, mocked_ask_method):
  1557. initial_state = ModelState(
  1558. "testapp",
  1559. "Author",
  1560. [
  1561. ("pkfield", models.IntegerField(primary_key=True)),
  1562. ],
  1563. )
  1564. for auto_field in [
  1565. models.AutoField,
  1566. models.BigAutoField,
  1567. models.SmallAutoField,
  1568. ]:
  1569. with self.subTest(auto_field=auto_field):
  1570. updated_state = ModelState(
  1571. "testapp",
  1572. "Author",
  1573. [
  1574. ("id", auto_field(primary_key=True)),
  1575. ("pkfield", models.IntegerField(primary_key=False)),
  1576. ],
  1577. )
  1578. self.get_changes([initial_state], [updated_state])
  1579. mocked_ask_method.assert_not_called()
  1580. @mock.patch(
  1581. "django.db.migrations.questioner.MigrationQuestioner.ask_not_null_alteration",
  1582. return_value=models.NOT_PROVIDED,
  1583. )
  1584. def test_alter_field_to_not_null_without_default(self, mocked_ask_method):
  1585. """
  1586. #23609 - Tests autodetection of nullable to non-nullable alterations.
  1587. """
  1588. changes = self.get_changes([self.author_name_null], [self.author_name])
  1589. self.assertEqual(mocked_ask_method.call_count, 1)
  1590. # Right number/type of migrations?
  1591. self.assertNumberMigrations(changes, "testapp", 1)
  1592. self.assertOperationTypes(changes, "testapp", 0, ["AlterField"])
  1593. self.assertOperationAttributes(
  1594. changes, "testapp", 0, 0, name="name", preserve_default=True
  1595. )
  1596. self.assertOperationFieldAttributes(
  1597. changes, "testapp", 0, 0, default=models.NOT_PROVIDED
  1598. )
  1599. @mock.patch(
  1600. "django.db.migrations.questioner.MigrationQuestioner.ask_not_null_alteration",
  1601. return_value="Some Name",
  1602. )
  1603. def test_alter_field_to_not_null_oneoff_default(self, mocked_ask_method):
  1604. """
  1605. #23609 - Tests autodetection of nullable to non-nullable alterations.
  1606. """
  1607. changes = self.get_changes([self.author_name_null], [self.author_name])
  1608. self.assertEqual(mocked_ask_method.call_count, 1)
  1609. # Right number/type of migrations?
  1610. self.assertNumberMigrations(changes, "testapp", 1)
  1611. self.assertOperationTypes(changes, "testapp", 0, ["AlterField"])
  1612. self.assertOperationAttributes(
  1613. changes, "testapp", 0, 0, name="name", preserve_default=False
  1614. )
  1615. self.assertOperationFieldAttributes(
  1616. changes, "testapp", 0, 0, default="Some Name"
  1617. )
  1618. def test_rename_field(self):
  1619. """Tests autodetection of renamed fields."""
  1620. changes = self.get_changes(
  1621. [self.author_name],
  1622. [self.author_name_renamed],
  1623. MigrationQuestioner({"ask_rename": True}),
  1624. )
  1625. # Right number/type of migrations?
  1626. self.assertNumberMigrations(changes, "testapp", 1)
  1627. self.assertOperationTypes(changes, "testapp", 0, ["RenameField"])
  1628. self.assertOperationAttributes(
  1629. changes, "testapp", 0, 0, old_name="name", new_name="names"
  1630. )
  1631. def test_rename_field_foreign_key_to_field(self):
  1632. before = [
  1633. ModelState(
  1634. "app",
  1635. "Foo",
  1636. [
  1637. ("id", models.AutoField(primary_key=True)),
  1638. ("field", models.IntegerField(unique=True)),
  1639. ],
  1640. ),
  1641. ModelState(
  1642. "app",
  1643. "Bar",
  1644. [
  1645. ("id", models.AutoField(primary_key=True)),
  1646. (
  1647. "foo",
  1648. models.ForeignKey("app.Foo", models.CASCADE, to_field="field"),
  1649. ),
  1650. ],
  1651. ),
  1652. ]
  1653. after = [
  1654. ModelState(
  1655. "app",
  1656. "Foo",
  1657. [
  1658. ("id", models.AutoField(primary_key=True)),
  1659. ("renamed_field", models.IntegerField(unique=True)),
  1660. ],
  1661. ),
  1662. ModelState(
  1663. "app",
  1664. "Bar",
  1665. [
  1666. ("id", models.AutoField(primary_key=True)),
  1667. (
  1668. "foo",
  1669. models.ForeignKey(
  1670. "app.Foo", models.CASCADE, to_field="renamed_field"
  1671. ),
  1672. ),
  1673. ],
  1674. ),
  1675. ]
  1676. changes = self.get_changes(
  1677. before, after, MigrationQuestioner({"ask_rename": True})
  1678. )
  1679. # Right number/type of migrations?
  1680. self.assertNumberMigrations(changes, "app", 1)
  1681. self.assertOperationTypes(changes, "app", 0, ["RenameField"])
  1682. self.assertOperationAttributes(
  1683. changes, "app", 0, 0, old_name="field", new_name="renamed_field"
  1684. )
  1685. def test_foreign_object_from_to_fields_list(self):
  1686. author_state = ModelState(
  1687. "app",
  1688. "Author",
  1689. [("id", models.AutoField(primary_key=True))],
  1690. )
  1691. book_state = ModelState(
  1692. "app",
  1693. "Book",
  1694. [
  1695. ("id", models.AutoField(primary_key=True)),
  1696. ("name", models.CharField()),
  1697. ("author_id", models.IntegerField()),
  1698. (
  1699. "author",
  1700. models.ForeignObject(
  1701. "app.Author",
  1702. models.CASCADE,
  1703. from_fields=["author_id"],
  1704. to_fields=["id"],
  1705. ),
  1706. ),
  1707. ],
  1708. )
  1709. book_state_copy = copy.deepcopy(book_state)
  1710. changes = self.get_changes(
  1711. [author_state, book_state],
  1712. [author_state, book_state_copy],
  1713. )
  1714. self.assertEqual(changes, {})
  1715. def test_rename_foreign_object_fields(self):
  1716. fields = ("first", "second")
  1717. renamed_fields = ("first_renamed", "second_renamed")
  1718. before = [
  1719. ModelState(
  1720. "app",
  1721. "Foo",
  1722. [
  1723. ("id", models.AutoField(primary_key=True)),
  1724. ("first", models.IntegerField()),
  1725. ("second", models.IntegerField()),
  1726. ],
  1727. options={"unique_together": {fields}},
  1728. ),
  1729. ModelState(
  1730. "app",
  1731. "Bar",
  1732. [
  1733. ("id", models.AutoField(primary_key=True)),
  1734. ("first", models.IntegerField()),
  1735. ("second", models.IntegerField()),
  1736. (
  1737. "foo",
  1738. models.ForeignObject(
  1739. "app.Foo",
  1740. models.CASCADE,
  1741. from_fields=fields,
  1742. to_fields=fields,
  1743. ),
  1744. ),
  1745. ],
  1746. ),
  1747. ]
  1748. # Case 1: to_fields renames.
  1749. after = [
  1750. ModelState(
  1751. "app",
  1752. "Foo",
  1753. [
  1754. ("id", models.AutoField(primary_key=True)),
  1755. ("first_renamed", models.IntegerField()),
  1756. ("second_renamed", models.IntegerField()),
  1757. ],
  1758. options={"unique_together": {renamed_fields}},
  1759. ),
  1760. ModelState(
  1761. "app",
  1762. "Bar",
  1763. [
  1764. ("id", models.AutoField(primary_key=True)),
  1765. ("first", models.IntegerField()),
  1766. ("second", models.IntegerField()),
  1767. (
  1768. "foo",
  1769. models.ForeignObject(
  1770. "app.Foo",
  1771. models.CASCADE,
  1772. from_fields=fields,
  1773. to_fields=renamed_fields,
  1774. ),
  1775. ),
  1776. ],
  1777. ),
  1778. ]
  1779. changes = self.get_changes(
  1780. before, after, MigrationQuestioner({"ask_rename": True})
  1781. )
  1782. self.assertNumberMigrations(changes, "app", 1)
  1783. self.assertOperationTypes(
  1784. changes, "app", 0, ["RenameField", "RenameField", "AlterUniqueTogether"]
  1785. )
  1786. self.assertOperationAttributes(
  1787. changes,
  1788. "app",
  1789. 0,
  1790. 0,
  1791. model_name="foo",
  1792. old_name="first",
  1793. new_name="first_renamed",
  1794. )
  1795. self.assertOperationAttributes(
  1796. changes,
  1797. "app",
  1798. 0,
  1799. 1,
  1800. model_name="foo",
  1801. old_name="second",
  1802. new_name="second_renamed",
  1803. )
  1804. # Case 2: from_fields renames.
  1805. after = [
  1806. ModelState(
  1807. "app",
  1808. "Foo",
  1809. [
  1810. ("id", models.AutoField(primary_key=True)),
  1811. ("first", models.IntegerField()),
  1812. ("second", models.IntegerField()),
  1813. ],
  1814. options={"unique_together": {fields}},
  1815. ),
  1816. ModelState(
  1817. "app",
  1818. "Bar",
  1819. [
  1820. ("id", models.AutoField(primary_key=True)),
  1821. ("first_renamed", models.IntegerField()),
  1822. ("second_renamed", models.IntegerField()),
  1823. (
  1824. "foo",
  1825. models.ForeignObject(
  1826. "app.Foo",
  1827. models.CASCADE,
  1828. from_fields=renamed_fields,
  1829. to_fields=fields,
  1830. ),
  1831. ),
  1832. ],
  1833. ),
  1834. ]
  1835. changes = self.get_changes(
  1836. before, after, MigrationQuestioner({"ask_rename": True})
  1837. )
  1838. self.assertNumberMigrations(changes, "app", 1)
  1839. self.assertOperationTypes(changes, "app", 0, ["RenameField", "RenameField"])
  1840. self.assertOperationAttributes(
  1841. changes,
  1842. "app",
  1843. 0,
  1844. 0,
  1845. model_name="bar",
  1846. old_name="first",
  1847. new_name="first_renamed",
  1848. )
  1849. self.assertOperationAttributes(
  1850. changes,
  1851. "app",
  1852. 0,
  1853. 1,
  1854. model_name="bar",
  1855. old_name="second",
  1856. new_name="second_renamed",
  1857. )
  1858. def test_rename_referenced_primary_key(self):
  1859. before = [
  1860. ModelState(
  1861. "app",
  1862. "Foo",
  1863. [
  1864. ("id", models.CharField(primary_key=True, serialize=False)),
  1865. ],
  1866. ),
  1867. ModelState(
  1868. "app",
  1869. "Bar",
  1870. [
  1871. ("id", models.AutoField(primary_key=True)),
  1872. ("foo", models.ForeignKey("app.Foo", models.CASCADE)),
  1873. ],
  1874. ),
  1875. ]
  1876. after = [
  1877. ModelState(
  1878. "app",
  1879. "Foo",
  1880. [("renamed_id", models.CharField(primary_key=True, serialize=False))],
  1881. ),
  1882. ModelState(
  1883. "app",
  1884. "Bar",
  1885. [
  1886. ("id", models.AutoField(primary_key=True)),
  1887. ("foo", models.ForeignKey("app.Foo", models.CASCADE)),
  1888. ],
  1889. ),
  1890. ]
  1891. changes = self.get_changes(
  1892. before, after, MigrationQuestioner({"ask_rename": True})
  1893. )
  1894. self.assertNumberMigrations(changes, "app", 1)
  1895. self.assertOperationTypes(changes, "app", 0, ["RenameField"])
  1896. self.assertOperationAttributes(
  1897. changes, "app", 0, 0, old_name="id", new_name="renamed_id"
  1898. )
  1899. def test_rename_field_preserved_db_column(self):
  1900. """
  1901. RenameField is used if a field is renamed and db_column equal to the
  1902. old field's column is added.
  1903. """
  1904. before = [
  1905. ModelState(
  1906. "app",
  1907. "Foo",
  1908. [
  1909. ("id", models.AutoField(primary_key=True)),
  1910. ("field", models.IntegerField()),
  1911. ],
  1912. ),
  1913. ]
  1914. after = [
  1915. ModelState(
  1916. "app",
  1917. "Foo",
  1918. [
  1919. ("id", models.AutoField(primary_key=True)),
  1920. ("renamed_field", models.IntegerField(db_column="field")),
  1921. ],
  1922. ),
  1923. ]
  1924. changes = self.get_changes(
  1925. before, after, MigrationQuestioner({"ask_rename": True})
  1926. )
  1927. self.assertNumberMigrations(changes, "app", 1)
  1928. self.assertOperationTypes(changes, "app", 0, ["AlterField", "RenameField"])
  1929. self.assertOperationAttributes(
  1930. changes,
  1931. "app",
  1932. 0,
  1933. 0,
  1934. model_name="foo",
  1935. name="field",
  1936. )
  1937. self.assertEqual(
  1938. changes["app"][0].operations[0].field.deconstruct(),
  1939. (
  1940. "field",
  1941. "django.db.models.IntegerField",
  1942. [],
  1943. {"db_column": "field"},
  1944. ),
  1945. )
  1946. self.assertOperationAttributes(
  1947. changes,
  1948. "app",
  1949. 0,
  1950. 1,
  1951. model_name="foo",
  1952. old_name="field",
  1953. new_name="renamed_field",
  1954. )
  1955. def test_rename_related_field_preserved_db_column(self):
  1956. before = [
  1957. ModelState(
  1958. "app",
  1959. "Foo",
  1960. [
  1961. ("id", models.AutoField(primary_key=True)),
  1962. ],
  1963. ),
  1964. ModelState(
  1965. "app",
  1966. "Bar",
  1967. [
  1968. ("id", models.AutoField(primary_key=True)),
  1969. ("foo", models.ForeignKey("app.Foo", models.CASCADE)),
  1970. ],
  1971. ),
  1972. ]
  1973. after = [
  1974. ModelState(
  1975. "app",
  1976. "Foo",
  1977. [
  1978. ("id", models.AutoField(primary_key=True)),
  1979. ],
  1980. ),
  1981. ModelState(
  1982. "app",
  1983. "Bar",
  1984. [
  1985. ("id", models.AutoField(primary_key=True)),
  1986. (
  1987. "renamed_foo",
  1988. models.ForeignKey(
  1989. "app.Foo", models.CASCADE, db_column="foo_id"
  1990. ),
  1991. ),
  1992. ],
  1993. ),
  1994. ]
  1995. changes = self.get_changes(
  1996. before, after, MigrationQuestioner({"ask_rename": True})
  1997. )
  1998. self.assertNumberMigrations(changes, "app", 1)
  1999. self.assertOperationTypes(changes, "app", 0, ["AlterField", "RenameField"])
  2000. self.assertOperationAttributes(
  2001. changes,
  2002. "app",
  2003. 0,
  2004. 0,
  2005. model_name="bar",
  2006. name="foo",
  2007. )
  2008. self.assertEqual(
  2009. changes["app"][0].operations[0].field.deconstruct(),
  2010. (
  2011. "foo",
  2012. "django.db.models.ForeignKey",
  2013. [],
  2014. {"to": "app.foo", "on_delete": models.CASCADE, "db_column": "foo_id"},
  2015. ),
  2016. )
  2017. self.assertOperationAttributes(
  2018. changes,
  2019. "app",
  2020. 0,
  2021. 1,
  2022. model_name="bar",
  2023. old_name="foo",
  2024. new_name="renamed_foo",
  2025. )
  2026. def test_rename_field_with_renamed_model(self):
  2027. changes = self.get_changes(
  2028. [self.author_name],
  2029. [
  2030. ModelState(
  2031. "testapp",
  2032. "RenamedAuthor",
  2033. [
  2034. ("id", models.AutoField(primary_key=True)),
  2035. ("renamed_name", models.CharField(max_length=200)),
  2036. ],
  2037. ),
  2038. ],
  2039. MigrationQuestioner({"ask_rename_model": True, "ask_rename": True}),
  2040. )
  2041. self.assertNumberMigrations(changes, "testapp", 1)
  2042. self.assertOperationTypes(changes, "testapp", 0, ["RenameModel", "RenameField"])
  2043. self.assertOperationAttributes(
  2044. changes,
  2045. "testapp",
  2046. 0,
  2047. 0,
  2048. old_name="Author",
  2049. new_name="RenamedAuthor",
  2050. )
  2051. self.assertOperationAttributes(
  2052. changes,
  2053. "testapp",
  2054. 0,
  2055. 1,
  2056. old_name="name",
  2057. new_name="renamed_name",
  2058. )
  2059. def test_rename_model(self):
  2060. """Tests autodetection of renamed models."""
  2061. changes = self.get_changes(
  2062. [self.author_with_book, self.book],
  2063. [self.author_renamed_with_book, self.book_with_author_renamed],
  2064. MigrationQuestioner({"ask_rename_model": True}),
  2065. )
  2066. # Right number/type of migrations?
  2067. self.assertNumberMigrations(changes, "testapp", 1)
  2068. self.assertOperationTypes(changes, "testapp", 0, ["RenameModel"])
  2069. self.assertOperationAttributes(
  2070. changes, "testapp", 0, 0, old_name="Author", new_name="Writer"
  2071. )
  2072. # Now that RenameModel handles related fields too, there should be
  2073. # no AlterField for the related field.
  2074. self.assertNumberMigrations(changes, "otherapp", 0)
  2075. def test_rename_model_case(self):
  2076. """
  2077. Model name is case-insensitive. Changing case doesn't lead to any
  2078. autodetected operations.
  2079. """
  2080. author_renamed = ModelState(
  2081. "testapp",
  2082. "author",
  2083. [
  2084. ("id", models.AutoField(primary_key=True)),
  2085. ],
  2086. )
  2087. changes = self.get_changes(
  2088. [self.author_empty, self.book],
  2089. [author_renamed, self.book],
  2090. questioner=MigrationQuestioner({"ask_rename_model": True}),
  2091. )
  2092. self.assertNumberMigrations(changes, "testapp", 0)
  2093. self.assertNumberMigrations(changes, "otherapp", 0)
  2094. def test_renamed_referenced_m2m_model_case(self):
  2095. publisher_renamed = ModelState(
  2096. "testapp",
  2097. "publisher",
  2098. [
  2099. ("id", models.AutoField(primary_key=True)),
  2100. ("name", models.CharField(max_length=100)),
  2101. ],
  2102. )
  2103. changes = self.get_changes(
  2104. [self.publisher, self.author_with_m2m],
  2105. [publisher_renamed, self.author_with_m2m],
  2106. questioner=MigrationQuestioner({"ask_rename_model": True}),
  2107. )
  2108. self.assertNumberMigrations(changes, "testapp", 0)
  2109. self.assertNumberMigrations(changes, "otherapp", 0)
  2110. def test_rename_m2m_through_model(self):
  2111. """
  2112. Tests autodetection of renamed models that are used in M2M relations as
  2113. through models.
  2114. """
  2115. changes = self.get_changes(
  2116. [self.author_with_m2m_through, self.publisher, self.contract],
  2117. [
  2118. self.author_with_renamed_m2m_through,
  2119. self.publisher,
  2120. self.contract_renamed,
  2121. ],
  2122. MigrationQuestioner({"ask_rename_model": True}),
  2123. )
  2124. # Right number/type of migrations?
  2125. self.assertNumberMigrations(changes, "testapp", 1)
  2126. self.assertOperationTypes(changes, "testapp", 0, ["RenameModel"])
  2127. self.assertOperationAttributes(
  2128. changes, "testapp", 0, 0, old_name="Contract", new_name="Deal"
  2129. )
  2130. def test_rename_model_with_renamed_rel_field(self):
  2131. """
  2132. Tests autodetection of renamed models while simultaneously renaming one
  2133. of the fields that relate to the renamed model.
  2134. """
  2135. changes = self.get_changes(
  2136. [self.author_with_book, self.book],
  2137. [self.author_renamed_with_book, self.book_with_field_and_author_renamed],
  2138. MigrationQuestioner({"ask_rename": True, "ask_rename_model": True}),
  2139. )
  2140. # Right number/type of migrations?
  2141. self.assertNumberMigrations(changes, "testapp", 1)
  2142. self.assertOperationTypes(changes, "testapp", 0, ["RenameModel"])
  2143. self.assertOperationAttributes(
  2144. changes, "testapp", 0, 0, old_name="Author", new_name="Writer"
  2145. )
  2146. # Right number/type of migrations for related field rename?
  2147. # Alter is already taken care of.
  2148. self.assertNumberMigrations(changes, "otherapp", 1)
  2149. self.assertOperationTypes(changes, "otherapp", 0, ["RenameField"])
  2150. self.assertOperationAttributes(
  2151. changes, "otherapp", 0, 0, old_name="author", new_name="writer"
  2152. )
  2153. def test_rename_model_with_fks_in_different_position(self):
  2154. """
  2155. #24537 - The order of fields in a model does not influence
  2156. the RenameModel detection.
  2157. """
  2158. before = [
  2159. ModelState(
  2160. "testapp",
  2161. "EntityA",
  2162. [
  2163. ("id", models.AutoField(primary_key=True)),
  2164. ],
  2165. ),
  2166. ModelState(
  2167. "testapp",
  2168. "EntityB",
  2169. [
  2170. ("id", models.AutoField(primary_key=True)),
  2171. ("some_label", models.CharField(max_length=255)),
  2172. ("entity_a", models.ForeignKey("testapp.EntityA", models.CASCADE)),
  2173. ],
  2174. ),
  2175. ]
  2176. after = [
  2177. ModelState(
  2178. "testapp",
  2179. "EntityA",
  2180. [
  2181. ("id", models.AutoField(primary_key=True)),
  2182. ],
  2183. ),
  2184. ModelState(
  2185. "testapp",
  2186. "RenamedEntityB",
  2187. [
  2188. ("id", models.AutoField(primary_key=True)),
  2189. ("entity_a", models.ForeignKey("testapp.EntityA", models.CASCADE)),
  2190. ("some_label", models.CharField(max_length=255)),
  2191. ],
  2192. ),
  2193. ]
  2194. changes = self.get_changes(
  2195. before, after, MigrationQuestioner({"ask_rename_model": True})
  2196. )
  2197. self.assertNumberMigrations(changes, "testapp", 1)
  2198. self.assertOperationTypes(changes, "testapp", 0, ["RenameModel"])
  2199. self.assertOperationAttributes(
  2200. changes, "testapp", 0, 0, old_name="EntityB", new_name="RenamedEntityB"
  2201. )
  2202. def test_rename_model_reverse_relation_dependencies(self):
  2203. """
  2204. The migration to rename a model pointed to by a foreign key in another
  2205. app must run after the other app's migration that adds the foreign key
  2206. with model's original name. Therefore, the renaming migration has a
  2207. dependency on that other migration.
  2208. """
  2209. before = [
  2210. ModelState(
  2211. "testapp",
  2212. "EntityA",
  2213. [
  2214. ("id", models.AutoField(primary_key=True)),
  2215. ],
  2216. ),
  2217. ModelState(
  2218. "otherapp",
  2219. "EntityB",
  2220. [
  2221. ("id", models.AutoField(primary_key=True)),
  2222. ("entity_a", models.ForeignKey("testapp.EntityA", models.CASCADE)),
  2223. ],
  2224. ),
  2225. ]
  2226. after = [
  2227. ModelState(
  2228. "testapp",
  2229. "RenamedEntityA",
  2230. [
  2231. ("id", models.AutoField(primary_key=True)),
  2232. ],
  2233. ),
  2234. ModelState(
  2235. "otherapp",
  2236. "EntityB",
  2237. [
  2238. ("id", models.AutoField(primary_key=True)),
  2239. (
  2240. "entity_a",
  2241. models.ForeignKey("testapp.RenamedEntityA", models.CASCADE),
  2242. ),
  2243. ],
  2244. ),
  2245. ]
  2246. changes = self.get_changes(
  2247. before, after, MigrationQuestioner({"ask_rename_model": True})
  2248. )
  2249. self.assertNumberMigrations(changes, "testapp", 1)
  2250. self.assertMigrationDependencies(
  2251. changes, "testapp", 0, [("otherapp", "__first__")]
  2252. )
  2253. self.assertOperationTypes(changes, "testapp", 0, ["RenameModel"])
  2254. self.assertOperationAttributes(
  2255. changes, "testapp", 0, 0, old_name="EntityA", new_name="RenamedEntityA"
  2256. )
  2257. def test_fk_dependency(self):
  2258. """Having a ForeignKey automatically adds a dependency."""
  2259. # Note that testapp (author) has no dependencies,
  2260. # otherapp (book) depends on testapp (author),
  2261. # thirdapp (edition) depends on otherapp (book)
  2262. changes = self.get_changes([], [self.author_name, self.book, self.edition])
  2263. # Right number/type of migrations?
  2264. self.assertNumberMigrations(changes, "testapp", 1)
  2265. self.assertOperationTypes(changes, "testapp", 0, ["CreateModel"])
  2266. self.assertOperationAttributes(changes, "testapp", 0, 0, name="Author")
  2267. self.assertMigrationDependencies(changes, "testapp", 0, [])
  2268. # Right number/type of migrations?
  2269. self.assertNumberMigrations(changes, "otherapp", 1)
  2270. self.assertOperationTypes(changes, "otherapp", 0, ["CreateModel"])
  2271. self.assertOperationAttributes(changes, "otherapp", 0, 0, name="Book")
  2272. self.assertMigrationDependencies(
  2273. changes, "otherapp", 0, [("testapp", "auto_1")]
  2274. )
  2275. # Right number/type of migrations?
  2276. self.assertNumberMigrations(changes, "thirdapp", 1)
  2277. self.assertOperationTypes(changes, "thirdapp", 0, ["CreateModel"])
  2278. self.assertOperationAttributes(changes, "thirdapp", 0, 0, name="Edition")
  2279. self.assertMigrationDependencies(
  2280. changes, "thirdapp", 0, [("otherapp", "auto_1")]
  2281. )
  2282. def test_proxy_fk_dependency(self):
  2283. """FK dependencies still work on proxy models."""
  2284. # Note that testapp (author) has no dependencies,
  2285. # otherapp (book) depends on testapp (authorproxy)
  2286. changes = self.get_changes(
  2287. [], [self.author_empty, self.author_proxy_third, self.book_proxy_fk]
  2288. )
  2289. # Right number/type of migrations?
  2290. self.assertNumberMigrations(changes, "testapp", 1)
  2291. self.assertOperationTypes(changes, "testapp", 0, ["CreateModel"])
  2292. self.assertOperationAttributes(changes, "testapp", 0, 0, name="Author")
  2293. self.assertMigrationDependencies(changes, "testapp", 0, [])
  2294. # Right number/type of migrations?
  2295. self.assertNumberMigrations(changes, "otherapp", 1)
  2296. self.assertOperationTypes(changes, "otherapp", 0, ["CreateModel"])
  2297. self.assertOperationAttributes(changes, "otherapp", 0, 0, name="Book")
  2298. self.assertMigrationDependencies(
  2299. changes, "otherapp", 0, [("thirdapp", "auto_1")]
  2300. )
  2301. # Right number/type of migrations?
  2302. self.assertNumberMigrations(changes, "thirdapp", 1)
  2303. self.assertOperationTypes(changes, "thirdapp", 0, ["CreateModel"])
  2304. self.assertOperationAttributes(changes, "thirdapp", 0, 0, name="AuthorProxy")
  2305. self.assertMigrationDependencies(
  2306. changes, "thirdapp", 0, [("testapp", "auto_1")]
  2307. )
  2308. def test_same_app_no_fk_dependency(self):
  2309. """
  2310. A migration with a FK between two models of the same app
  2311. does not have a dependency to itself.
  2312. """
  2313. changes = self.get_changes([], [self.author_with_publisher, self.publisher])
  2314. # Right number/type of migrations?
  2315. self.assertNumberMigrations(changes, "testapp", 1)
  2316. self.assertOperationTypes(changes, "testapp", 0, ["CreateModel", "CreateModel"])
  2317. self.assertOperationAttributes(changes, "testapp", 0, 0, name="Publisher")
  2318. self.assertOperationAttributes(changes, "testapp", 0, 1, name="Author")
  2319. self.assertMigrationDependencies(changes, "testapp", 0, [])
  2320. def test_circular_fk_dependency(self):
  2321. """
  2322. Having a circular ForeignKey dependency automatically
  2323. resolves the situation into 2 migrations on one side and 1 on the other.
  2324. """
  2325. changes = self.get_changes(
  2326. [], [self.author_with_book, self.book, self.publisher_with_book]
  2327. )
  2328. # Right number/type of migrations?
  2329. self.assertNumberMigrations(changes, "testapp", 1)
  2330. self.assertOperationTypes(changes, "testapp", 0, ["CreateModel", "CreateModel"])
  2331. self.assertOperationAttributes(changes, "testapp", 0, 0, name="Author")
  2332. self.assertOperationAttributes(changes, "testapp", 0, 1, name="Publisher")
  2333. self.assertMigrationDependencies(
  2334. changes, "testapp", 0, [("otherapp", "auto_1")]
  2335. )
  2336. # Right number/type of migrations?
  2337. self.assertNumberMigrations(changes, "otherapp", 2)
  2338. self.assertOperationTypes(changes, "otherapp", 0, ["CreateModel"])
  2339. self.assertOperationTypes(changes, "otherapp", 1, ["AddField"])
  2340. self.assertMigrationDependencies(changes, "otherapp", 0, [])
  2341. self.assertMigrationDependencies(
  2342. changes, "otherapp", 1, [("otherapp", "auto_1"), ("testapp", "auto_1")]
  2343. )
  2344. # both split migrations should be `initial`
  2345. self.assertTrue(changes["otherapp"][0].initial)
  2346. self.assertTrue(changes["otherapp"][1].initial)
  2347. def test_same_app_circular_fk_dependency(self):
  2348. """
  2349. A migration with a FK between two models of the same app does
  2350. not have a dependency to itself.
  2351. """
  2352. changes = self.get_changes(
  2353. [], [self.author_with_publisher, self.publisher_with_author]
  2354. )
  2355. # Right number/type of migrations?
  2356. self.assertNumberMigrations(changes, "testapp", 1)
  2357. self.assertOperationTypes(
  2358. changes, "testapp", 0, ["CreateModel", "CreateModel", "AddField"]
  2359. )
  2360. self.assertOperationAttributes(changes, "testapp", 0, 0, name="Author")
  2361. self.assertOperationAttributes(changes, "testapp", 0, 1, name="Publisher")
  2362. self.assertOperationAttributes(changes, "testapp", 0, 2, name="publisher")
  2363. self.assertMigrationDependencies(changes, "testapp", 0, [])
  2364. def test_same_app_circular_fk_dependency_with_unique_together_and_indexes(self):
  2365. """
  2366. #22275 - A migration with circular FK dependency does not try
  2367. to create unique together constraint and indexes before creating all
  2368. required fields first.
  2369. """
  2370. changes = self.get_changes([], [self.knight, self.rabbit])
  2371. # Right number/type of migrations?
  2372. self.assertNumberMigrations(changes, "eggs", 1)
  2373. self.assertOperationTypes(
  2374. changes,
  2375. "eggs",
  2376. 0,
  2377. ["CreateModel", "CreateModel"],
  2378. )
  2379. self.assertNotIn("unique_together", changes["eggs"][0].operations[0].options)
  2380. self.assertMigrationDependencies(changes, "eggs", 0, [])
  2381. def test_alter_db_table_add(self):
  2382. """Tests detection for adding db_table in model's options."""
  2383. changes = self.get_changes(
  2384. [self.author_empty], [self.author_with_db_table_options]
  2385. )
  2386. # Right number/type of migrations?
  2387. self.assertNumberMigrations(changes, "testapp", 1)
  2388. self.assertOperationTypes(changes, "testapp", 0, ["AlterModelTable"])
  2389. self.assertOperationAttributes(
  2390. changes, "testapp", 0, 0, name="author", table="author_one"
  2391. )
  2392. def test_alter_db_table_change(self):
  2393. """Tests detection for changing db_table in model's options'."""
  2394. changes = self.get_changes(
  2395. [self.author_with_db_table_options], [self.author_with_new_db_table_options]
  2396. )
  2397. # Right number/type of migrations?
  2398. self.assertNumberMigrations(changes, "testapp", 1)
  2399. self.assertOperationTypes(changes, "testapp", 0, ["AlterModelTable"])
  2400. self.assertOperationAttributes(
  2401. changes, "testapp", 0, 0, name="author", table="author_two"
  2402. )
  2403. def test_alter_db_table_remove(self):
  2404. """Tests detection for removing db_table in model's options."""
  2405. changes = self.get_changes(
  2406. [self.author_with_db_table_options], [self.author_empty]
  2407. )
  2408. # Right number/type of migrations?
  2409. self.assertNumberMigrations(changes, "testapp", 1)
  2410. self.assertOperationTypes(changes, "testapp", 0, ["AlterModelTable"])
  2411. self.assertOperationAttributes(
  2412. changes, "testapp", 0, 0, name="author", table=None
  2413. )
  2414. def test_alter_db_table_no_changes(self):
  2415. """
  2416. Alter_db_table doesn't generate a migration if no changes have been made.
  2417. """
  2418. changes = self.get_changes(
  2419. [self.author_with_db_table_options], [self.author_with_db_table_options]
  2420. )
  2421. # Right number of migrations?
  2422. self.assertEqual(len(changes), 0)
  2423. def test_keep_db_table_with_model_change(self):
  2424. """
  2425. Tests when model changes but db_table stays as-is, autodetector must not
  2426. create more than one operation.
  2427. """
  2428. changes = self.get_changes(
  2429. [self.author_with_db_table_options],
  2430. [self.author_renamed_with_db_table_options],
  2431. MigrationQuestioner({"ask_rename_model": True}),
  2432. )
  2433. # Right number/type of migrations?
  2434. self.assertNumberMigrations(changes, "testapp", 1)
  2435. self.assertOperationTypes(changes, "testapp", 0, ["RenameModel"])
  2436. self.assertOperationAttributes(
  2437. changes, "testapp", 0, 0, old_name="Author", new_name="NewAuthor"
  2438. )
  2439. def test_alter_db_table_with_model_change(self):
  2440. """
  2441. Tests when model and db_table changes, autodetector must create two
  2442. operations.
  2443. """
  2444. changes = self.get_changes(
  2445. [self.author_with_db_table_options],
  2446. [self.author_renamed_with_new_db_table_options],
  2447. MigrationQuestioner({"ask_rename_model": True}),
  2448. )
  2449. # Right number/type of migrations?
  2450. self.assertNumberMigrations(changes, "testapp", 1)
  2451. self.assertOperationTypes(
  2452. changes, "testapp", 0, ["RenameModel", "AlterModelTable"]
  2453. )
  2454. self.assertOperationAttributes(
  2455. changes, "testapp", 0, 0, old_name="Author", new_name="NewAuthor"
  2456. )
  2457. self.assertOperationAttributes(
  2458. changes, "testapp", 0, 1, name="newauthor", table="author_three"
  2459. )
  2460. def test_alter_db_table_comment_add(self):
  2461. changes = self.get_changes(
  2462. [self.author_empty], [self.author_with_db_table_comment]
  2463. )
  2464. self.assertNumberMigrations(changes, "testapp", 1)
  2465. self.assertOperationTypes(changes, "testapp", 0, ["AlterModelTableComment"])
  2466. self.assertOperationAttributes(
  2467. changes, "testapp", 0, 0, name="author", table_comment="Table comment"
  2468. )
  2469. def test_alter_db_table_comment_change(self):
  2470. author_with_new_db_table_comment = ModelState(
  2471. "testapp",
  2472. "Author",
  2473. [
  2474. ("id", models.AutoField(primary_key=True)),
  2475. ],
  2476. {"db_table_comment": "New table comment"},
  2477. )
  2478. changes = self.get_changes(
  2479. [self.author_with_db_table_comment],
  2480. [author_with_new_db_table_comment],
  2481. )
  2482. self.assertNumberMigrations(changes, "testapp", 1)
  2483. self.assertOperationTypes(changes, "testapp", 0, ["AlterModelTableComment"])
  2484. self.assertOperationAttributes(
  2485. changes,
  2486. "testapp",
  2487. 0,
  2488. 0,
  2489. name="author",
  2490. table_comment="New table comment",
  2491. )
  2492. def test_alter_db_table_comment_remove(self):
  2493. changes = self.get_changes(
  2494. [self.author_with_db_table_comment],
  2495. [self.author_empty],
  2496. )
  2497. self.assertNumberMigrations(changes, "testapp", 1)
  2498. self.assertOperationTypes(changes, "testapp", 0, ["AlterModelTableComment"])
  2499. self.assertOperationAttributes(
  2500. changes, "testapp", 0, 0, name="author", db_table_comment=None
  2501. )
  2502. def test_alter_db_table_comment_no_changes(self):
  2503. changes = self.get_changes(
  2504. [self.author_with_db_table_comment],
  2505. [self.author_with_db_table_comment],
  2506. )
  2507. self.assertNumberMigrations(changes, "testapp", 0)
  2508. def test_identical_regex_doesnt_alter(self):
  2509. from_state = ModelState(
  2510. "testapp",
  2511. "model",
  2512. [
  2513. (
  2514. "id",
  2515. models.AutoField(
  2516. primary_key=True,
  2517. validators=[
  2518. RegexValidator(
  2519. re.compile("^[-a-zA-Z0-9_]+\\Z"),
  2520. "Enter a valid “slug” consisting of letters, numbers, "
  2521. "underscores or hyphens.",
  2522. "invalid",
  2523. )
  2524. ],
  2525. ),
  2526. )
  2527. ],
  2528. )
  2529. to_state = ModelState(
  2530. "testapp",
  2531. "model",
  2532. [("id", models.AutoField(primary_key=True, validators=[validate_slug]))],
  2533. )
  2534. changes = self.get_changes([from_state], [to_state])
  2535. # Right number/type of migrations?
  2536. self.assertNumberMigrations(changes, "testapp", 0)
  2537. def test_different_regex_does_alter(self):
  2538. from_state = ModelState(
  2539. "testapp",
  2540. "model",
  2541. [
  2542. (
  2543. "id",
  2544. models.AutoField(
  2545. primary_key=True,
  2546. validators=[
  2547. RegexValidator(
  2548. re.compile("^[a-z]+\\Z", 32),
  2549. "Enter a valid “slug” consisting of letters, numbers, "
  2550. "underscores or hyphens.",
  2551. "invalid",
  2552. )
  2553. ],
  2554. ),
  2555. )
  2556. ],
  2557. )
  2558. to_state = ModelState(
  2559. "testapp",
  2560. "model",
  2561. [("id", models.AutoField(primary_key=True, validators=[validate_slug]))],
  2562. )
  2563. changes = self.get_changes([from_state], [to_state])
  2564. self.assertNumberMigrations(changes, "testapp", 1)
  2565. self.assertOperationTypes(changes, "testapp", 0, ["AlterField"])
  2566. def test_alter_regex_string_to_compiled_regex(self):
  2567. regex_string = "^[a-z]+$"
  2568. from_state = ModelState(
  2569. "testapp",
  2570. "model",
  2571. [
  2572. (
  2573. "id",
  2574. models.AutoField(
  2575. primary_key=True, validators=[RegexValidator(regex_string)]
  2576. ),
  2577. )
  2578. ],
  2579. )
  2580. to_state = ModelState(
  2581. "testapp",
  2582. "model",
  2583. [
  2584. (
  2585. "id",
  2586. models.AutoField(
  2587. primary_key=True,
  2588. validators=[RegexValidator(re.compile(regex_string))],
  2589. ),
  2590. )
  2591. ],
  2592. )
  2593. changes = self.get_changes([from_state], [to_state])
  2594. self.assertNumberMigrations(changes, "testapp", 1)
  2595. self.assertOperationTypes(changes, "testapp", 0, ["AlterField"])
  2596. def test_empty_unique_together(self):
  2597. """Empty unique_together shouldn't generate a migration."""
  2598. # Explicitly testing for not specified, since this is the case after
  2599. # a CreateModel operation w/o any definition on the original model
  2600. model_state_not_specified = ModelState(
  2601. "a", "model", [("id", models.AutoField(primary_key=True))]
  2602. )
  2603. # Explicitly testing for None, since this was the issue in #23452 after
  2604. # an AlterUniqueTogether operation with e.g. () as value
  2605. model_state_none = ModelState(
  2606. "a",
  2607. "model",
  2608. [("id", models.AutoField(primary_key=True))],
  2609. {
  2610. "unique_together": None,
  2611. },
  2612. )
  2613. # Explicitly testing for the empty set, since we now always have sets.
  2614. # During removal (('col1', 'col2'),) --> () this becomes set([])
  2615. model_state_empty = ModelState(
  2616. "a",
  2617. "model",
  2618. [("id", models.AutoField(primary_key=True))],
  2619. {
  2620. "unique_together": set(),
  2621. },
  2622. )
  2623. def test(from_state, to_state, msg):
  2624. changes = self.get_changes([from_state], [to_state])
  2625. if changes:
  2626. ops = ", ".join(
  2627. o.__class__.__name__ for o in changes["a"][0].operations
  2628. )
  2629. self.fail("Created operation(s) %s from %s" % (ops, msg))
  2630. tests = (
  2631. (
  2632. model_state_not_specified,
  2633. model_state_not_specified,
  2634. '"not specified" to "not specified"',
  2635. ),
  2636. (model_state_not_specified, model_state_none, '"not specified" to "None"'),
  2637. (
  2638. model_state_not_specified,
  2639. model_state_empty,
  2640. '"not specified" to "empty"',
  2641. ),
  2642. (model_state_none, model_state_not_specified, '"None" to "not specified"'),
  2643. (model_state_none, model_state_none, '"None" to "None"'),
  2644. (model_state_none, model_state_empty, '"None" to "empty"'),
  2645. (
  2646. model_state_empty,
  2647. model_state_not_specified,
  2648. '"empty" to "not specified"',
  2649. ),
  2650. (model_state_empty, model_state_none, '"empty" to "None"'),
  2651. (model_state_empty, model_state_empty, '"empty" to "empty"'),
  2652. )
  2653. for t in tests:
  2654. test(*t)
  2655. def test_create_model_with_indexes(self):
  2656. """Test creation of new model with indexes already defined."""
  2657. added_index = models.Index(
  2658. fields=["name"], name="create_model_with_indexes_idx"
  2659. )
  2660. author = ModelState(
  2661. "otherapp",
  2662. "Author",
  2663. [
  2664. ("id", models.AutoField(primary_key=True)),
  2665. ("name", models.CharField(max_length=200)),
  2666. ],
  2667. {
  2668. "indexes": [added_index],
  2669. },
  2670. )
  2671. changes = self.get_changes([], [author])
  2672. # Right number of migrations?
  2673. self.assertEqual(len(changes["otherapp"]), 1)
  2674. # Right number of actions?
  2675. migration = changes["otherapp"][0]
  2676. self.assertEqual(len(migration.operations), 1)
  2677. # Right actions order?
  2678. self.assertOperationTypes(changes, "otherapp", 0, ["CreateModel"])
  2679. self.assertOperationAttributes(changes, "otherapp", 0, 0, name="Author")
  2680. self.assertOperationAttributes(
  2681. changes,
  2682. "otherapp",
  2683. 0,
  2684. 0,
  2685. name="Author",
  2686. options={"indexes": [added_index]},
  2687. )
  2688. def test_add_indexes(self):
  2689. """Test change detection of new indexes."""
  2690. changes = self.get_changes(
  2691. [self.author_empty, self.book], [self.author_empty, self.book_indexes]
  2692. )
  2693. self.assertNumberMigrations(changes, "otherapp", 1)
  2694. self.assertOperationTypes(changes, "otherapp", 0, ["AddIndex"])
  2695. added_index = models.Index(
  2696. fields=["author", "title"], name="book_title_author_idx"
  2697. )
  2698. self.assertOperationAttributes(
  2699. changes, "otherapp", 0, 0, model_name="book", index=added_index
  2700. )
  2701. def test_remove_indexes(self):
  2702. """Test change detection of removed indexes."""
  2703. changes = self.get_changes(
  2704. [self.author_empty, self.book_indexes], [self.author_empty, self.book]
  2705. )
  2706. # Right number/type of migrations?
  2707. self.assertNumberMigrations(changes, "otherapp", 1)
  2708. self.assertOperationTypes(changes, "otherapp", 0, ["RemoveIndex"])
  2709. self.assertOperationAttributes(
  2710. changes, "otherapp", 0, 0, model_name="book", name="book_title_author_idx"
  2711. )
  2712. def test_rename_indexes(self):
  2713. book_renamed_indexes = ModelState(
  2714. "otherapp",
  2715. "Book",
  2716. [
  2717. ("id", models.AutoField(primary_key=True)),
  2718. ("author", models.ForeignKey("testapp.Author", models.CASCADE)),
  2719. ("title", models.CharField(max_length=200)),
  2720. ],
  2721. {
  2722. "indexes": [
  2723. models.Index(
  2724. fields=["author", "title"], name="renamed_book_title_author_idx"
  2725. )
  2726. ],
  2727. },
  2728. )
  2729. changes = self.get_changes(
  2730. [self.author_empty, self.book_indexes],
  2731. [self.author_empty, book_renamed_indexes],
  2732. )
  2733. self.assertNumberMigrations(changes, "otherapp", 1)
  2734. self.assertOperationTypes(changes, "otherapp", 0, ["RenameIndex"])
  2735. self.assertOperationAttributes(
  2736. changes,
  2737. "otherapp",
  2738. 0,
  2739. 0,
  2740. model_name="book",
  2741. new_name="renamed_book_title_author_idx",
  2742. old_name="book_title_author_idx",
  2743. )
  2744. def test_order_fields_indexes(self):
  2745. """Test change detection of reordering of fields in indexes."""
  2746. changes = self.get_changes(
  2747. [self.author_empty, self.book_indexes],
  2748. [self.author_empty, self.book_unordered_indexes],
  2749. )
  2750. self.assertNumberMigrations(changes, "otherapp", 1)
  2751. self.assertOperationTypes(changes, "otherapp", 0, ["RemoveIndex", "AddIndex"])
  2752. self.assertOperationAttributes(
  2753. changes, "otherapp", 0, 0, model_name="book", name="book_title_author_idx"
  2754. )
  2755. added_index = models.Index(
  2756. fields=["title", "author"], name="book_author_title_idx"
  2757. )
  2758. self.assertOperationAttributes(
  2759. changes, "otherapp", 0, 1, model_name="book", index=added_index
  2760. )
  2761. def test_create_model_with_check_constraint(self):
  2762. """Test creation of new model with constraints already defined."""
  2763. author = ModelState(
  2764. "otherapp",
  2765. "Author",
  2766. [
  2767. ("id", models.AutoField(primary_key=True)),
  2768. ("name", models.CharField(max_length=200)),
  2769. ],
  2770. {
  2771. "constraints": [
  2772. models.CheckConstraint(
  2773. condition=models.Q(name__contains="Bob"),
  2774. name="name_contains_bob",
  2775. )
  2776. ]
  2777. },
  2778. )
  2779. changes = self.get_changes([], [author])
  2780. constraint = models.CheckConstraint(
  2781. condition=models.Q(name__contains="Bob"), name="name_contains_bob"
  2782. )
  2783. # Right number of migrations?
  2784. self.assertEqual(len(changes["otherapp"]), 1)
  2785. # Right number of actions?
  2786. migration = changes["otherapp"][0]
  2787. self.assertEqual(len(migration.operations), 1)
  2788. # Right actions order?
  2789. self.assertOperationTypes(changes, "otherapp", 0, ["CreateModel"])
  2790. self.assertOperationAttributes(
  2791. changes,
  2792. "otherapp",
  2793. 0,
  2794. 0,
  2795. name="Author",
  2796. options={"constraints": [constraint]},
  2797. )
  2798. def test_add_constraints(self):
  2799. """Test change detection of new constraints."""
  2800. changes = self.get_changes(
  2801. [self.author_name], [self.author_name_check_constraint]
  2802. )
  2803. self.assertNumberMigrations(changes, "testapp", 1)
  2804. self.assertOperationTypes(changes, "testapp", 0, ["AddConstraint"])
  2805. added_constraint = models.CheckConstraint(
  2806. condition=models.Q(name__contains="Bob"), name="name_contains_bob"
  2807. )
  2808. self.assertOperationAttributes(
  2809. changes, "testapp", 0, 0, model_name="author", constraint=added_constraint
  2810. )
  2811. def test_add_constraints_with_new_model(self):
  2812. book_with_unique_title_and_pony = ModelState(
  2813. "otherapp",
  2814. "Book",
  2815. [
  2816. ("id", models.AutoField(primary_key=True)),
  2817. ("title", models.CharField(max_length=200)),
  2818. ("pony", models.ForeignKey("otherapp.Pony", models.CASCADE)),
  2819. ],
  2820. {
  2821. "constraints": [
  2822. models.UniqueConstraint(
  2823. fields=["title", "pony"],
  2824. name="unique_title_pony",
  2825. )
  2826. ]
  2827. },
  2828. )
  2829. changes = self.get_changes(
  2830. [self.book_with_no_author],
  2831. [book_with_unique_title_and_pony, self.other_pony],
  2832. )
  2833. self.assertNumberMigrations(changes, "otherapp", 1)
  2834. self.assertOperationTypes(
  2835. changes,
  2836. "otherapp",
  2837. 0,
  2838. ["CreateModel", "AddField", "AddConstraint"],
  2839. )
  2840. def test_add_constraints_with_dict_keys(self):
  2841. book_types = {"F": "Fantasy", "M": "Mystery"}
  2842. book_with_type = ModelState(
  2843. "testapp",
  2844. "Book",
  2845. [
  2846. ("id", models.AutoField(primary_key=True)),
  2847. ("type", models.CharField(max_length=1)),
  2848. ],
  2849. {
  2850. "constraints": [
  2851. models.CheckConstraint(
  2852. condition=models.Q(type__in=book_types.keys()),
  2853. name="book_type_check",
  2854. ),
  2855. ],
  2856. },
  2857. )
  2858. book_with_resolved_type = ModelState(
  2859. "testapp",
  2860. "Book",
  2861. [
  2862. ("id", models.AutoField(primary_key=True)),
  2863. ("type", models.CharField(max_length=1)),
  2864. ],
  2865. {
  2866. "constraints": [
  2867. models.CheckConstraint(
  2868. condition=models.Q(("type__in", tuple(book_types))),
  2869. name="book_type_check",
  2870. ),
  2871. ],
  2872. },
  2873. )
  2874. changes = self.get_changes([book_with_type], [book_with_resolved_type])
  2875. self.assertEqual(len(changes), 0)
  2876. def test_add_index_with_new_model(self):
  2877. book_with_index_title_and_pony = ModelState(
  2878. "otherapp",
  2879. "Book",
  2880. [
  2881. ("id", models.AutoField(primary_key=True)),
  2882. ("title", models.CharField(max_length=200)),
  2883. ("pony", models.ForeignKey("otherapp.Pony", models.CASCADE)),
  2884. ],
  2885. {
  2886. "indexes": [
  2887. models.Index(fields=["title", "pony"], name="index_title_pony"),
  2888. ]
  2889. },
  2890. )
  2891. changes = self.get_changes(
  2892. [self.book_with_no_author],
  2893. [book_with_index_title_and_pony, self.other_pony],
  2894. )
  2895. self.assertNumberMigrations(changes, "otherapp", 1)
  2896. self.assertOperationTypes(
  2897. changes,
  2898. "otherapp",
  2899. 0,
  2900. ["CreateModel", "AddField", "AddIndex"],
  2901. )
  2902. def test_alter_constraint(self):
  2903. book_constraint = models.CheckConstraint(
  2904. condition=models.Q(title__contains="title"),
  2905. name="title_contains_title",
  2906. )
  2907. book_altered_constraint = models.CheckConstraint(
  2908. condition=models.Q(title__contains="title"),
  2909. name="title_contains_title",
  2910. violation_error_code="error_code",
  2911. )
  2912. author_altered_constraint = models.CheckConstraint(
  2913. condition=models.Q(name__contains="Bob"),
  2914. name="name_contains_bob",
  2915. violation_error_message="Name doesn't contain Bob",
  2916. )
  2917. book_check_constraint = copy.deepcopy(self.book)
  2918. book_check_constraint_with_error_message = copy.deepcopy(self.book)
  2919. author_name_check_constraint_with_error_message = copy.deepcopy(
  2920. self.author_name_check_constraint
  2921. )
  2922. book_check_constraint.options = {"constraints": [book_constraint]}
  2923. book_check_constraint_with_error_message.options = {
  2924. "constraints": [book_altered_constraint]
  2925. }
  2926. author_name_check_constraint_with_error_message.options = {
  2927. "constraints": [author_altered_constraint]
  2928. }
  2929. changes = self.get_changes(
  2930. [self.author_name_check_constraint, book_check_constraint],
  2931. [
  2932. author_name_check_constraint_with_error_message,
  2933. book_check_constraint_with_error_message,
  2934. ],
  2935. )
  2936. self.assertNumberMigrations(changes, "testapp", 1)
  2937. self.assertOperationTypes(changes, "testapp", 0, ["AlterConstraint"])
  2938. self.assertOperationAttributes(
  2939. changes,
  2940. "testapp",
  2941. 0,
  2942. 0,
  2943. model_name="author",
  2944. name="name_contains_bob",
  2945. constraint=author_altered_constraint,
  2946. )
  2947. self.assertNumberMigrations(changes, "otherapp", 1)
  2948. self.assertOperationTypes(changes, "otherapp", 0, ["AlterConstraint"])
  2949. self.assertOperationAttributes(
  2950. changes,
  2951. "otherapp",
  2952. 0,
  2953. 0,
  2954. model_name="book",
  2955. name="title_contains_title",
  2956. constraint=book_altered_constraint,
  2957. )
  2958. self.assertMigrationDependencies(
  2959. changes, "otherapp", 0, [("testapp", "auto_1")]
  2960. )
  2961. def test_remove_constraints(self):
  2962. """Test change detection of removed constraints."""
  2963. changes = self.get_changes(
  2964. [self.author_name_check_constraint], [self.author_name]
  2965. )
  2966. # Right number/type of migrations?
  2967. self.assertNumberMigrations(changes, "testapp", 1)
  2968. self.assertOperationTypes(changes, "testapp", 0, ["RemoveConstraint"])
  2969. self.assertOperationAttributes(
  2970. changes, "testapp", 0, 0, model_name="author", name="name_contains_bob"
  2971. )
  2972. def test_constraint_dropped_and_recreated(self):
  2973. altered_constraint = models.CheckConstraint(
  2974. condition=models.Q(name__contains="bob"),
  2975. name="name_contains_bob",
  2976. )
  2977. author_name_check_constraint_lowercased = copy.deepcopy(
  2978. self.author_name_check_constraint
  2979. )
  2980. author_name_check_constraint_lowercased.options = {
  2981. "constraints": [altered_constraint]
  2982. }
  2983. changes = self.get_changes(
  2984. [self.author_name_check_constraint],
  2985. [author_name_check_constraint_lowercased],
  2986. )
  2987. self.assertNumberMigrations(changes, "testapp", 1)
  2988. self.assertOperationTypes(
  2989. changes, "testapp", 0, ["RemoveConstraint", "AddConstraint"]
  2990. )
  2991. self.assertOperationAttributes(
  2992. changes,
  2993. "testapp",
  2994. 0,
  2995. 0,
  2996. model_name="author",
  2997. name="name_contains_bob",
  2998. )
  2999. self.assertOperationAttributes(
  3000. changes,
  3001. "testapp",
  3002. 0,
  3003. 1,
  3004. model_name="author",
  3005. constraint=altered_constraint,
  3006. )
  3007. def test_add_unique_together(self):
  3008. """Tests unique_together detection."""
  3009. changes = self.get_changes(
  3010. [self.author_empty, self.book],
  3011. [self.author_empty, self.book_unique_together],
  3012. )
  3013. # Right number/type of migrations?
  3014. self.assertNumberMigrations(changes, "otherapp", 1)
  3015. self.assertOperationTypes(changes, "otherapp", 0, ["AlterUniqueTogether"])
  3016. self.assertOperationAttributes(
  3017. changes,
  3018. "otherapp",
  3019. 0,
  3020. 0,
  3021. name="book",
  3022. unique_together={("author", "title")},
  3023. )
  3024. def test_remove_unique_together(self):
  3025. """Tests unique_together detection."""
  3026. changes = self.get_changes(
  3027. [self.author_empty, self.book_unique_together],
  3028. [self.author_empty, self.book],
  3029. )
  3030. # Right number/type of migrations?
  3031. self.assertNumberMigrations(changes, "otherapp", 1)
  3032. self.assertOperationTypes(changes, "otherapp", 0, ["AlterUniqueTogether"])
  3033. self.assertOperationAttributes(
  3034. changes, "otherapp", 0, 0, name="book", unique_together=set()
  3035. )
  3036. def test_unique_together_remove_fk(self):
  3037. """Tests unique_together and field removal detection & ordering"""
  3038. changes = self.get_changes(
  3039. [self.author_empty, self.book_unique_together],
  3040. [self.author_empty, self.book_with_no_author],
  3041. )
  3042. # Right number/type of migrations?
  3043. self.assertNumberMigrations(changes, "otherapp", 1)
  3044. self.assertOperationTypes(
  3045. changes,
  3046. "otherapp",
  3047. 0,
  3048. ["AlterUniqueTogether", "RemoveField"],
  3049. )
  3050. self.assertOperationAttributes(
  3051. changes, "otherapp", 0, 0, name="book", unique_together=set()
  3052. )
  3053. self.assertOperationAttributes(
  3054. changes, "otherapp", 0, 1, model_name="book", name="author"
  3055. )
  3056. def test_unique_together_no_changes(self):
  3057. """
  3058. unique_together doesn't generate a migration if no
  3059. changes have been made.
  3060. """
  3061. changes = self.get_changes(
  3062. [self.author_empty, self.book_unique_together],
  3063. [self.author_empty, self.book_unique_together],
  3064. )
  3065. # Right number of migrations?
  3066. self.assertEqual(len(changes), 0)
  3067. def test_unique_together_ordering(self):
  3068. """
  3069. unique_together also triggers on ordering changes.
  3070. """
  3071. changes = self.get_changes(
  3072. [self.author_empty, self.book_unique_together],
  3073. [self.author_empty, self.book_unique_together_2],
  3074. )
  3075. # Right number/type of migrations?
  3076. self.assertNumberMigrations(changes, "otherapp", 1)
  3077. self.assertOperationTypes(
  3078. changes,
  3079. "otherapp",
  3080. 0,
  3081. ["AlterUniqueTogether"],
  3082. )
  3083. self.assertOperationAttributes(
  3084. changes,
  3085. "otherapp",
  3086. 0,
  3087. 0,
  3088. name="book",
  3089. unique_together={("title", "author")},
  3090. )
  3091. def test_add_field_and_unique_together(self):
  3092. """
  3093. Added fields will be created before using them in unique_together.
  3094. """
  3095. changes = self.get_changes(
  3096. [self.author_empty, self.book],
  3097. [self.author_empty, self.book_unique_together_3],
  3098. )
  3099. # Right number/type of migrations?
  3100. self.assertNumberMigrations(changes, "otherapp", 1)
  3101. self.assertOperationTypes(
  3102. changes,
  3103. "otherapp",
  3104. 0,
  3105. ["AddField", "AlterUniqueTogether"],
  3106. )
  3107. self.assertOperationAttributes(
  3108. changes,
  3109. "otherapp",
  3110. 0,
  3111. 1,
  3112. name="book",
  3113. unique_together={("title", "newfield")},
  3114. )
  3115. def test_create_model_and_unique_together(self):
  3116. author = ModelState(
  3117. "otherapp",
  3118. "Author",
  3119. [
  3120. ("id", models.AutoField(primary_key=True)),
  3121. ("name", models.CharField(max_length=200)),
  3122. ],
  3123. )
  3124. book_with_author = ModelState(
  3125. "otherapp",
  3126. "Book",
  3127. [
  3128. ("id", models.AutoField(primary_key=True)),
  3129. ("author", models.ForeignKey("otherapp.Author", models.CASCADE)),
  3130. ("title", models.CharField(max_length=200)),
  3131. ],
  3132. {
  3133. "unique_together": {("title", "author")},
  3134. },
  3135. )
  3136. changes = self.get_changes(
  3137. [self.book_with_no_author], [author, book_with_author]
  3138. )
  3139. # Right number of migrations?
  3140. self.assertEqual(len(changes["otherapp"]), 1)
  3141. # Right number of actions?
  3142. migration = changes["otherapp"][0]
  3143. self.assertEqual(len(migration.operations), 3)
  3144. # Right actions order?
  3145. self.assertOperationTypes(
  3146. changes,
  3147. "otherapp",
  3148. 0,
  3149. ["CreateModel", "AddField", "AlterUniqueTogether"],
  3150. )
  3151. def test_remove_field_and_unique_together(self):
  3152. """
  3153. Removed fields will be removed after updating unique_together.
  3154. """
  3155. changes = self.get_changes(
  3156. [self.author_empty, self.book_unique_together_3],
  3157. [self.author_empty, self.book_unique_together],
  3158. )
  3159. # Right number/type of migrations?
  3160. self.assertNumberMigrations(changes, "otherapp", 1)
  3161. self.assertOperationTypes(
  3162. changes,
  3163. "otherapp",
  3164. 0,
  3165. ["AlterUniqueTogether", "RemoveField"],
  3166. )
  3167. self.assertOperationAttributes(
  3168. changes,
  3169. "otherapp",
  3170. 0,
  3171. 0,
  3172. name="book",
  3173. unique_together={("author", "title")},
  3174. )
  3175. self.assertOperationAttributes(
  3176. changes,
  3177. "otherapp",
  3178. 0,
  3179. 1,
  3180. model_name="book",
  3181. name="newfield",
  3182. )
  3183. def test_alter_field_and_unique_together(self):
  3184. """Fields are altered after deleting some unique_together."""
  3185. initial_author = ModelState(
  3186. "testapp",
  3187. "Author",
  3188. [
  3189. ("id", models.AutoField(primary_key=True)),
  3190. ("name", models.CharField(max_length=200)),
  3191. ("age", models.IntegerField(db_index=True)),
  3192. ],
  3193. {
  3194. "unique_together": {("name",)},
  3195. },
  3196. )
  3197. author_reversed_constraints = ModelState(
  3198. "testapp",
  3199. "Author",
  3200. [
  3201. ("id", models.AutoField(primary_key=True)),
  3202. ("name", models.CharField(max_length=200, unique=True)),
  3203. ("age", models.IntegerField()),
  3204. ],
  3205. {
  3206. "unique_together": {("age",)},
  3207. },
  3208. )
  3209. changes = self.get_changes([initial_author], [author_reversed_constraints])
  3210. self.assertNumberMigrations(changes, "testapp", 1)
  3211. self.assertOperationTypes(
  3212. changes,
  3213. "testapp",
  3214. 0,
  3215. [
  3216. "AlterUniqueTogether",
  3217. "AlterField",
  3218. "AlterField",
  3219. "AlterUniqueTogether",
  3220. ],
  3221. )
  3222. self.assertOperationAttributes(
  3223. changes,
  3224. "testapp",
  3225. 0,
  3226. 0,
  3227. name="author",
  3228. unique_together=set(),
  3229. )
  3230. self.assertOperationAttributes(
  3231. changes,
  3232. "testapp",
  3233. 0,
  3234. 1,
  3235. model_name="author",
  3236. name="age",
  3237. )
  3238. self.assertOperationAttributes(
  3239. changes,
  3240. "testapp",
  3241. 0,
  3242. 2,
  3243. model_name="author",
  3244. name="name",
  3245. )
  3246. self.assertOperationAttributes(
  3247. changes,
  3248. "testapp",
  3249. 0,
  3250. 3,
  3251. name="author",
  3252. unique_together={("age",)},
  3253. )
  3254. def test_partly_alter_unique_together_increase(self):
  3255. initial_author = ModelState(
  3256. "testapp",
  3257. "Author",
  3258. [
  3259. ("id", models.AutoField(primary_key=True)),
  3260. ("name", models.CharField(max_length=200)),
  3261. ("age", models.IntegerField()),
  3262. ],
  3263. {
  3264. "unique_together": {("name",)},
  3265. },
  3266. )
  3267. author_new_constraints = ModelState(
  3268. "testapp",
  3269. "Author",
  3270. [
  3271. ("id", models.AutoField(primary_key=True)),
  3272. ("name", models.CharField(max_length=200)),
  3273. ("age", models.IntegerField()),
  3274. ],
  3275. {
  3276. "unique_together": {("name",), ("age",)},
  3277. },
  3278. )
  3279. changes = self.get_changes([initial_author], [author_new_constraints])
  3280. self.assertNumberMigrations(changes, "testapp", 1)
  3281. self.assertOperationTypes(
  3282. changes,
  3283. "testapp",
  3284. 0,
  3285. ["AlterUniqueTogether"],
  3286. )
  3287. self.assertOperationAttributes(
  3288. changes,
  3289. "testapp",
  3290. 0,
  3291. 0,
  3292. name="author",
  3293. unique_together={("name",), ("age",)},
  3294. )
  3295. def test_partly_alter_unique_together_decrease(self):
  3296. initial_author = ModelState(
  3297. "testapp",
  3298. "Author",
  3299. [
  3300. ("id", models.AutoField(primary_key=True)),
  3301. ("name", models.CharField(max_length=200)),
  3302. ("age", models.IntegerField()),
  3303. ],
  3304. {
  3305. "unique_together": {("name",), ("age",)},
  3306. },
  3307. )
  3308. author_new_constraints = ModelState(
  3309. "testapp",
  3310. "Author",
  3311. [
  3312. ("id", models.AutoField(primary_key=True)),
  3313. ("name", models.CharField(max_length=200)),
  3314. ("age", models.IntegerField()),
  3315. ],
  3316. {
  3317. "unique_together": {("name",)},
  3318. },
  3319. )
  3320. changes = self.get_changes([initial_author], [author_new_constraints])
  3321. self.assertNumberMigrations(changes, "testapp", 1)
  3322. self.assertOperationTypes(
  3323. changes,
  3324. "testapp",
  3325. 0,
  3326. ["AlterUniqueTogether"],
  3327. )
  3328. self.assertOperationAttributes(
  3329. changes,
  3330. "testapp",
  3331. 0,
  3332. 0,
  3333. name="author",
  3334. unique_together={("name",)},
  3335. )
  3336. def test_rename_field_and_unique_together(self):
  3337. """Fields are renamed before updating unique_together."""
  3338. changes = self.get_changes(
  3339. [self.author_empty, self.book_unique_together_3],
  3340. [self.author_empty, self.book_unique_together_4],
  3341. MigrationQuestioner({"ask_rename": True}),
  3342. )
  3343. # Right number/type of migrations?
  3344. self.assertNumberMigrations(changes, "otherapp", 1)
  3345. self.assertOperationTypes(
  3346. changes,
  3347. "otherapp",
  3348. 0,
  3349. ["RenameField", "AlterUniqueTogether"],
  3350. )
  3351. self.assertOperationAttributes(
  3352. changes,
  3353. "otherapp",
  3354. 0,
  3355. 1,
  3356. name="book",
  3357. unique_together={("title", "newfield2")},
  3358. )
  3359. def test_proxy(self):
  3360. """The autodetector correctly deals with proxy models."""
  3361. # First, we test adding a proxy model
  3362. changes = self.get_changes(
  3363. [self.author_empty], [self.author_empty, self.author_proxy]
  3364. )
  3365. # Right number/type of migrations?
  3366. self.assertNumberMigrations(changes, "testapp", 1)
  3367. self.assertOperationTypes(changes, "testapp", 0, ["CreateModel"])
  3368. self.assertOperationAttributes(
  3369. changes,
  3370. "testapp",
  3371. 0,
  3372. 0,
  3373. name="AuthorProxy",
  3374. options={"proxy": True, "indexes": [], "constraints": []},
  3375. )
  3376. # Now, we test turning a proxy model into a non-proxy model
  3377. # It should delete the proxy then make the real one
  3378. changes = self.get_changes(
  3379. [self.author_empty, self.author_proxy],
  3380. [self.author_empty, self.author_proxy_notproxy],
  3381. )
  3382. # Right number/type of migrations?
  3383. self.assertNumberMigrations(changes, "testapp", 1)
  3384. self.assertOperationTypes(changes, "testapp", 0, ["DeleteModel", "CreateModel"])
  3385. self.assertOperationAttributes(changes, "testapp", 0, 0, name="AuthorProxy")
  3386. self.assertOperationAttributes(
  3387. changes, "testapp", 0, 1, name="AuthorProxy", options={}
  3388. )
  3389. def test_proxy_non_model_parent(self):
  3390. class Mixin:
  3391. pass
  3392. author_proxy_non_model_parent = ModelState(
  3393. "testapp",
  3394. "AuthorProxy",
  3395. [],
  3396. {"proxy": True},
  3397. (Mixin, "testapp.author"),
  3398. )
  3399. changes = self.get_changes(
  3400. [self.author_empty],
  3401. [self.author_empty, author_proxy_non_model_parent],
  3402. )
  3403. self.assertNumberMigrations(changes, "testapp", 1)
  3404. self.assertOperationTypes(changes, "testapp", 0, ["CreateModel"])
  3405. self.assertOperationAttributes(
  3406. changes,
  3407. "testapp",
  3408. 0,
  3409. 0,
  3410. name="AuthorProxy",
  3411. options={"proxy": True, "indexes": [], "constraints": []},
  3412. bases=(Mixin, "testapp.author"),
  3413. )
  3414. def test_proxy_custom_pk(self):
  3415. """
  3416. #23415 - The autodetector must correctly deal with custom FK on proxy
  3417. models.
  3418. """
  3419. # First, we test the default pk field name
  3420. changes = self.get_changes(
  3421. [], [self.author_empty, self.author_proxy_third, self.book_proxy_fk]
  3422. )
  3423. # The model the FK is pointing from and to.
  3424. self.assertEqual(
  3425. changes["otherapp"][0].operations[0].fields[2][1].remote_field.model,
  3426. "thirdapp.AuthorProxy",
  3427. )
  3428. # Now, we test the custom pk field name
  3429. changes = self.get_changes(
  3430. [], [self.author_custom_pk, self.author_proxy_third, self.book_proxy_fk]
  3431. )
  3432. # The model the FK is pointing from and to.
  3433. self.assertEqual(
  3434. changes["otherapp"][0].operations[0].fields[2][1].remote_field.model,
  3435. "thirdapp.AuthorProxy",
  3436. )
  3437. def test_proxy_to_mti_with_fk_to_proxy(self):
  3438. # First, test the pk table and field name.
  3439. to_state = self.make_project_state(
  3440. [self.author_empty, self.author_proxy_third, self.book_proxy_fk],
  3441. )
  3442. changes = self.get_changes([], to_state)
  3443. fk_field = changes["otherapp"][0].operations[0].fields[2][1]
  3444. self.assertEqual(
  3445. to_state.get_concrete_model_key(fk_field.remote_field.model),
  3446. ("testapp", "author"),
  3447. )
  3448. self.assertEqual(fk_field.remote_field.model, "thirdapp.AuthorProxy")
  3449. # Change AuthorProxy to use MTI.
  3450. from_state = to_state.clone()
  3451. to_state = self.make_project_state(
  3452. [self.author_empty, self.author_proxy_third_notproxy, self.book_proxy_fk],
  3453. )
  3454. changes = self.get_changes(from_state, to_state)
  3455. # Right number/type of migrations for the AuthorProxy model?
  3456. self.assertNumberMigrations(changes, "thirdapp", 1)
  3457. self.assertOperationTypes(
  3458. changes, "thirdapp", 0, ["DeleteModel", "CreateModel"]
  3459. )
  3460. # Right number/type of migrations for the Book model with a FK to
  3461. # AuthorProxy?
  3462. self.assertNumberMigrations(changes, "otherapp", 1)
  3463. self.assertOperationTypes(changes, "otherapp", 0, ["AlterField"])
  3464. # otherapp should depend on thirdapp.
  3465. self.assertMigrationDependencies(
  3466. changes, "otherapp", 0, [("thirdapp", "auto_1")]
  3467. )
  3468. # Now, test the pk table and field name.
  3469. fk_field = changes["otherapp"][0].operations[0].field
  3470. self.assertEqual(
  3471. to_state.get_concrete_model_key(fk_field.remote_field.model),
  3472. ("thirdapp", "authorproxy"),
  3473. )
  3474. self.assertEqual(fk_field.remote_field.model, "thirdapp.AuthorProxy")
  3475. def test_proxy_to_mti_with_fk_to_proxy_proxy(self):
  3476. # First, test the pk table and field name.
  3477. to_state = self.make_project_state(
  3478. [
  3479. self.author_empty,
  3480. self.author_proxy,
  3481. self.author_proxy_proxy,
  3482. self.book_proxy_proxy_fk,
  3483. ]
  3484. )
  3485. changes = self.get_changes([], to_state)
  3486. fk_field = changes["otherapp"][0].operations[0].fields[1][1]
  3487. self.assertEqual(
  3488. to_state.get_concrete_model_key(fk_field.remote_field.model),
  3489. ("testapp", "author"),
  3490. )
  3491. self.assertEqual(fk_field.remote_field.model, "testapp.AAuthorProxyProxy")
  3492. # Change AuthorProxy to use MTI. FK still points to AAuthorProxyProxy,
  3493. # a proxy of AuthorProxy.
  3494. from_state = to_state.clone()
  3495. to_state = self.make_project_state(
  3496. [
  3497. self.author_empty,
  3498. self.author_proxy_notproxy,
  3499. self.author_proxy_proxy,
  3500. self.book_proxy_proxy_fk,
  3501. ]
  3502. )
  3503. changes = self.get_changes(from_state, to_state)
  3504. # Right number/type of migrations for the AuthorProxy model?
  3505. self.assertNumberMigrations(changes, "testapp", 1)
  3506. self.assertOperationTypes(changes, "testapp", 0, ["DeleteModel", "CreateModel"])
  3507. # Right number/type of migrations for the Book model with a FK to
  3508. # AAuthorProxyProxy?
  3509. self.assertNumberMigrations(changes, "otherapp", 1)
  3510. self.assertOperationTypes(changes, "otherapp", 0, ["AlterField"])
  3511. # otherapp should depend on testapp.
  3512. self.assertMigrationDependencies(
  3513. changes, "otherapp", 0, [("testapp", "auto_1")]
  3514. )
  3515. # Now, test the pk table and field name.
  3516. fk_field = changes["otherapp"][0].operations[0].field
  3517. self.assertEqual(
  3518. to_state.get_concrete_model_key(fk_field.remote_field.model),
  3519. ("testapp", "authorproxy"),
  3520. )
  3521. self.assertEqual(fk_field.remote_field.model, "testapp.AAuthorProxyProxy")
  3522. def test_unmanaged_create(self):
  3523. """The autodetector correctly deals with managed models."""
  3524. # First, we test adding an unmanaged model
  3525. changes = self.get_changes(
  3526. [self.author_empty], [self.author_empty, self.author_unmanaged]
  3527. )
  3528. # Right number/type of migrations?
  3529. self.assertNumberMigrations(changes, "testapp", 1)
  3530. self.assertOperationTypes(changes, "testapp", 0, ["CreateModel"])
  3531. self.assertOperationAttributes(
  3532. changes, "testapp", 0, 0, name="AuthorUnmanaged", options={"managed": False}
  3533. )
  3534. def test_unmanaged_delete(self):
  3535. changes = self.get_changes(
  3536. [self.author_empty, self.author_unmanaged], [self.author_empty]
  3537. )
  3538. self.assertNumberMigrations(changes, "testapp", 1)
  3539. self.assertOperationTypes(changes, "testapp", 0, ["DeleteModel"])
  3540. def test_unmanaged_to_managed(self):
  3541. # Now, we test turning an unmanaged model into a managed model
  3542. changes = self.get_changes(
  3543. [self.author_empty, self.author_unmanaged],
  3544. [self.author_empty, self.author_unmanaged_managed],
  3545. )
  3546. # Right number/type of migrations?
  3547. self.assertNumberMigrations(changes, "testapp", 1)
  3548. self.assertOperationTypes(changes, "testapp", 0, ["AlterModelOptions"])
  3549. self.assertOperationAttributes(
  3550. changes, "testapp", 0, 0, name="authorunmanaged", options={}
  3551. )
  3552. def test_managed_to_unmanaged(self):
  3553. # Now, we turn managed to unmanaged.
  3554. changes = self.get_changes(
  3555. [self.author_empty, self.author_unmanaged_managed],
  3556. [self.author_empty, self.author_unmanaged],
  3557. )
  3558. # Right number/type of migrations?
  3559. self.assertNumberMigrations(changes, "testapp", 1)
  3560. self.assertOperationTypes(changes, "testapp", 0, ["AlterModelOptions"])
  3561. self.assertOperationAttributes(
  3562. changes, "testapp", 0, 0, name="authorunmanaged", options={"managed": False}
  3563. )
  3564. def test_unmanaged_custom_pk(self):
  3565. """
  3566. #23415 - The autodetector must correctly deal with custom FK on
  3567. unmanaged models.
  3568. """
  3569. # First, we test the default pk field name
  3570. changes = self.get_changes([], [self.author_unmanaged_default_pk, self.book])
  3571. # The model the FK on the book model points to.
  3572. fk_field = changes["otherapp"][0].operations[0].fields[2][1]
  3573. self.assertEqual(fk_field.remote_field.model, "testapp.Author")
  3574. # Now, we test the custom pk field name
  3575. changes = self.get_changes([], [self.author_unmanaged_custom_pk, self.book])
  3576. # The model the FK on the book model points to.
  3577. fk_field = changes["otherapp"][0].operations[0].fields[2][1]
  3578. self.assertEqual(fk_field.remote_field.model, "testapp.Author")
  3579. @override_settings(AUTH_USER_MODEL="thirdapp.CustomUser")
  3580. def test_swappable(self):
  3581. with isolate_lru_cache(apps.get_swappable_settings_name):
  3582. changes = self.get_changes(
  3583. [self.custom_user], [self.custom_user, self.author_with_custom_user]
  3584. )
  3585. # Right number/type of migrations?
  3586. self.assertNumberMigrations(changes, "testapp", 1)
  3587. self.assertOperationTypes(changes, "testapp", 0, ["CreateModel"])
  3588. self.assertOperationAttributes(changes, "testapp", 0, 0, name="Author")
  3589. self.assertMigrationDependencies(
  3590. changes, "testapp", 0, [("__setting__", "AUTH_USER_MODEL")]
  3591. )
  3592. def test_swappable_lowercase(self):
  3593. model_state = ModelState(
  3594. "testapp",
  3595. "Document",
  3596. [
  3597. ("id", models.AutoField(primary_key=True)),
  3598. (
  3599. "owner",
  3600. models.ForeignKey(
  3601. settings.AUTH_USER_MODEL.lower(),
  3602. models.CASCADE,
  3603. ),
  3604. ),
  3605. ],
  3606. )
  3607. with isolate_lru_cache(apps.get_swappable_settings_name):
  3608. changes = self.get_changes([], [model_state])
  3609. self.assertNumberMigrations(changes, "testapp", 1)
  3610. self.assertOperationTypes(changes, "testapp", 0, ["CreateModel"])
  3611. self.assertOperationAttributes(changes, "testapp", 0, 0, name="Document")
  3612. self.assertMigrationDependencies(
  3613. changes,
  3614. "testapp",
  3615. 0,
  3616. [("__setting__", "AUTH_USER_MODEL")],
  3617. )
  3618. @override_settings(AUTH_USER_MODEL="thirdapp.CustomUser")
  3619. def test_swappable_many_to_many_model_case(self):
  3620. document_lowercase = ModelState(
  3621. "testapp",
  3622. "Document",
  3623. [
  3624. ("id", models.AutoField(primary_key=True)),
  3625. ("owners", models.ManyToManyField(settings.AUTH_USER_MODEL.lower())),
  3626. ],
  3627. )
  3628. document = ModelState(
  3629. "testapp",
  3630. "Document",
  3631. [
  3632. ("id", models.AutoField(primary_key=True)),
  3633. ("owners", models.ManyToManyField(settings.AUTH_USER_MODEL)),
  3634. ],
  3635. )
  3636. with isolate_lru_cache(apps.get_swappable_settings_name):
  3637. changes = self.get_changes(
  3638. [self.custom_user, document_lowercase],
  3639. [self.custom_user, document],
  3640. )
  3641. self.assertEqual(len(changes), 0)
  3642. def test_swappable_changed(self):
  3643. with isolate_lru_cache(apps.get_swappable_settings_name):
  3644. before = self.make_project_state([self.custom_user, self.author_with_user])
  3645. with override_settings(AUTH_USER_MODEL="thirdapp.CustomUser"):
  3646. after = self.make_project_state(
  3647. [self.custom_user, self.author_with_custom_user]
  3648. )
  3649. autodetector = MigrationAutodetector(before, after)
  3650. changes = autodetector._detect_changes()
  3651. # Right number/type of migrations?
  3652. self.assertNumberMigrations(changes, "testapp", 1)
  3653. self.assertOperationTypes(changes, "testapp", 0, ["AlterField"])
  3654. self.assertOperationAttributes(
  3655. changes, "testapp", 0, 0, model_name="author", name="user"
  3656. )
  3657. fk_field = changes["testapp"][0].operations[0].field
  3658. self.assertEqual(fk_field.remote_field.model, "thirdapp.CustomUser")
  3659. def test_add_field_with_default(self):
  3660. """#22030 - Adding a field with a default should work."""
  3661. changes = self.get_changes([self.author_empty], [self.author_name_default])
  3662. # Right number/type of migrations?
  3663. self.assertNumberMigrations(changes, "testapp", 1)
  3664. self.assertOperationTypes(changes, "testapp", 0, ["AddField"])
  3665. self.assertOperationAttributes(changes, "testapp", 0, 0, name="name")
  3666. def test_custom_deconstructible(self):
  3667. """
  3668. Two instances which deconstruct to the same value aren't considered a
  3669. change.
  3670. """
  3671. changes = self.get_changes(
  3672. [self.author_name_deconstructible_1], [self.author_name_deconstructible_2]
  3673. )
  3674. # Right number of migrations?
  3675. self.assertEqual(len(changes), 0)
  3676. def test_deconstruct_field_kwarg(self):
  3677. """Field instances are handled correctly by nested deconstruction."""
  3678. changes = self.get_changes(
  3679. [self.author_name_deconstructible_3], [self.author_name_deconstructible_4]
  3680. )
  3681. self.assertEqual(changes, {})
  3682. def test_deconstructible_list(self):
  3683. """Nested deconstruction descends into lists."""
  3684. # When lists contain items that deconstruct to identical values, those lists
  3685. # should be considered equal for the purpose of detecting state changes
  3686. # (even if the original items are unequal).
  3687. changes = self.get_changes(
  3688. [self.author_name_deconstructible_list_1],
  3689. [self.author_name_deconstructible_list_2],
  3690. )
  3691. self.assertEqual(changes, {})
  3692. # Legitimate differences within the deconstructed lists should be reported
  3693. # as a change
  3694. changes = self.get_changes(
  3695. [self.author_name_deconstructible_list_1],
  3696. [self.author_name_deconstructible_list_3],
  3697. )
  3698. self.assertEqual(len(changes), 1)
  3699. def test_deconstructible_tuple(self):
  3700. """Nested deconstruction descends into tuples."""
  3701. # When tuples contain items that deconstruct to identical values, those tuples
  3702. # should be considered equal for the purpose of detecting state changes
  3703. # (even if the original items are unequal).
  3704. changes = self.get_changes(
  3705. [self.author_name_deconstructible_tuple_1],
  3706. [self.author_name_deconstructible_tuple_2],
  3707. )
  3708. self.assertEqual(changes, {})
  3709. # Legitimate differences within the deconstructed tuples should be reported
  3710. # as a change
  3711. changes = self.get_changes(
  3712. [self.author_name_deconstructible_tuple_1],
  3713. [self.author_name_deconstructible_tuple_3],
  3714. )
  3715. self.assertEqual(len(changes), 1)
  3716. def test_deconstructible_dict(self):
  3717. """Nested deconstruction descends into dict values."""
  3718. # When dicts contain items whose values deconstruct to identical values,
  3719. # those dicts should be considered equal for the purpose of detecting
  3720. # state changes (even if the original values are unequal).
  3721. changes = self.get_changes(
  3722. [self.author_name_deconstructible_dict_1],
  3723. [self.author_name_deconstructible_dict_2],
  3724. )
  3725. self.assertEqual(changes, {})
  3726. # Legitimate differences within the deconstructed dicts should be reported
  3727. # as a change
  3728. changes = self.get_changes(
  3729. [self.author_name_deconstructible_dict_1],
  3730. [self.author_name_deconstructible_dict_3],
  3731. )
  3732. self.assertEqual(len(changes), 1)
  3733. def test_nested_deconstructible_objects(self):
  3734. """
  3735. Nested deconstruction is applied recursively to the args/kwargs of
  3736. deconstructed objects.
  3737. """
  3738. # If the items within a deconstructed object's args/kwargs have the same
  3739. # deconstructed values - whether or not the items themselves are different
  3740. # instances - then the object as a whole is regarded as unchanged.
  3741. changes = self.get_changes(
  3742. [self.author_name_nested_deconstructible_1],
  3743. [self.author_name_nested_deconstructible_2],
  3744. )
  3745. self.assertEqual(changes, {})
  3746. # Differences that exist solely within the args list of a deconstructed object
  3747. # should be reported as changes
  3748. changes = self.get_changes(
  3749. [self.author_name_nested_deconstructible_1],
  3750. [self.author_name_nested_deconstructible_changed_arg],
  3751. )
  3752. self.assertEqual(len(changes), 1)
  3753. # Additional args should also be reported as a change
  3754. changes = self.get_changes(
  3755. [self.author_name_nested_deconstructible_1],
  3756. [self.author_name_nested_deconstructible_extra_arg],
  3757. )
  3758. self.assertEqual(len(changes), 1)
  3759. # Differences that exist solely within the kwargs dict of a deconstructed object
  3760. # should be reported as changes
  3761. changes = self.get_changes(
  3762. [self.author_name_nested_deconstructible_1],
  3763. [self.author_name_nested_deconstructible_changed_kwarg],
  3764. )
  3765. self.assertEqual(len(changes), 1)
  3766. # Additional kwargs should also be reported as a change
  3767. changes = self.get_changes(
  3768. [self.author_name_nested_deconstructible_1],
  3769. [self.author_name_nested_deconstructible_extra_kwarg],
  3770. )
  3771. self.assertEqual(len(changes), 1)
  3772. def test_deconstruct_type(self):
  3773. """
  3774. #22951 -- Uninstantiated classes with deconstruct are correctly returned
  3775. by deep_deconstruct during serialization.
  3776. """
  3777. author = ModelState(
  3778. "testapp",
  3779. "Author",
  3780. [
  3781. ("id", models.AutoField(primary_key=True)),
  3782. (
  3783. "name",
  3784. models.CharField(
  3785. max_length=200,
  3786. # IntegerField intentionally not instantiated.
  3787. default=models.IntegerField,
  3788. ),
  3789. ),
  3790. ],
  3791. )
  3792. changes = self.get_changes([], [author])
  3793. # Right number/type of migrations?
  3794. self.assertNumberMigrations(changes, "testapp", 1)
  3795. self.assertOperationTypes(changes, "testapp", 0, ["CreateModel"])
  3796. def test_replace_string_with_foreignkey(self):
  3797. """
  3798. #22300 - Adding an FK in the same "spot" as a deleted CharField should
  3799. work.
  3800. """
  3801. changes = self.get_changes(
  3802. [self.author_with_publisher_string],
  3803. [self.author_with_publisher, self.publisher],
  3804. )
  3805. # Right number/type of migrations?
  3806. self.assertNumberMigrations(changes, "testapp", 1)
  3807. self.assertOperationTypes(
  3808. changes, "testapp", 0, ["CreateModel", "RemoveField", "AddField"]
  3809. )
  3810. self.assertOperationAttributes(changes, "testapp", 0, 0, name="Publisher")
  3811. self.assertOperationAttributes(changes, "testapp", 0, 1, name="publisher_name")
  3812. self.assertOperationAttributes(changes, "testapp", 0, 2, name="publisher")
  3813. def test_foreign_key_removed_before_target_model(self):
  3814. """
  3815. Removing an FK and the model it targets in the same change must remove
  3816. the FK field before the model to maintain consistency.
  3817. """
  3818. changes = self.get_changes(
  3819. [self.author_with_publisher, self.publisher], [self.author_name]
  3820. ) # removes both the model and FK
  3821. # Right number/type of migrations?
  3822. self.assertNumberMigrations(changes, "testapp", 1)
  3823. self.assertOperationTypes(changes, "testapp", 0, ["RemoveField", "DeleteModel"])
  3824. self.assertOperationAttributes(changes, "testapp", 0, 0, name="publisher")
  3825. self.assertOperationAttributes(changes, "testapp", 0, 1, name="Publisher")
  3826. @mock.patch(
  3827. "django.db.migrations.questioner.MigrationQuestioner.ask_not_null_addition",
  3828. side_effect=AssertionError("Should not have prompted for not null addition"),
  3829. )
  3830. def test_add_many_to_many(self, mocked_ask_method):
  3831. """#22435 - Adding a ManyToManyField should not prompt for a default."""
  3832. changes = self.get_changes(
  3833. [self.author_empty, self.publisher], [self.author_with_m2m, self.publisher]
  3834. )
  3835. # Right number/type of migrations?
  3836. self.assertNumberMigrations(changes, "testapp", 1)
  3837. self.assertOperationTypes(changes, "testapp", 0, ["AddField"])
  3838. self.assertOperationAttributes(changes, "testapp", 0, 0, name="publishers")
  3839. def test_alter_many_to_many(self):
  3840. changes = self.get_changes(
  3841. [self.author_with_m2m, self.publisher],
  3842. [self.author_with_m2m_blank, self.publisher],
  3843. )
  3844. # Right number/type of migrations?
  3845. self.assertNumberMigrations(changes, "testapp", 1)
  3846. self.assertOperationTypes(changes, "testapp", 0, ["AlterField"])
  3847. self.assertOperationAttributes(changes, "testapp", 0, 0, name="publishers")
  3848. def test_create_with_through_model(self):
  3849. """
  3850. Adding a m2m with a through model and the models that use it should be
  3851. ordered correctly.
  3852. """
  3853. changes = self.get_changes(
  3854. [], [self.author_with_m2m_through, self.publisher, self.contract]
  3855. )
  3856. # Right number/type of migrations?
  3857. self.assertNumberMigrations(changes, "testapp", 1)
  3858. self.assertOperationTypes(
  3859. changes,
  3860. "testapp",
  3861. 0,
  3862. [
  3863. "CreateModel",
  3864. "CreateModel",
  3865. "CreateModel",
  3866. "AddField",
  3867. ],
  3868. )
  3869. self.assertOperationAttributes(changes, "testapp", 0, 0, name="Author")
  3870. self.assertOperationAttributes(changes, "testapp", 0, 1, name="Publisher")
  3871. self.assertOperationAttributes(changes, "testapp", 0, 2, name="Contract")
  3872. self.assertOperationAttributes(
  3873. changes, "testapp", 0, 3, model_name="author", name="publishers"
  3874. )
  3875. def test_create_with_through_model_separate_apps(self):
  3876. author_with_m2m_through = ModelState(
  3877. "authors",
  3878. "Author",
  3879. [
  3880. ("id", models.AutoField(primary_key=True)),
  3881. (
  3882. "publishers",
  3883. models.ManyToManyField(
  3884. "testapp.Publisher", through="contract.Contract"
  3885. ),
  3886. ),
  3887. ],
  3888. )
  3889. contract = ModelState(
  3890. "contract",
  3891. "Contract",
  3892. [
  3893. ("id", models.AutoField(primary_key=True)),
  3894. ("author", models.ForeignKey("authors.Author", models.CASCADE)),
  3895. ("publisher", models.ForeignKey("testapp.Publisher", models.CASCADE)),
  3896. ],
  3897. )
  3898. changes = self.get_changes(
  3899. [], [author_with_m2m_through, self.publisher, contract]
  3900. )
  3901. self.assertNumberMigrations(changes, "testapp", 1)
  3902. self.assertNumberMigrations(changes, "contract", 1)
  3903. self.assertNumberMigrations(changes, "authors", 2)
  3904. self.assertMigrationDependencies(
  3905. changes,
  3906. "authors",
  3907. 1,
  3908. {("authors", "auto_1"), ("contract", "auto_1"), ("testapp", "auto_1")},
  3909. )
  3910. self.assertOperationTypes(changes, "testapp", 0, ["CreateModel"])
  3911. self.assertOperationAttributes(changes, "testapp", 0, 0, name="Publisher")
  3912. self.assertOperationTypes(changes, "contract", 0, ["CreateModel"])
  3913. self.assertOperationAttributes(changes, "contract", 0, 0, name="Contract")
  3914. self.assertOperationTypes(changes, "authors", 0, ["CreateModel"])
  3915. self.assertOperationTypes(changes, "authors", 1, ["AddField"])
  3916. self.assertOperationAttributes(changes, "authors", 0, 0, name="Author")
  3917. self.assertOperationAttributes(
  3918. changes, "authors", 1, 0, model_name="author", name="publishers"
  3919. )
  3920. def test_many_to_many_removed_before_through_model(self):
  3921. """
  3922. Removing a ManyToManyField and the "through" model in the same change
  3923. must remove the field before the model to maintain consistency.
  3924. """
  3925. changes = self.get_changes(
  3926. [
  3927. self.book_with_multiple_authors_through_attribution,
  3928. self.author_name,
  3929. self.attribution,
  3930. ],
  3931. [self.book_with_no_author, self.author_name],
  3932. )
  3933. # Remove both the through model and ManyToMany
  3934. # Right number/type of migrations?
  3935. self.assertNumberMigrations(changes, "otherapp", 1)
  3936. self.assertOperationTypes(
  3937. changes, "otherapp", 0, ["RemoveField", "DeleteModel"]
  3938. )
  3939. self.assertOperationAttributes(
  3940. changes, "otherapp", 0, 0, name="authors", model_name="book"
  3941. )
  3942. self.assertOperationAttributes(changes, "otherapp", 0, 1, name="Attribution")
  3943. def test_many_to_many_removed_before_through_model_2(self):
  3944. """
  3945. Removing a model that contains a ManyToManyField and the "through" model
  3946. in the same change must remove the field before the model to maintain
  3947. consistency.
  3948. """
  3949. changes = self.get_changes(
  3950. [
  3951. self.book_with_multiple_authors_through_attribution,
  3952. self.author_name,
  3953. self.attribution,
  3954. ],
  3955. [self.author_name],
  3956. )
  3957. # Remove both the through model and ManyToMany
  3958. # Right number/type of migrations?
  3959. self.assertNumberMigrations(changes, "otherapp", 1)
  3960. self.assertOperationTypes(
  3961. changes, "otherapp", 0, ["RemoveField", "DeleteModel", "DeleteModel"]
  3962. )
  3963. self.assertOperationAttributes(
  3964. changes, "otherapp", 0, 0, name="authors", model_name="book"
  3965. )
  3966. self.assertOperationAttributes(changes, "otherapp", 0, 1, name="Attribution")
  3967. self.assertOperationAttributes(changes, "otherapp", 0, 2, name="Book")
  3968. def test_m2m_w_through_multistep_remove(self):
  3969. """
  3970. A model with a m2m field that specifies a "through" model cannot be
  3971. removed in the same migration as that through model as the schema will
  3972. pass through an inconsistent state. The autodetector should produce two
  3973. migrations to avoid this issue.
  3974. """
  3975. changes = self.get_changes(
  3976. [self.author_with_m2m_through, self.publisher, self.contract],
  3977. [self.publisher],
  3978. )
  3979. # Right number/type of migrations?
  3980. self.assertNumberMigrations(changes, "testapp", 1)
  3981. self.assertOperationTypes(
  3982. changes,
  3983. "testapp",
  3984. 0,
  3985. ["RemoveField", "RemoveField", "DeleteModel", "DeleteModel"],
  3986. )
  3987. self.assertOperationAttributes(
  3988. changes, "testapp", 0, 0, name="author", model_name="contract"
  3989. )
  3990. self.assertOperationAttributes(
  3991. changes, "testapp", 0, 1, name="publisher", model_name="contract"
  3992. )
  3993. self.assertOperationAttributes(changes, "testapp", 0, 2, name="Author")
  3994. self.assertOperationAttributes(changes, "testapp", 0, 3, name="Contract")
  3995. def test_concrete_field_changed_to_many_to_many(self):
  3996. """
  3997. #23938 - Changing a concrete field into a ManyToManyField
  3998. first removes the concrete field and then adds the m2m field.
  3999. """
  4000. changes = self.get_changes(
  4001. [self.author_with_former_m2m], [self.author_with_m2m, self.publisher]
  4002. )
  4003. # Right number/type of migrations?
  4004. self.assertNumberMigrations(changes, "testapp", 1)
  4005. self.assertOperationTypes(
  4006. changes, "testapp", 0, ["CreateModel", "RemoveField", "AddField"]
  4007. )
  4008. self.assertOperationAttributes(changes, "testapp", 0, 0, name="Publisher")
  4009. self.assertOperationAttributes(
  4010. changes, "testapp", 0, 1, name="publishers", model_name="author"
  4011. )
  4012. self.assertOperationAttributes(
  4013. changes, "testapp", 0, 2, name="publishers", model_name="author"
  4014. )
  4015. def test_many_to_many_changed_to_concrete_field(self):
  4016. """
  4017. #23938 - Changing a ManyToManyField into a concrete field
  4018. first removes the m2m field and then adds the concrete field.
  4019. """
  4020. changes = self.get_changes(
  4021. [self.author_with_m2m, self.publisher], [self.author_with_former_m2m]
  4022. )
  4023. # Right number/type of migrations?
  4024. self.assertNumberMigrations(changes, "testapp", 1)
  4025. self.assertOperationTypes(
  4026. changes, "testapp", 0, ["RemoveField", "DeleteModel", "AddField"]
  4027. )
  4028. self.assertOperationAttributes(
  4029. changes, "testapp", 0, 0, name="publishers", model_name="author"
  4030. )
  4031. self.assertOperationAttributes(changes, "testapp", 0, 1, name="Publisher")
  4032. self.assertOperationAttributes(
  4033. changes, "testapp", 0, 2, name="publishers", model_name="author"
  4034. )
  4035. self.assertOperationFieldAttributes(changes, "testapp", 0, 2, max_length=100)
  4036. def test_non_circular_foreignkey_dependency_removal(self):
  4037. """
  4038. If two models with a ForeignKey from one to the other are removed at the
  4039. same time, the autodetector should remove them in the correct order.
  4040. """
  4041. changes = self.get_changes(
  4042. [self.author_with_publisher, self.publisher_with_author], []
  4043. )
  4044. # Right number/type of migrations?
  4045. self.assertNumberMigrations(changes, "testapp", 1)
  4046. self.assertOperationTypes(
  4047. changes, "testapp", 0, ["RemoveField", "DeleteModel", "DeleteModel"]
  4048. )
  4049. self.assertOperationAttributes(
  4050. changes, "testapp", 0, 0, name="author", model_name="publisher"
  4051. )
  4052. self.assertOperationAttributes(changes, "testapp", 0, 1, name="Author")
  4053. self.assertOperationAttributes(changes, "testapp", 0, 2, name="Publisher")
  4054. def test_alter_model_options(self):
  4055. """Changing a model's options should make a change."""
  4056. changes = self.get_changes([self.author_empty], [self.author_with_options])
  4057. # Right number/type of migrations?
  4058. self.assertNumberMigrations(changes, "testapp", 1)
  4059. self.assertOperationTypes(changes, "testapp", 0, ["AlterModelOptions"])
  4060. self.assertOperationAttributes(
  4061. changes,
  4062. "testapp",
  4063. 0,
  4064. 0,
  4065. options={
  4066. "permissions": [("can_hire", "Can hire")],
  4067. "verbose_name": "Authi",
  4068. },
  4069. )
  4070. # Changing them back to empty should also make a change
  4071. changes = self.get_changes([self.author_with_options], [self.author_empty])
  4072. # Right number/type of migrations?
  4073. self.assertNumberMigrations(changes, "testapp", 1)
  4074. self.assertOperationTypes(changes, "testapp", 0, ["AlterModelOptions"])
  4075. self.assertOperationAttributes(
  4076. changes, "testapp", 0, 0, name="author", options={}
  4077. )
  4078. def test_alter_model_options_proxy(self):
  4079. """Changing a proxy model's options should also make a change."""
  4080. changes = self.get_changes(
  4081. [self.author_proxy, self.author_empty],
  4082. [self.author_proxy_options, self.author_empty],
  4083. )
  4084. # Right number/type of migrations?
  4085. self.assertNumberMigrations(changes, "testapp", 1)
  4086. self.assertOperationTypes(changes, "testapp", 0, ["AlterModelOptions"])
  4087. self.assertOperationAttributes(
  4088. changes,
  4089. "testapp",
  4090. 0,
  4091. 0,
  4092. name="authorproxy",
  4093. options={"verbose_name": "Super Author"},
  4094. )
  4095. def test_set_alter_order_with_respect_to(self):
  4096. """Setting order_with_respect_to adds a field."""
  4097. changes = self.get_changes(
  4098. [self.book, self.author_with_book],
  4099. [self.book, self.author_with_book_order_wrt],
  4100. )
  4101. # Right number/type of migrations?
  4102. self.assertNumberMigrations(changes, "testapp", 1)
  4103. self.assertOperationTypes(changes, "testapp", 0, ["AlterOrderWithRespectTo"])
  4104. self.assertOperationAttributes(
  4105. changes, "testapp", 0, 0, name="author", order_with_respect_to="book"
  4106. )
  4107. def test_add_alter_order_with_respect_to(self):
  4108. """
  4109. Setting order_with_respect_to when adding the FK too does
  4110. things in the right order.
  4111. """
  4112. changes = self.get_changes(
  4113. [self.author_name], [self.book, self.author_with_book_order_wrt]
  4114. )
  4115. # Right number/type of migrations?
  4116. self.assertNumberMigrations(changes, "testapp", 1)
  4117. self.assertOperationTypes(
  4118. changes, "testapp", 0, ["AddField", "AlterOrderWithRespectTo"]
  4119. )
  4120. self.assertOperationAttributes(
  4121. changes, "testapp", 0, 0, model_name="author", name="book"
  4122. )
  4123. self.assertOperationAttributes(
  4124. changes, "testapp", 0, 1, name="author", order_with_respect_to="book"
  4125. )
  4126. def test_remove_alter_order_with_respect_to(self):
  4127. """
  4128. Removing order_with_respect_to when removing the FK too does
  4129. things in the right order.
  4130. """
  4131. changes = self.get_changes(
  4132. [self.book, self.author_with_book_order_wrt], [self.author_name]
  4133. )
  4134. # Right number/type of migrations?
  4135. self.assertNumberMigrations(changes, "testapp", 1)
  4136. self.assertOperationTypes(
  4137. changes, "testapp", 0, ["AlterOrderWithRespectTo", "RemoveField"]
  4138. )
  4139. self.assertOperationAttributes(
  4140. changes, "testapp", 0, 0, name="author", order_with_respect_to=None
  4141. )
  4142. self.assertOperationAttributes(
  4143. changes, "testapp", 0, 1, model_name="author", name="book"
  4144. )
  4145. def test_add_model_order_with_respect_to(self):
  4146. """
  4147. Setting order_with_respect_to when adding the whole model
  4148. does things in the right order.
  4149. """
  4150. changes = self.get_changes([], [self.book, self.author_with_book_order_wrt])
  4151. # Right number/type of migrations?
  4152. self.assertNumberMigrations(changes, "testapp", 1)
  4153. self.assertOperationTypes(changes, "testapp", 0, ["CreateModel"])
  4154. self.assertOperationAttributes(
  4155. changes,
  4156. "testapp",
  4157. 0,
  4158. 0,
  4159. name="Author",
  4160. options={"order_with_respect_to": "book"},
  4161. )
  4162. self.assertNotIn(
  4163. "_order",
  4164. [name for name, field in changes["testapp"][0].operations[0].fields],
  4165. )
  4166. def test_add_model_order_with_respect_to_unique_together(self):
  4167. changes = self.get_changes(
  4168. [],
  4169. [
  4170. self.book,
  4171. ModelState(
  4172. "testapp",
  4173. "Author",
  4174. [
  4175. ("id", models.AutoField(primary_key=True)),
  4176. ("name", models.CharField(max_length=200)),
  4177. ("book", models.ForeignKey("otherapp.Book", models.CASCADE)),
  4178. ],
  4179. options={
  4180. "order_with_respect_to": "book",
  4181. "unique_together": {("id", "_order")},
  4182. },
  4183. ),
  4184. ],
  4185. )
  4186. self.assertNumberMigrations(changes, "testapp", 1)
  4187. self.assertOperationTypes(changes, "testapp", 0, ["CreateModel"])
  4188. self.assertOperationAttributes(
  4189. changes,
  4190. "testapp",
  4191. 0,
  4192. 0,
  4193. name="Author",
  4194. options={
  4195. "order_with_respect_to": "book",
  4196. "unique_together": {("id", "_order")},
  4197. },
  4198. )
  4199. def test_add_model_order_with_respect_to_constraint(self):
  4200. after = ModelState(
  4201. "testapp",
  4202. "Author",
  4203. [
  4204. ("id", models.AutoField(primary_key=True)),
  4205. ("name", models.CharField(max_length=200)),
  4206. ("book", models.ForeignKey("otherapp.Book", models.CASCADE)),
  4207. ],
  4208. options={
  4209. "order_with_respect_to": "book",
  4210. "constraints": [
  4211. models.CheckConstraint(
  4212. condition=models.Q(_order__gt=1), name="book_order_gt_1"
  4213. ),
  4214. ],
  4215. },
  4216. )
  4217. changes = self.get_changes([], [self.book, after])
  4218. self.assertNumberMigrations(changes, "testapp", 1)
  4219. self.assertOperationTypes(
  4220. changes,
  4221. "testapp",
  4222. 0,
  4223. ["CreateModel"],
  4224. )
  4225. self.assertOperationAttributes(
  4226. changes,
  4227. "testapp",
  4228. 0,
  4229. 0,
  4230. name="Author",
  4231. options={
  4232. "order_with_respect_to": "book",
  4233. "constraints": [
  4234. models.CheckConstraint(
  4235. condition=models.Q(_order__gt=1), name="book_order_gt_1"
  4236. )
  4237. ],
  4238. },
  4239. )
  4240. def test_add_model_order_with_respect_to_index(self):
  4241. after = ModelState(
  4242. "testapp",
  4243. "Author",
  4244. [
  4245. ("id", models.AutoField(primary_key=True)),
  4246. ("name", models.CharField(max_length=200)),
  4247. ("book", models.ForeignKey("otherapp.Book", models.CASCADE)),
  4248. ],
  4249. options={
  4250. "order_with_respect_to": "book",
  4251. "indexes": [models.Index(fields=["_order"], name="book_order_idx")],
  4252. },
  4253. )
  4254. changes = self.get_changes([], [self.book, after])
  4255. self.assertNumberMigrations(changes, "testapp", 1)
  4256. self.assertOperationTypes(changes, "testapp", 0, ["CreateModel"])
  4257. self.assertOperationAttributes(
  4258. changes,
  4259. "testapp",
  4260. 0,
  4261. 0,
  4262. name="Author",
  4263. options={
  4264. "order_with_respect_to": "book",
  4265. "indexes": [models.Index(fields=["_order"], name="book_order_idx")],
  4266. },
  4267. )
  4268. def test_set_alter_order_with_respect_to_index_constraint_unique_together(self):
  4269. tests = [
  4270. (
  4271. "AddIndex",
  4272. {
  4273. "indexes": [
  4274. models.Index(fields=["_order"], name="book_order_idx"),
  4275. ]
  4276. },
  4277. ),
  4278. (
  4279. "AddConstraint",
  4280. {
  4281. "constraints": [
  4282. models.CheckConstraint(
  4283. condition=models.Q(_order__gt=1),
  4284. name="book_order_gt_1",
  4285. ),
  4286. ]
  4287. },
  4288. ),
  4289. ("AlterUniqueTogether", {"unique_together": {("id", "_order")}}),
  4290. ]
  4291. for operation, extra_option in tests:
  4292. with self.subTest(operation=operation):
  4293. after = ModelState(
  4294. "testapp",
  4295. "Author",
  4296. [
  4297. ("id", models.AutoField(primary_key=True)),
  4298. ("name", models.CharField(max_length=200)),
  4299. ("book", models.ForeignKey("otherapp.Book", models.CASCADE)),
  4300. ],
  4301. options={
  4302. "order_with_respect_to": "book",
  4303. **extra_option,
  4304. },
  4305. )
  4306. changes = self.get_changes(
  4307. [self.book, self.author_with_book],
  4308. [self.book, after],
  4309. )
  4310. self.assertNumberMigrations(changes, "testapp", 1)
  4311. self.assertOperationTypes(
  4312. changes,
  4313. "testapp",
  4314. 0,
  4315. [
  4316. "AlterOrderWithRespectTo",
  4317. operation,
  4318. ],
  4319. )
  4320. def test_alter_model_managers(self):
  4321. """
  4322. Changing the model managers adds a new operation.
  4323. """
  4324. changes = self.get_changes([self.other_pony], [self.other_pony_food])
  4325. # Right number/type of migrations?
  4326. self.assertNumberMigrations(changes, "otherapp", 1)
  4327. self.assertOperationTypes(changes, "otherapp", 0, ["AlterModelManagers"])
  4328. self.assertOperationAttributes(changes, "otherapp", 0, 0, name="pony")
  4329. self.assertEqual(
  4330. [name for name, mgr in changes["otherapp"][0].operations[0].managers],
  4331. ["food_qs", "food_mgr", "food_mgr_kwargs"],
  4332. )
  4333. self.assertEqual(
  4334. changes["otherapp"][0].operations[0].managers[1][1].args, ("a", "b", 1, 2)
  4335. )
  4336. self.assertEqual(
  4337. changes["otherapp"][0].operations[0].managers[2][1].args, ("x", "y", 3, 4)
  4338. )
  4339. def test_swappable_first_inheritance(self):
  4340. """Swappable models get their CreateModel first."""
  4341. changes = self.get_changes([], [self.custom_user, self.aardvark])
  4342. # Right number/type of migrations?
  4343. self.assertNumberMigrations(changes, "thirdapp", 1)
  4344. self.assertOperationTypes(
  4345. changes, "thirdapp", 0, ["CreateModel", "CreateModel"]
  4346. )
  4347. self.assertOperationAttributes(changes, "thirdapp", 0, 0, name="CustomUser")
  4348. self.assertOperationAttributes(changes, "thirdapp", 0, 1, name="Aardvark")
  4349. def test_default_related_name_option(self):
  4350. model_state = ModelState(
  4351. "app",
  4352. "model",
  4353. [
  4354. ("id", models.AutoField(primary_key=True)),
  4355. ],
  4356. options={"default_related_name": "related_name"},
  4357. )
  4358. changes = self.get_changes([], [model_state])
  4359. self.assertNumberMigrations(changes, "app", 1)
  4360. self.assertOperationTypes(changes, "app", 0, ["CreateModel"])
  4361. self.assertOperationAttributes(
  4362. changes,
  4363. "app",
  4364. 0,
  4365. 0,
  4366. name="model",
  4367. options={"default_related_name": "related_name"},
  4368. )
  4369. altered_model_state = ModelState(
  4370. "app",
  4371. "Model",
  4372. [
  4373. ("id", models.AutoField(primary_key=True)),
  4374. ],
  4375. )
  4376. changes = self.get_changes([model_state], [altered_model_state])
  4377. self.assertNumberMigrations(changes, "app", 1)
  4378. self.assertOperationTypes(changes, "app", 0, ["AlterModelOptions"])
  4379. self.assertOperationAttributes(changes, "app", 0, 0, name="model", options={})
  4380. @override_settings(AUTH_USER_MODEL="thirdapp.CustomUser")
  4381. def test_swappable_first_setting(self):
  4382. """Swappable models get their CreateModel first."""
  4383. with isolate_lru_cache(apps.get_swappable_settings_name):
  4384. changes = self.get_changes([], [self.custom_user_no_inherit, self.aardvark])
  4385. # Right number/type of migrations?
  4386. self.assertNumberMigrations(changes, "thirdapp", 1)
  4387. self.assertOperationTypes(
  4388. changes, "thirdapp", 0, ["CreateModel", "CreateModel"]
  4389. )
  4390. self.assertOperationAttributes(changes, "thirdapp", 0, 0, name="CustomUser")
  4391. self.assertOperationAttributes(changes, "thirdapp", 0, 1, name="Aardvark")
  4392. def test_bases_first(self):
  4393. """Bases of other models come first."""
  4394. changes = self.get_changes(
  4395. [], [self.aardvark_based_on_author, self.author_name]
  4396. )
  4397. # Right number/type of migrations?
  4398. self.assertNumberMigrations(changes, "testapp", 1)
  4399. self.assertOperationTypes(changes, "testapp", 0, ["CreateModel", "CreateModel"])
  4400. self.assertOperationAttributes(changes, "testapp", 0, 0, name="Author")
  4401. self.assertOperationAttributes(changes, "testapp", 0, 1, name="Aardvark")
  4402. def test_bases_first_mixed_case_app_label(self):
  4403. app_label = "MiXedCaseApp"
  4404. changes = self.get_changes(
  4405. [],
  4406. [
  4407. ModelState(
  4408. app_label,
  4409. "owner",
  4410. [
  4411. ("id", models.AutoField(primary_key=True)),
  4412. ],
  4413. ),
  4414. ModelState(
  4415. app_label,
  4416. "place",
  4417. [
  4418. ("id", models.AutoField(primary_key=True)),
  4419. (
  4420. "owner",
  4421. models.ForeignKey("MiXedCaseApp.owner", models.CASCADE),
  4422. ),
  4423. ],
  4424. ),
  4425. ModelState(app_label, "restaurant", [], bases=("MiXedCaseApp.place",)),
  4426. ],
  4427. )
  4428. self.assertNumberMigrations(changes, app_label, 1)
  4429. self.assertOperationTypes(
  4430. changes,
  4431. app_label,
  4432. 0,
  4433. [
  4434. "CreateModel",
  4435. "CreateModel",
  4436. "CreateModel",
  4437. ],
  4438. )
  4439. self.assertOperationAttributes(changes, app_label, 0, 0, name="owner")
  4440. self.assertOperationAttributes(changes, app_label, 0, 1, name="place")
  4441. self.assertOperationAttributes(changes, app_label, 0, 2, name="restaurant")
  4442. def test_multiple_bases(self):
  4443. """
  4444. Inheriting models doesn't move *_ptr fields into AddField operations.
  4445. """
  4446. A = ModelState("app", "A", [("a_id", models.AutoField(primary_key=True))])
  4447. B = ModelState("app", "B", [("b_id", models.AutoField(primary_key=True))])
  4448. C = ModelState("app", "C", [], bases=("app.A", "app.B"))
  4449. D = ModelState("app", "D", [], bases=("app.A", "app.B"))
  4450. E = ModelState("app", "E", [], bases=("app.A", "app.B"))
  4451. changes = self.get_changes([], [A, B, C, D, E])
  4452. # Right number/type of migrations?
  4453. self.assertNumberMigrations(changes, "app", 1)
  4454. self.assertOperationTypes(
  4455. changes,
  4456. "app",
  4457. 0,
  4458. ["CreateModel", "CreateModel", "CreateModel", "CreateModel", "CreateModel"],
  4459. )
  4460. self.assertOperationAttributes(changes, "app", 0, 0, name="A")
  4461. self.assertOperationAttributes(changes, "app", 0, 1, name="B")
  4462. self.assertOperationAttributes(changes, "app", 0, 2, name="C")
  4463. self.assertOperationAttributes(changes, "app", 0, 3, name="D")
  4464. self.assertOperationAttributes(changes, "app", 0, 4, name="E")
  4465. def test_proxy_bases_first(self):
  4466. """Bases of proxies come first."""
  4467. changes = self.get_changes(
  4468. [], [self.author_empty, self.author_proxy, self.author_proxy_proxy]
  4469. )
  4470. # Right number/type of migrations?
  4471. self.assertNumberMigrations(changes, "testapp", 1)
  4472. self.assertOperationTypes(
  4473. changes, "testapp", 0, ["CreateModel", "CreateModel", "CreateModel"]
  4474. )
  4475. self.assertOperationAttributes(changes, "testapp", 0, 0, name="Author")
  4476. self.assertOperationAttributes(changes, "testapp", 0, 1, name="AuthorProxy")
  4477. self.assertOperationAttributes(
  4478. changes, "testapp", 0, 2, name="AAuthorProxyProxy"
  4479. )
  4480. def test_pk_fk_included(self):
  4481. """
  4482. A relation used as the primary key is kept as part of CreateModel.
  4483. """
  4484. changes = self.get_changes([], [self.aardvark_pk_fk_author, self.author_name])
  4485. # Right number/type of migrations?
  4486. self.assertNumberMigrations(changes, "testapp", 1)
  4487. self.assertOperationTypes(changes, "testapp", 0, ["CreateModel", "CreateModel"])
  4488. self.assertOperationAttributes(changes, "testapp", 0, 0, name="Author")
  4489. self.assertOperationAttributes(changes, "testapp", 0, 1, name="Aardvark")
  4490. def test_first_dependency(self):
  4491. """
  4492. A dependency to an app with no migrations uses __first__.
  4493. """
  4494. # Load graph
  4495. loader = MigrationLoader(connection)
  4496. before = self.make_project_state([])
  4497. after = self.make_project_state([self.book_migrations_fk])
  4498. after.real_apps = {"migrations"}
  4499. autodetector = MigrationAutodetector(before, after)
  4500. changes = autodetector._detect_changes(graph=loader.graph)
  4501. # Right number/type of migrations?
  4502. self.assertNumberMigrations(changes, "otherapp", 1)
  4503. self.assertOperationTypes(changes, "otherapp", 0, ["CreateModel"])
  4504. self.assertOperationAttributes(changes, "otherapp", 0, 0, name="Book")
  4505. self.assertMigrationDependencies(
  4506. changes, "otherapp", 0, [("migrations", "__first__")]
  4507. )
  4508. @override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations"})
  4509. def test_last_dependency(self):
  4510. """
  4511. A dependency to an app with existing migrations uses the
  4512. last migration of that app.
  4513. """
  4514. # Load graph
  4515. loader = MigrationLoader(connection)
  4516. before = self.make_project_state([])
  4517. after = self.make_project_state([self.book_migrations_fk])
  4518. after.real_apps = {"migrations"}
  4519. autodetector = MigrationAutodetector(before, after)
  4520. changes = autodetector._detect_changes(graph=loader.graph)
  4521. # Right number/type of migrations?
  4522. self.assertNumberMigrations(changes, "otherapp", 1)
  4523. self.assertOperationTypes(changes, "otherapp", 0, ["CreateModel"])
  4524. self.assertOperationAttributes(changes, "otherapp", 0, 0, name="Book")
  4525. self.assertMigrationDependencies(
  4526. changes, "otherapp", 0, [("migrations", "0002_second")]
  4527. )
  4528. def test_alter_fk_before_model_deletion(self):
  4529. """
  4530. ForeignKeys are altered _before_ the model they used to
  4531. refer to are deleted.
  4532. """
  4533. changes = self.get_changes(
  4534. [self.author_name, self.publisher_with_author],
  4535. [self.aardvark_testapp, self.publisher_with_aardvark_author],
  4536. )
  4537. # Right number/type of migrations?
  4538. self.assertNumberMigrations(changes, "testapp", 1)
  4539. self.assertOperationTypes(
  4540. changes, "testapp", 0, ["CreateModel", "AlterField", "DeleteModel"]
  4541. )
  4542. self.assertOperationAttributes(changes, "testapp", 0, 0, name="Aardvark")
  4543. self.assertOperationAttributes(changes, "testapp", 0, 1, name="author")
  4544. self.assertOperationAttributes(changes, "testapp", 0, 2, name="Author")
  4545. def test_fk_dependency_other_app(self):
  4546. """
  4547. #23100 - ForeignKeys correctly depend on other apps' models.
  4548. """
  4549. changes = self.get_changes(
  4550. [self.author_name, self.book], [self.author_with_book, self.book]
  4551. )
  4552. # Right number/type of migrations?
  4553. self.assertNumberMigrations(changes, "testapp", 1)
  4554. self.assertOperationTypes(changes, "testapp", 0, ["AddField"])
  4555. self.assertOperationAttributes(changes, "testapp", 0, 0, name="book")
  4556. self.assertMigrationDependencies(
  4557. changes, "testapp", 0, [("otherapp", "__first__")]
  4558. )
  4559. def test_alter_unique_together_fk_to_m2m(self):
  4560. changes = self.get_changes(
  4561. [self.author_name, self.book_unique_together],
  4562. [
  4563. self.author_name,
  4564. ModelState(
  4565. "otherapp",
  4566. "Book",
  4567. [
  4568. ("id", models.AutoField(primary_key=True)),
  4569. ("author", models.ManyToManyField("testapp.Author")),
  4570. ("title", models.CharField(max_length=200)),
  4571. ],
  4572. ),
  4573. ],
  4574. )
  4575. self.assertNumberMigrations(changes, "otherapp", 1)
  4576. self.assertOperationTypes(
  4577. changes, "otherapp", 0, ["AlterUniqueTogether", "RemoveField", "AddField"]
  4578. )
  4579. self.assertOperationAttributes(
  4580. changes, "otherapp", 0, 0, name="book", unique_together=set()
  4581. )
  4582. self.assertOperationAttributes(
  4583. changes, "otherapp", 0, 1, model_name="book", name="author"
  4584. )
  4585. self.assertOperationAttributes(
  4586. changes, "otherapp", 0, 2, model_name="book", name="author"
  4587. )
  4588. def test_alter_field_to_fk_dependency_other_app(self):
  4589. changes = self.get_changes(
  4590. [self.author_empty, self.book_with_no_author_fk],
  4591. [self.author_empty, self.book],
  4592. )
  4593. self.assertNumberMigrations(changes, "otherapp", 1)
  4594. self.assertOperationTypes(changes, "otherapp", 0, ["AlterField"])
  4595. self.assertMigrationDependencies(
  4596. changes, "otherapp", 0, [("testapp", "__first__")]
  4597. )
  4598. def test_circular_dependency_mixed_addcreate(self):
  4599. """
  4600. #23315 - The dependency resolver knows to put all CreateModel
  4601. before AddField and not become unsolvable.
  4602. """
  4603. address = ModelState(
  4604. "a",
  4605. "Address",
  4606. [
  4607. ("id", models.AutoField(primary_key=True)),
  4608. ("country", models.ForeignKey("b.DeliveryCountry", models.CASCADE)),
  4609. ],
  4610. )
  4611. person = ModelState(
  4612. "a",
  4613. "Person",
  4614. [
  4615. ("id", models.AutoField(primary_key=True)),
  4616. ],
  4617. )
  4618. apackage = ModelState(
  4619. "b",
  4620. "APackage",
  4621. [
  4622. ("id", models.AutoField(primary_key=True)),
  4623. ("person", models.ForeignKey("a.Person", models.CASCADE)),
  4624. ],
  4625. )
  4626. country = ModelState(
  4627. "b",
  4628. "DeliveryCountry",
  4629. [
  4630. ("id", models.AutoField(primary_key=True)),
  4631. ],
  4632. )
  4633. changes = self.get_changes([], [address, person, apackage, country])
  4634. # Right number/type of migrations?
  4635. self.assertNumberMigrations(changes, "a", 2)
  4636. self.assertNumberMigrations(changes, "b", 1)
  4637. self.assertOperationTypes(changes, "a", 0, ["CreateModel", "CreateModel"])
  4638. self.assertOperationTypes(changes, "a", 1, ["AddField"])
  4639. self.assertOperationTypes(changes, "b", 0, ["CreateModel", "CreateModel"])
  4640. @override_settings(AUTH_USER_MODEL="a.Tenant")
  4641. def test_circular_dependency_swappable(self):
  4642. """
  4643. #23322 - The dependency resolver knows to explicitly resolve
  4644. swappable models.
  4645. """
  4646. with isolate_lru_cache(apps.get_swappable_settings_name):
  4647. tenant = ModelState(
  4648. "a",
  4649. "Tenant",
  4650. [
  4651. ("id", models.AutoField(primary_key=True)),
  4652. ("primary_address", models.ForeignKey("b.Address", models.CASCADE)),
  4653. ],
  4654. bases=(AbstractBaseUser,),
  4655. )
  4656. address = ModelState(
  4657. "b",
  4658. "Address",
  4659. [
  4660. ("id", models.AutoField(primary_key=True)),
  4661. (
  4662. "tenant",
  4663. models.ForeignKey(settings.AUTH_USER_MODEL, models.CASCADE),
  4664. ),
  4665. ],
  4666. )
  4667. changes = self.get_changes([], [address, tenant])
  4668. # Right number/type of migrations?
  4669. self.assertNumberMigrations(changes, "a", 2)
  4670. self.assertOperationTypes(changes, "a", 0, ["CreateModel"])
  4671. self.assertOperationTypes(changes, "a", 1, ["AddField"])
  4672. self.assertMigrationDependencies(changes, "a", 0, [])
  4673. self.assertMigrationDependencies(
  4674. changes, "a", 1, [("a", "auto_1"), ("b", "auto_1")]
  4675. )
  4676. # Right number/type of migrations?
  4677. self.assertNumberMigrations(changes, "b", 1)
  4678. self.assertOperationTypes(changes, "b", 0, ["CreateModel"])
  4679. self.assertMigrationDependencies(
  4680. changes, "b", 0, [("__setting__", "AUTH_USER_MODEL")]
  4681. )
  4682. @override_settings(AUTH_USER_MODEL="b.Tenant")
  4683. def test_circular_dependency_swappable2(self):
  4684. """
  4685. #23322 - The dependency resolver knows to explicitly resolve
  4686. swappable models but with the swappable not being the first migrated
  4687. model.
  4688. """
  4689. with isolate_lru_cache(apps.get_swappable_settings_name):
  4690. address = ModelState(
  4691. "a",
  4692. "Address",
  4693. [
  4694. ("id", models.AutoField(primary_key=True)),
  4695. (
  4696. "tenant",
  4697. models.ForeignKey(settings.AUTH_USER_MODEL, models.CASCADE),
  4698. ),
  4699. ],
  4700. )
  4701. tenant = ModelState(
  4702. "b",
  4703. "Tenant",
  4704. [
  4705. ("id", models.AutoField(primary_key=True)),
  4706. ("primary_address", models.ForeignKey("a.Address", models.CASCADE)),
  4707. ],
  4708. bases=(AbstractBaseUser,),
  4709. )
  4710. changes = self.get_changes([], [address, tenant])
  4711. # Right number/type of migrations?
  4712. self.assertNumberMigrations(changes, "a", 2)
  4713. self.assertOperationTypes(changes, "a", 0, ["CreateModel"])
  4714. self.assertOperationTypes(changes, "a", 1, ["AddField"])
  4715. self.assertMigrationDependencies(changes, "a", 0, [])
  4716. self.assertMigrationDependencies(
  4717. changes, "a", 1, [("__setting__", "AUTH_USER_MODEL"), ("a", "auto_1")]
  4718. )
  4719. # Right number/type of migrations?
  4720. self.assertNumberMigrations(changes, "b", 1)
  4721. self.assertOperationTypes(changes, "b", 0, ["CreateModel"])
  4722. self.assertMigrationDependencies(changes, "b", 0, [("a", "auto_1")])
  4723. @override_settings(AUTH_USER_MODEL="a.Person")
  4724. def test_circular_dependency_swappable_self(self):
  4725. """
  4726. #23322 - The dependency resolver knows to explicitly resolve
  4727. swappable models.
  4728. """
  4729. with isolate_lru_cache(apps.get_swappable_settings_name):
  4730. person = ModelState(
  4731. "a",
  4732. "Person",
  4733. [
  4734. ("id", models.AutoField(primary_key=True)),
  4735. (
  4736. "parent1",
  4737. models.ForeignKey(
  4738. settings.AUTH_USER_MODEL,
  4739. models.CASCADE,
  4740. related_name="children",
  4741. ),
  4742. ),
  4743. ],
  4744. )
  4745. changes = self.get_changes([], [person])
  4746. # Right number/type of migrations?
  4747. self.assertNumberMigrations(changes, "a", 1)
  4748. self.assertOperationTypes(changes, "a", 0, ["CreateModel"])
  4749. self.assertMigrationDependencies(changes, "a", 0, [])
  4750. @override_settings(AUTH_USER_MODEL="a.User")
  4751. def test_swappable_circular_multi_mti(self):
  4752. with isolate_lru_cache(apps.get_swappable_settings_name):
  4753. parent = ModelState(
  4754. "a",
  4755. "Parent",
  4756. [("user", models.ForeignKey(settings.AUTH_USER_MODEL, models.CASCADE))],
  4757. )
  4758. child = ModelState("a", "Child", [], bases=("a.Parent",))
  4759. user = ModelState("a", "User", [], bases=(AbstractBaseUser, "a.Child"))
  4760. changes = self.get_changes([], [parent, child, user])
  4761. self.assertNumberMigrations(changes, "a", 1)
  4762. self.assertOperationTypes(
  4763. changes, "a", 0, ["CreateModel", "CreateModel", "CreateModel", "AddField"]
  4764. )
  4765. @mock.patch(
  4766. "django.db.migrations.questioner.MigrationQuestioner.ask_not_null_addition",
  4767. side_effect=AssertionError("Should not have prompted for not null addition"),
  4768. )
  4769. def test_add_blank_textfield_and_charfield(self, mocked_ask_method):
  4770. """
  4771. #23405 - Adding a NOT NULL and blank `CharField` or `TextField`
  4772. without default should not prompt for a default.
  4773. """
  4774. changes = self.get_changes(
  4775. [self.author_empty], [self.author_with_biography_blank]
  4776. )
  4777. # Right number/type of migrations?
  4778. self.assertNumberMigrations(changes, "testapp", 1)
  4779. self.assertOperationTypes(changes, "testapp", 0, ["AddField", "AddField"])
  4780. self.assertOperationAttributes(changes, "testapp", 0, 0)
  4781. @mock.patch(
  4782. "django.db.migrations.questioner.MigrationQuestioner.ask_not_null_addition"
  4783. )
  4784. def test_add_non_blank_textfield_and_charfield(self, mocked_ask_method):
  4785. """
  4786. #23405 - Adding a NOT NULL and non-blank `CharField` or `TextField`
  4787. without default should prompt for a default.
  4788. """
  4789. changes = self.get_changes(
  4790. [self.author_empty], [self.author_with_biography_non_blank]
  4791. )
  4792. self.assertEqual(mocked_ask_method.call_count, 2)
  4793. # Right number/type of migrations?
  4794. self.assertNumberMigrations(changes, "testapp", 1)
  4795. self.assertOperationTypes(changes, "testapp", 0, ["AddField", "AddField"])
  4796. self.assertOperationAttributes(changes, "testapp", 0, 0)
  4797. def test_mti_inheritance_model_removal(self):
  4798. Animal = ModelState(
  4799. "app",
  4800. "Animal",
  4801. [
  4802. ("id", models.AutoField(primary_key=True)),
  4803. ],
  4804. )
  4805. Dog = ModelState("app", "Dog", [], bases=("app.Animal",))
  4806. changes = self.get_changes([Animal, Dog], [Animal])
  4807. self.assertNumberMigrations(changes, "app", 1)
  4808. self.assertOperationTypes(changes, "app", 0, ["DeleteModel"])
  4809. self.assertOperationAttributes(changes, "app", 0, 0, name="Dog")
  4810. def test_add_model_with_field_removed_from_base_model(self):
  4811. """
  4812. Removing a base field takes place before adding a new inherited model
  4813. that has a field with the same name.
  4814. """
  4815. before = [
  4816. ModelState(
  4817. "app",
  4818. "readable",
  4819. [
  4820. ("id", models.AutoField(primary_key=True)),
  4821. ("title", models.CharField(max_length=200)),
  4822. ],
  4823. ),
  4824. ]
  4825. after = [
  4826. ModelState(
  4827. "app",
  4828. "readable",
  4829. [
  4830. ("id", models.AutoField(primary_key=True)),
  4831. ],
  4832. ),
  4833. ModelState(
  4834. "app",
  4835. "book",
  4836. [
  4837. ("title", models.CharField(max_length=200)),
  4838. ],
  4839. bases=("app.readable",),
  4840. ),
  4841. ]
  4842. changes = self.get_changes(before, after)
  4843. self.assertNumberMigrations(changes, "app", 1)
  4844. self.assertOperationTypes(changes, "app", 0, ["RemoveField", "CreateModel"])
  4845. self.assertOperationAttributes(
  4846. changes, "app", 0, 0, name="title", model_name="readable"
  4847. )
  4848. self.assertOperationAttributes(changes, "app", 0, 1, name="book")
  4849. def test_parse_number(self):
  4850. tests = [
  4851. ("no_number", None),
  4852. ("0001_initial", 1),
  4853. ("0002_model3", 2),
  4854. ("0002_auto_20380101_1112", 2),
  4855. ("0002_squashed_0003", 3),
  4856. ("0002_model2_squashed_0003_other4", 3),
  4857. ("0002_squashed_0003_squashed_0004", 4),
  4858. ("0002_model2_squashed_0003_other4_squashed_0005_other6", 5),
  4859. ("0002_custom_name_20380101_1112_squashed_0003_model", 3),
  4860. ("2_squashed_4", 4),
  4861. ]
  4862. for migration_name, expected_number in tests:
  4863. with self.subTest(migration_name=migration_name):
  4864. self.assertEqual(
  4865. MigrationAutodetector.parse_number(migration_name),
  4866. expected_number,
  4867. )
  4868. def test_add_custom_fk_with_hardcoded_to(self):
  4869. class HardcodedForeignKey(models.ForeignKey):
  4870. def __init__(self, *args, **kwargs):
  4871. kwargs["to"] = "testapp.Author"
  4872. super().__init__(*args, **kwargs)
  4873. def deconstruct(self):
  4874. name, path, args, kwargs = super().deconstruct()
  4875. del kwargs["to"]
  4876. return name, path, args, kwargs
  4877. book_hardcoded_fk_to = ModelState(
  4878. "testapp",
  4879. "Book",
  4880. [
  4881. ("author", HardcodedForeignKey(on_delete=models.CASCADE)),
  4882. ],
  4883. )
  4884. changes = self.get_changes(
  4885. [self.author_empty],
  4886. [self.author_empty, book_hardcoded_fk_to],
  4887. )
  4888. self.assertNumberMigrations(changes, "testapp", 1)
  4889. self.assertOperationTypes(changes, "testapp", 0, ["CreateModel"])
  4890. self.assertOperationAttributes(changes, "testapp", 0, 0, name="Book")
  4891. @mock.patch(
  4892. "django.db.migrations.questioner.MigrationQuestioner.ask_not_null_addition"
  4893. )
  4894. def test_add_composite_pk(self, mocked_ask_method):
  4895. before = [
  4896. ModelState(
  4897. "app",
  4898. "foo",
  4899. [
  4900. ("id", models.AutoField(primary_key=True)),
  4901. ],
  4902. ),
  4903. ]
  4904. after = [
  4905. ModelState(
  4906. "app",
  4907. "foo",
  4908. [
  4909. ("pk", models.CompositePrimaryKey("foo_id", "bar_id")),
  4910. ("id", models.IntegerField()),
  4911. ],
  4912. ),
  4913. ]
  4914. changes = self.get_changes(before, after)
  4915. self.assertEqual(mocked_ask_method.call_count, 0)
  4916. self.assertNumberMigrations(changes, "app", 1)
  4917. self.assertOperationTypes(changes, "app", 0, ["AddField", "AlterField"])
  4918. self.assertOperationAttributes(
  4919. changes,
  4920. "app",
  4921. 0,
  4922. 0,
  4923. name="pk",
  4924. model_name="foo",
  4925. preserve_default=True,
  4926. )
  4927. self.assertOperationAttributes(
  4928. changes,
  4929. "app",
  4930. 0,
  4931. 1,
  4932. name="id",
  4933. model_name="foo",
  4934. preserve_default=True,
  4935. )
  4936. def test_remove_composite_pk(self):
  4937. before = [
  4938. ModelState(
  4939. "app",
  4940. "foo",
  4941. [
  4942. ("pk", models.CompositePrimaryKey("foo_id", "bar_id")),
  4943. ("id", models.IntegerField()),
  4944. ],
  4945. ),
  4946. ]
  4947. after = [
  4948. ModelState(
  4949. "app",
  4950. "foo",
  4951. [
  4952. ("id", models.AutoField(primary_key=True)),
  4953. ],
  4954. ),
  4955. ]
  4956. changes = self.get_changes(before, after)
  4957. self.assertNumberMigrations(changes, "app", 1)
  4958. self.assertOperationTypes(changes, "app", 0, ["RemoveField", "AlterField"])
  4959. self.assertOperationAttributes(
  4960. changes,
  4961. "app",
  4962. 0,
  4963. 0,
  4964. name="pk",
  4965. model_name="foo",
  4966. )
  4967. self.assertOperationAttributes(
  4968. changes,
  4969. "app",
  4970. 0,
  4971. 1,
  4972. name="id",
  4973. model_name="foo",
  4974. preserve_default=True,
  4975. )
  4976. class MigrationSuggestNameTests(SimpleTestCase):
  4977. def test_no_operations(self):
  4978. class Migration(migrations.Migration):
  4979. operations = []
  4980. migration = Migration("some_migration", "test_app")
  4981. self.assertIs(migration.suggest_name().startswith("auto_"), True)
  4982. def test_no_operations_initial(self):
  4983. class Migration(migrations.Migration):
  4984. initial = True
  4985. operations = []
  4986. migration = Migration("some_migration", "test_app")
  4987. self.assertEqual(migration.suggest_name(), "initial")
  4988. def test_single_operation(self):
  4989. class Migration(migrations.Migration):
  4990. operations = [migrations.CreateModel("Person", fields=[])]
  4991. migration = Migration("0001_initial", "test_app")
  4992. self.assertEqual(migration.suggest_name(), "person")
  4993. class Migration(migrations.Migration):
  4994. operations = [migrations.DeleteModel("Person")]
  4995. migration = Migration("0002_initial", "test_app")
  4996. self.assertEqual(migration.suggest_name(), "delete_person")
  4997. def test_single_operation_long_name(self):
  4998. class Migration(migrations.Migration):
  4999. operations = [migrations.CreateModel("A" * 53, fields=[])]
  5000. migration = Migration("some_migration", "test_app")
  5001. self.assertEqual(migration.suggest_name(), "a" * 53)
  5002. def test_two_operations(self):
  5003. class Migration(migrations.Migration):
  5004. operations = [
  5005. migrations.CreateModel("Person", fields=[]),
  5006. migrations.DeleteModel("Animal"),
  5007. ]
  5008. migration = Migration("some_migration", "test_app")
  5009. self.assertEqual(migration.suggest_name(), "person_delete_animal")
  5010. def test_two_create_models(self):
  5011. class Migration(migrations.Migration):
  5012. operations = [
  5013. migrations.CreateModel("Person", fields=[]),
  5014. migrations.CreateModel("Animal", fields=[]),
  5015. ]
  5016. migration = Migration("0001_initial", "test_app")
  5017. self.assertEqual(migration.suggest_name(), "person_animal")
  5018. def test_two_create_models_with_initial_true(self):
  5019. class Migration(migrations.Migration):
  5020. initial = True
  5021. operations = [
  5022. migrations.CreateModel("Person", fields=[]),
  5023. migrations.CreateModel("Animal", fields=[]),
  5024. ]
  5025. migration = Migration("0001_initial", "test_app")
  5026. self.assertEqual(migration.suggest_name(), "initial")
  5027. def test_many_operations_suffix(self):
  5028. class Migration(migrations.Migration):
  5029. operations = [
  5030. migrations.CreateModel("Person1", fields=[]),
  5031. migrations.CreateModel("Person2", fields=[]),
  5032. migrations.CreateModel("Person3", fields=[]),
  5033. migrations.DeleteModel("Person4"),
  5034. migrations.DeleteModel("Person5"),
  5035. ]
  5036. migration = Migration("some_migration", "test_app")
  5037. self.assertEqual(
  5038. migration.suggest_name(),
  5039. "person1_person2_person3_delete_person4_and_more",
  5040. )
  5041. def test_operation_with_no_suggested_name(self):
  5042. class Migration(migrations.Migration):
  5043. operations = [
  5044. migrations.CreateModel("Person", fields=[]),
  5045. migrations.RunSQL("SELECT 1 FROM person;"),
  5046. ]
  5047. migration = Migration("some_migration", "test_app")
  5048. self.assertIs(migration.suggest_name().startswith("auto_"), True)
  5049. def test_operation_with_invalid_chars_in_suggested_name(self):
  5050. class Migration(migrations.Migration):
  5051. operations = [
  5052. migrations.AddConstraint(
  5053. "Person",
  5054. models.UniqueConstraint(
  5055. fields=["name"], name="person.name-*~unique!"
  5056. ),
  5057. ),
  5058. ]
  5059. migration = Migration("some_migration", "test_app")
  5060. self.assertEqual(migration.suggest_name(), "person_person_name_unique_")
  5061. def test_none_name(self):
  5062. class Migration(migrations.Migration):
  5063. operations = [migrations.RunSQL("SELECT 1 FROM person;")]
  5064. migration = Migration("0001_initial", "test_app")
  5065. suggest_name = migration.suggest_name()
  5066. self.assertIs(suggest_name.startswith("auto_"), True)
  5067. def test_none_name_with_initial_true(self):
  5068. class Migration(migrations.Migration):
  5069. initial = True
  5070. operations = [migrations.RunSQL("SELECT 1 FROM person;")]
  5071. migration = Migration("0001_initial", "test_app")
  5072. self.assertEqual(migration.suggest_name(), "initial")
  5073. def test_auto(self):
  5074. migration = migrations.Migration("0001_initial", "test_app")
  5075. suggest_name = migration.suggest_name()
  5076. self.assertIs(suggest_name.startswith("auto_"), True)