1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765 |
- import datetime
- import itertools
- import unittest
- from copy import copy
- from unittest import mock
- from django.core.exceptions import FieldError
- from django.core.management.color import no_style
- from django.db import (
- DatabaseError,
- DataError,
- IntegrityError,
- OperationalError,
- connection,
- )
- from django.db.models import (
- CASCADE,
- PROTECT,
- AutoField,
- BigAutoField,
- BigIntegerField,
- BinaryField,
- BooleanField,
- CharField,
- CheckConstraint,
- DateField,
- DateTimeField,
- DecimalField,
- DurationField,
- F,
- FloatField,
- ForeignKey,
- ForeignObject,
- Index,
- IntegerField,
- JSONField,
- ManyToManyField,
- Model,
- OneToOneField,
- OrderBy,
- PositiveIntegerField,
- Q,
- SlugField,
- SmallAutoField,
- SmallIntegerField,
- TextField,
- TimeField,
- UniqueConstraint,
- UUIDField,
- Value,
- )
- from django.db.models.fields.json import KeyTextTransform
- from django.db.models.functions import Abs, Cast, Collate, Lower, Random, Upper
- from django.db.models.indexes import IndexExpression
- from django.db.transaction import TransactionManagementError, atomic
- from django.test import (
- TransactionTestCase,
- ignore_warnings,
- skipIfDBFeature,
- skipUnlessDBFeature,
- )
- from django.test.utils import CaptureQueriesContext, isolate_apps, register_lookup
- from django.utils.deprecation import RemovedInDjango51Warning
- from .fields import CustomManyToManyField, InheritedManyToManyField, MediumBlobField
- from .models import (
- Author,
- AuthorCharFieldWithIndex,
- AuthorTextFieldWithIndex,
- AuthorWithDefaultHeight,
- AuthorWithEvenLongerName,
- AuthorWithIndexedName,
- AuthorWithUniqueName,
- AuthorWithUniqueNameAndBirthday,
- Book,
- BookForeignObj,
- BookWeak,
- BookWithLongName,
- BookWithO2O,
- BookWithoutAuthor,
- BookWithSlug,
- IntegerPK,
- Node,
- Note,
- NoteRename,
- Tag,
- TagM2MTest,
- TagUniqueRename,
- Thing,
- UniqueTest,
- new_apps,
- )
- class SchemaTests(TransactionTestCase):
- """
- Tests for the schema-alteration code.
- Be aware that these tests are more liable than most to false results,
- as sometimes the code to check if a test has worked is almost as complex
- as the code it is testing.
- """
- available_apps = []
- models = [
- Author,
- AuthorCharFieldWithIndex,
- AuthorTextFieldWithIndex,
- AuthorWithDefaultHeight,
- AuthorWithEvenLongerName,
- Book,
- BookWeak,
- BookWithLongName,
- BookWithO2O,
- BookWithSlug,
- IntegerPK,
- Node,
- Note,
- Tag,
- TagM2MTest,
- TagUniqueRename,
- Thing,
- UniqueTest,
- ]
- # Utility functions
- def setUp(self):
- # local_models should contain test dependent model classes that will be
- # automatically removed from the app cache on test tear down.
- self.local_models = []
- # isolated_local_models contains models that are in test methods
- # decorated with @isolate_apps.
- self.isolated_local_models = []
- def tearDown(self):
- # Delete any tables made for our models
- self.delete_tables()
- new_apps.clear_cache()
- for model in new_apps.get_models():
- model._meta._expire_cache()
- if "schema" in new_apps.all_models:
- for model in self.local_models:
- for many_to_many in model._meta.many_to_many:
- through = many_to_many.remote_field.through
- if through and through._meta.auto_created:
- del new_apps.all_models["schema"][through._meta.model_name]
- del new_apps.all_models["schema"][model._meta.model_name]
- if self.isolated_local_models:
- with connection.schema_editor() as editor:
- for model in self.isolated_local_models:
- editor.delete_model(model)
- def delete_tables(self):
- "Deletes all model tables for our models for a clean test environment"
- converter = connection.introspection.identifier_converter
- with connection.schema_editor() as editor:
- connection.disable_constraint_checking()
- table_names = connection.introspection.table_names()
- if connection.features.ignores_table_name_case:
- table_names = [table_name.lower() for table_name in table_names]
- for model in itertools.chain(SchemaTests.models, self.local_models):
- tbl = converter(model._meta.db_table)
- if connection.features.ignores_table_name_case:
- tbl = tbl.lower()
- if tbl in table_names:
- editor.delete_model(model)
- table_names.remove(tbl)
- connection.enable_constraint_checking()
- def column_classes(self, model):
- with connection.cursor() as cursor:
- columns = {
- d[0]: (connection.introspection.get_field_type(d[1], d), d)
- for d in connection.introspection.get_table_description(
- cursor,
- model._meta.db_table,
- )
- }
- # SQLite has a different format for field_type
- for name, (type, desc) in columns.items():
- if isinstance(type, tuple):
- columns[name] = (type[0], desc)
- return columns
- def get_primary_key(self, table):
- with connection.cursor() as cursor:
- return connection.introspection.get_primary_key_column(cursor, table)
- def get_indexes(self, table):
- """
- Get the indexes on the table using a new cursor.
- """
- with connection.cursor() as cursor:
- return [
- c["columns"][0]
- for c in connection.introspection.get_constraints(
- cursor, table
- ).values()
- if c["index"] and len(c["columns"]) == 1
- ]
- def get_uniques(self, table):
- with connection.cursor() as cursor:
- return [
- c["columns"][0]
- for c in connection.introspection.get_constraints(
- cursor, table
- ).values()
- if c["unique"] and len(c["columns"]) == 1
- ]
- def get_constraints(self, table):
- """
- Get the constraints on a table using a new cursor.
- """
- with connection.cursor() as cursor:
- return connection.introspection.get_constraints(cursor, table)
- def get_constraints_for_column(self, model, column_name):
- constraints = self.get_constraints(model._meta.db_table)
- constraints_for_column = []
- for name, details in constraints.items():
- if details["columns"] == [column_name]:
- constraints_for_column.append(name)
- return sorted(constraints_for_column)
- def check_added_field_default(
- self,
- schema_editor,
- model,
- field,
- field_name,
- expected_default,
- cast_function=None,
- ):
- with connection.cursor() as cursor:
- schema_editor.add_field(model, field)
- cursor.execute(
- "SELECT {} FROM {};".format(field_name, model._meta.db_table)
- )
- database_default = cursor.fetchall()[0][0]
- if cast_function and type(database_default) != type(expected_default):
- database_default = cast_function(database_default)
- self.assertEqual(database_default, expected_default)
- def get_constraints_count(self, table, column, fk_to):
- """
- Return a dict with keys 'fks', 'uniques, and 'indexes' indicating the
- number of foreign keys, unique constraints, and indexes on
- `table`.`column`. The `fk_to` argument is a 2-tuple specifying the
- expected foreign key relationship's (table, column).
- """
- with connection.cursor() as cursor:
- constraints = connection.introspection.get_constraints(cursor, table)
- counts = {"fks": 0, "uniques": 0, "indexes": 0}
- for c in constraints.values():
- if c["columns"] == [column]:
- if c["foreign_key"] == fk_to:
- counts["fks"] += 1
- if c["unique"]:
- counts["uniques"] += 1
- elif c["index"]:
- counts["indexes"] += 1
- return counts
- def get_column_collation(self, table, column):
- with connection.cursor() as cursor:
- return next(
- f.collation
- for f in connection.introspection.get_table_description(cursor, table)
- if f.name == column
- )
- def assertIndexOrder(self, table, index, order):
- constraints = self.get_constraints(table)
- self.assertIn(index, constraints)
- index_orders = constraints[index]["orders"]
- self.assertTrue(
- all(val == expected for val, expected in zip(index_orders, order))
- )
- def assertForeignKeyExists(self, model, column, expected_fk_table, field="id"):
- """
- Fail if the FK constraint on `model.Meta.db_table`.`column` to
- `expected_fk_table`.id doesn't exist.
- """
- if not connection.features.can_introspect_foreign_keys:
- return
- constraints = self.get_constraints(model._meta.db_table)
- constraint_fk = None
- for details in constraints.values():
- if details["columns"] == [column] and details["foreign_key"]:
- constraint_fk = details["foreign_key"]
- break
- self.assertEqual(constraint_fk, (expected_fk_table, field))
- def assertForeignKeyNotExists(self, model, column, expected_fk_table):
- if not connection.features.can_introspect_foreign_keys:
- return
- with self.assertRaises(AssertionError):
- self.assertForeignKeyExists(model, column, expected_fk_table)
- # Tests
- def test_creation_deletion(self):
- """
- Tries creating a model's table, and then deleting it.
- """
- with connection.schema_editor() as editor:
- # Create the table
- editor.create_model(Author)
- # The table is there
- list(Author.objects.all())
- # Clean up that table
- editor.delete_model(Author)
- # No deferred SQL should be left over.
- self.assertEqual(editor.deferred_sql, [])
- # The table is gone
- with self.assertRaises(DatabaseError):
- list(Author.objects.all())
- @skipUnlessDBFeature("supports_foreign_keys")
- def test_fk(self):
- "Creating tables out of FK order, then repointing, works"
- # Create the table
- with connection.schema_editor() as editor:
- editor.create_model(Book)
- editor.create_model(Author)
- editor.create_model(Tag)
- # Initial tables are there
- list(Author.objects.all())
- list(Book.objects.all())
- # Make sure the FK constraint is present
- with self.assertRaises(IntegrityError):
- Book.objects.create(
- author_id=1,
- title="Much Ado About Foreign Keys",
- pub_date=datetime.datetime.now(),
- )
- # Repoint the FK constraint
- old_field = Book._meta.get_field("author")
- new_field = ForeignKey(Tag, CASCADE)
- new_field.set_attributes_from_name("author")
- with connection.schema_editor() as editor:
- editor.alter_field(Book, old_field, new_field, strict=True)
- self.assertForeignKeyExists(Book, "author_id", "schema_tag")
- @skipUnlessDBFeature("can_create_inline_fk")
- def test_inline_fk(self):
- # Create some tables.
- with connection.schema_editor() as editor:
- editor.create_model(Author)
- editor.create_model(Book)
- editor.create_model(Note)
- self.assertForeignKeyNotExists(Note, "book_id", "schema_book")
- # Add a foreign key from one to the other.
- with connection.schema_editor() as editor:
- new_field = ForeignKey(Book, CASCADE)
- new_field.set_attributes_from_name("book")
- editor.add_field(Note, new_field)
- self.assertForeignKeyExists(Note, "book_id", "schema_book")
- # Creating a FK field with a constraint uses a single statement without
- # a deferred ALTER TABLE.
- self.assertFalse(
- [
- sql
- for sql in (str(statement) for statement in editor.deferred_sql)
- if sql.startswith("ALTER TABLE") and "ADD CONSTRAINT" in sql
- ]
- )
- @skipUnlessDBFeature("can_create_inline_fk")
- def test_add_inline_fk_update_data(self):
- with connection.schema_editor() as editor:
- editor.create_model(Node)
- # Add an inline foreign key and update data in the same transaction.
- new_field = ForeignKey(Node, CASCADE, related_name="new_fk", null=True)
- new_field.set_attributes_from_name("new_parent_fk")
- parent = Node.objects.create()
- with connection.schema_editor() as editor:
- editor.add_field(Node, new_field)
- editor.execute("UPDATE schema_node SET new_parent_fk_id = %s;", [parent.pk])
- assertIndex = (
- self.assertIn
- if connection.features.indexes_foreign_keys
- else self.assertNotIn
- )
- assertIndex("new_parent_fk_id", self.get_indexes(Node._meta.db_table))
- @skipUnlessDBFeature(
- "can_create_inline_fk",
- "allows_multiple_constraints_on_same_fields",
- )
- @isolate_apps("schema")
- def test_add_inline_fk_index_update_data(self):
- class Node(Model):
- class Meta:
- app_label = "schema"
- with connection.schema_editor() as editor:
- editor.create_model(Node)
- # Add an inline foreign key, update data, and an index in the same
- # transaction.
- new_field = ForeignKey(Node, CASCADE, related_name="new_fk", null=True)
- new_field.set_attributes_from_name("new_parent_fk")
- parent = Node.objects.create()
- with connection.schema_editor() as editor:
- editor.add_field(Node, new_field)
- Node._meta.add_field(new_field)
- editor.execute("UPDATE schema_node SET new_parent_fk_id = %s;", [parent.pk])
- editor.add_index(
- Node, Index(fields=["new_parent_fk"], name="new_parent_inline_fk_idx")
- )
- self.assertIn("new_parent_fk_id", self.get_indexes(Node._meta.db_table))
- @skipUnlessDBFeature("supports_foreign_keys")
- def test_char_field_with_db_index_to_fk(self):
- # Create the table
- with connection.schema_editor() as editor:
- editor.create_model(Author)
- editor.create_model(AuthorCharFieldWithIndex)
- # Change CharField to FK
- old_field = AuthorCharFieldWithIndex._meta.get_field("char_field")
- new_field = ForeignKey(Author, CASCADE, blank=True)
- new_field.set_attributes_from_name("char_field")
- with connection.schema_editor() as editor:
- editor.alter_field(
- AuthorCharFieldWithIndex, old_field, new_field, strict=True
- )
- self.assertForeignKeyExists(
- AuthorCharFieldWithIndex, "char_field_id", "schema_author"
- )
- @skipUnlessDBFeature("supports_foreign_keys")
- @skipUnlessDBFeature("supports_index_on_text_field")
- def test_text_field_with_db_index_to_fk(self):
- # Create the table
- with connection.schema_editor() as editor:
- editor.create_model(Author)
- editor.create_model(AuthorTextFieldWithIndex)
- # Change TextField to FK
- old_field = AuthorTextFieldWithIndex._meta.get_field("text_field")
- new_field = ForeignKey(Author, CASCADE, blank=True)
- new_field.set_attributes_from_name("text_field")
- with connection.schema_editor() as editor:
- editor.alter_field(
- AuthorTextFieldWithIndex, old_field, new_field, strict=True
- )
- self.assertForeignKeyExists(
- AuthorTextFieldWithIndex, "text_field_id", "schema_author"
- )
- @isolate_apps("schema")
- def test_char_field_pk_to_auto_field(self):
- class Foo(Model):
- id = CharField(max_length=255, primary_key=True)
- class Meta:
- app_label = "schema"
- with connection.schema_editor() as editor:
- editor.create_model(Foo)
- self.isolated_local_models = [Foo]
- old_field = Foo._meta.get_field("id")
- new_field = AutoField(primary_key=True)
- new_field.set_attributes_from_name("id")
- new_field.model = Foo
- with connection.schema_editor() as editor:
- editor.alter_field(Foo, old_field, new_field, strict=True)
- @skipUnlessDBFeature("supports_foreign_keys")
- def test_fk_to_proxy(self):
- "Creating a FK to a proxy model creates database constraints."
- class AuthorProxy(Author):
- class Meta:
- app_label = "schema"
- apps = new_apps
- proxy = True
- class AuthorRef(Model):
- author = ForeignKey(AuthorProxy, on_delete=CASCADE)
- class Meta:
- app_label = "schema"
- apps = new_apps
- self.local_models = [AuthorProxy, AuthorRef]
- # Create the table
- with connection.schema_editor() as editor:
- editor.create_model(Author)
- editor.create_model(AuthorRef)
- self.assertForeignKeyExists(AuthorRef, "author_id", "schema_author")
- @skipUnlessDBFeature("supports_foreign_keys", "can_introspect_foreign_keys")
- def test_fk_db_constraint(self):
- "The db_constraint parameter is respected"
- # Create the table
- with connection.schema_editor() as editor:
- editor.create_model(Tag)
- editor.create_model(Author)
- editor.create_model(BookWeak)
- # Initial tables are there
- list(Author.objects.all())
- list(Tag.objects.all())
- list(BookWeak.objects.all())
- self.assertForeignKeyNotExists(BookWeak, "author_id", "schema_author")
- # Make a db_constraint=False FK
- new_field = ForeignKey(Tag, CASCADE, db_constraint=False)
- new_field.set_attributes_from_name("tag")
- with connection.schema_editor() as editor:
- editor.add_field(Author, new_field)
- self.assertForeignKeyNotExists(Author, "tag_id", "schema_tag")
- # Alter to one with a constraint
- new_field2 = ForeignKey(Tag, CASCADE)
- new_field2.set_attributes_from_name("tag")
- with connection.schema_editor() as editor:
- editor.alter_field(Author, new_field, new_field2, strict=True)
- self.assertForeignKeyExists(Author, "tag_id", "schema_tag")
- # Alter to one without a constraint again
- new_field2 = ForeignKey(Tag, CASCADE)
- new_field2.set_attributes_from_name("tag")
- with connection.schema_editor() as editor:
- editor.alter_field(Author, new_field2, new_field, strict=True)
- self.assertForeignKeyNotExists(Author, "tag_id", "schema_tag")
- @isolate_apps("schema")
- def test_no_db_constraint_added_during_primary_key_change(self):
- """
- When a primary key that's pointed to by a ForeignKey with
- db_constraint=False is altered, a foreign key constraint isn't added.
- """
- class Author(Model):
- class Meta:
- app_label = "schema"
- class BookWeak(Model):
- author = ForeignKey(Author, CASCADE, db_constraint=False)
- class Meta:
- app_label = "schema"
- with connection.schema_editor() as editor:
- editor.create_model(Author)
- editor.create_model(BookWeak)
- self.assertForeignKeyNotExists(BookWeak, "author_id", "schema_author")
- old_field = Author._meta.get_field("id")
- new_field = BigAutoField(primary_key=True)
- new_field.model = Author
- new_field.set_attributes_from_name("id")
- # @isolate_apps() and inner models are needed to have the model
- # relations populated, otherwise this doesn't act as a regression test.
- self.assertEqual(len(new_field.model._meta.related_objects), 1)
- with connection.schema_editor() as editor:
- editor.alter_field(Author, old_field, new_field, strict=True)
- self.assertForeignKeyNotExists(BookWeak, "author_id", "schema_author")
- def _test_m2m_db_constraint(self, M2MFieldClass):
- class LocalAuthorWithM2M(Model):
- name = CharField(max_length=255)
- class Meta:
- app_label = "schema"
- apps = new_apps
- self.local_models = [LocalAuthorWithM2M]
- # Create the table
- with connection.schema_editor() as editor:
- editor.create_model(Tag)
- editor.create_model(LocalAuthorWithM2M)
- # Initial tables are there
- list(LocalAuthorWithM2M.objects.all())
- list(Tag.objects.all())
- # Make a db_constraint=False FK
- new_field = M2MFieldClass(Tag, related_name="authors", db_constraint=False)
- new_field.contribute_to_class(LocalAuthorWithM2M, "tags")
- # Add the field
- with connection.schema_editor() as editor:
- editor.add_field(LocalAuthorWithM2M, new_field)
- self.assertForeignKeyNotExists(
- new_field.remote_field.through, "tag_id", "schema_tag"
- )
- @skipUnlessDBFeature("supports_foreign_keys")
- def test_m2m_db_constraint(self):
- self._test_m2m_db_constraint(ManyToManyField)
- @skipUnlessDBFeature("supports_foreign_keys")
- def test_m2m_db_constraint_custom(self):
- self._test_m2m_db_constraint(CustomManyToManyField)
- @skipUnlessDBFeature("supports_foreign_keys")
- def test_m2m_db_constraint_inherited(self):
- self._test_m2m_db_constraint(InheritedManyToManyField)
- def test_add_field(self):
- """
- Tests adding fields to models
- """
- # Create the table
- with connection.schema_editor() as editor:
- editor.create_model(Author)
- # Ensure there's no age field
- columns = self.column_classes(Author)
- self.assertNotIn("age", columns)
- # Add the new field
- new_field = IntegerField(null=True)
- new_field.set_attributes_from_name("age")
- with CaptureQueriesContext(
- connection
- ) as ctx, connection.schema_editor() as editor:
- editor.add_field(Author, new_field)
- drop_default_sql = editor.sql_alter_column_no_default % {
- "column": editor.quote_name(new_field.name),
- }
- self.assertFalse(
- any(drop_default_sql in query["sql"] for query in ctx.captured_queries)
- )
- # Table is not rebuilt.
- self.assertIs(
- any("CREATE TABLE" in query["sql"] for query in ctx.captured_queries), False
- )
- self.assertIs(
- any("DROP TABLE" in query["sql"] for query in ctx.captured_queries), False
- )
- columns = self.column_classes(Author)
- self.assertEqual(
- columns["age"][0],
- connection.features.introspected_field_types["IntegerField"],
- )
- self.assertTrue(columns["age"][1][6])
- def test_add_field_remove_field(self):
- """
- Adding a field and removing it removes all deferred sql referring to it.
- """
- with connection.schema_editor() as editor:
- # Create a table with a unique constraint on the slug field.
- editor.create_model(Tag)
- # Remove the slug column.
- editor.remove_field(Tag, Tag._meta.get_field("slug"))
- self.assertEqual(editor.deferred_sql, [])
- def test_add_field_temp_default(self):
- """
- Tests adding fields to models with a temporary default
- """
- # Create the table
- with connection.schema_editor() as editor:
- editor.create_model(Author)
- # Ensure there's no age field
- columns = self.column_classes(Author)
- self.assertNotIn("age", columns)
- # Add some rows of data
- Author.objects.create(name="Andrew", height=30)
- Author.objects.create(name="Andrea")
- # Add a not-null field
- new_field = CharField(max_length=30, default="Godwin")
- new_field.set_attributes_from_name("surname")
- with connection.schema_editor() as editor:
- editor.add_field(Author, new_field)
- columns = self.column_classes(Author)
- self.assertEqual(
- columns["surname"][0],
- connection.features.introspected_field_types["CharField"],
- )
- self.assertEqual(
- columns["surname"][1][6],
- connection.features.interprets_empty_strings_as_nulls,
- )
- def test_add_field_temp_default_boolean(self):
- """
- Tests adding fields to models with a temporary default where
- the default is False. (#21783)
- """
- # Create the table
- with connection.schema_editor() as editor:
- editor.create_model(Author)
- # Ensure there's no age field
- columns = self.column_classes(Author)
- self.assertNotIn("age", columns)
- # Add some rows of data
- Author.objects.create(name="Andrew", height=30)
- Author.objects.create(name="Andrea")
- # Add a not-null field
- new_field = BooleanField(default=False)
- new_field.set_attributes_from_name("awesome")
- with connection.schema_editor() as editor:
- editor.add_field(Author, new_field)
- columns = self.column_classes(Author)
- # BooleanField are stored as TINYINT(1) on MySQL.
- field_type = columns["awesome"][0]
- self.assertEqual(
- field_type, connection.features.introspected_field_types["BooleanField"]
- )
- def test_add_field_default_transform(self):
- """
- Tests adding fields to models with a default that is not directly
- valid in the database (#22581)
- """
- class TestTransformField(IntegerField):
- # Weird field that saves the count of items in its value
- def get_default(self):
- return self.default
- def get_prep_value(self, value):
- if value is None:
- return 0
- return len(value)
- # Create the table
- with connection.schema_editor() as editor:
- editor.create_model(Author)
- # Add some rows of data
- Author.objects.create(name="Andrew", height=30)
- Author.objects.create(name="Andrea")
- # Add the field with a default it needs to cast (to string in this case)
- new_field = TestTransformField(default={1: 2})
- new_field.set_attributes_from_name("thing")
- with connection.schema_editor() as editor:
- editor.add_field(Author, new_field)
- # Ensure the field is there
- columns = self.column_classes(Author)
- field_type, field_info = columns["thing"]
- self.assertEqual(
- field_type, connection.features.introspected_field_types["IntegerField"]
- )
- # Make sure the values were transformed correctly
- self.assertEqual(Author.objects.extra(where=["thing = 1"]).count(), 2)
- def test_add_field_o2o_nullable(self):
- with connection.schema_editor() as editor:
- editor.create_model(Author)
- editor.create_model(Note)
- new_field = OneToOneField(Note, CASCADE, null=True)
- new_field.set_attributes_from_name("note")
- with connection.schema_editor() as editor:
- editor.add_field(Author, new_field)
- columns = self.column_classes(Author)
- self.assertIn("note_id", columns)
- self.assertTrue(columns["note_id"][1][6])
- def test_add_field_binary(self):
- """
- Tests binary fields get a sane default (#22851)
- """
- # Create the table
- with connection.schema_editor() as editor:
- editor.create_model(Author)
- # Add the new field
- new_field = BinaryField(blank=True)
- new_field.set_attributes_from_name("bits")
- with connection.schema_editor() as editor:
- editor.add_field(Author, new_field)
- columns = self.column_classes(Author)
- # MySQL annoyingly uses the same backend, so it'll come back as one of
- # these two types.
- self.assertIn(columns["bits"][0], ("BinaryField", "TextField"))
- def test_add_field_durationfield_with_default(self):
- with connection.schema_editor() as editor:
- editor.create_model(Author)
- new_field = DurationField(default=datetime.timedelta(minutes=10))
- new_field.set_attributes_from_name("duration")
- with connection.schema_editor() as editor:
- editor.add_field(Author, new_field)
- columns = self.column_classes(Author)
- self.assertEqual(
- columns["duration"][0],
- connection.features.introspected_field_types["DurationField"],
- )
- @unittest.skipUnless(connection.vendor == "mysql", "MySQL specific")
- def test_add_binaryfield_mediumblob(self):
- """
- Test adding a custom-sized binary field on MySQL (#24846).
- """
- # Create the table
- with connection.schema_editor() as editor:
- editor.create_model(Author)
- # Add the new field with default
- new_field = MediumBlobField(blank=True, default=b"123")
- new_field.set_attributes_from_name("bits")
- with connection.schema_editor() as editor:
- editor.add_field(Author, new_field)
- columns = self.column_classes(Author)
- # Introspection treats BLOBs as TextFields
- self.assertEqual(columns["bits"][0], "TextField")
- def test_remove_field(self):
- with connection.schema_editor() as editor:
- editor.create_model(Author)
- with CaptureQueriesContext(connection) as ctx:
- editor.remove_field(Author, Author._meta.get_field("name"))
- columns = self.column_classes(Author)
- self.assertNotIn("name", columns)
- if getattr(connection.features, "can_alter_table_drop_column", True):
- # Table is not rebuilt.
- self.assertIs(
- any("CREATE TABLE" in query["sql"] for query in ctx.captured_queries),
- False,
- )
- self.assertIs(
- any("DROP TABLE" in query["sql"] for query in ctx.captured_queries),
- False,
- )
- def test_alter(self):
- """
- Tests simple altering of fields
- """
- # Create the table
- with connection.schema_editor() as editor:
- editor.create_model(Author)
- # Ensure the field is right to begin with
- columns = self.column_classes(Author)
- self.assertEqual(
- columns["name"][0],
- connection.features.introspected_field_types["CharField"],
- )
- self.assertEqual(
- bool(columns["name"][1][6]),
- bool(connection.features.interprets_empty_strings_as_nulls),
- )
- # Alter the name field to a TextField
- old_field = Author._meta.get_field("name")
- new_field = TextField(null=True)
- new_field.set_attributes_from_name("name")
- with connection.schema_editor() as editor:
- editor.alter_field(Author, old_field, new_field, strict=True)
- columns = self.column_classes(Author)
- self.assertEqual(columns["name"][0], "TextField")
- self.assertTrue(columns["name"][1][6])
- # Change nullability again
- new_field2 = TextField(null=False)
- new_field2.set_attributes_from_name("name")
- with connection.schema_editor() as editor:
- editor.alter_field(Author, new_field, new_field2, strict=True)
- columns = self.column_classes(Author)
- self.assertEqual(columns["name"][0], "TextField")
- self.assertEqual(
- bool(columns["name"][1][6]),
- bool(connection.features.interprets_empty_strings_as_nulls),
- )
- def test_alter_auto_field_to_integer_field(self):
- # Create the table
- with connection.schema_editor() as editor:
- editor.create_model(Author)
- # Change AutoField to IntegerField
- old_field = Author._meta.get_field("id")
- new_field = IntegerField(primary_key=True)
- new_field.set_attributes_from_name("id")
- new_field.model = Author
- with connection.schema_editor() as editor:
- editor.alter_field(Author, old_field, new_field, strict=True)
- # Now that ID is an IntegerField, the database raises an error if it
- # isn't provided.
- if not connection.features.supports_unspecified_pk:
- with self.assertRaises(DatabaseError):
- Author.objects.create()
- def test_alter_auto_field_to_char_field(self):
- # Create the table
- with connection.schema_editor() as editor:
- editor.create_model(Author)
- # Change AutoField to CharField
- old_field = Author._meta.get_field("id")
- new_field = CharField(primary_key=True, max_length=50)
- new_field.set_attributes_from_name("id")
- new_field.model = Author
- with connection.schema_editor() as editor:
- editor.alter_field(Author, old_field, new_field, strict=True)
- @isolate_apps("schema")
- def test_alter_auto_field_quoted_db_column(self):
- class Foo(Model):
- id = AutoField(primary_key=True, db_column='"quoted_id"')
- class Meta:
- app_label = "schema"
- with connection.schema_editor() as editor:
- editor.create_model(Foo)
- self.isolated_local_models = [Foo]
- old_field = Foo._meta.get_field("id")
- new_field = BigAutoField(primary_key=True)
- new_field.model = Foo
- new_field.db_column = '"quoted_id"'
- new_field.set_attributes_from_name("id")
- with connection.schema_editor() as editor:
- editor.alter_field(Foo, old_field, new_field, strict=True)
- Foo.objects.create()
- def test_alter_not_unique_field_to_primary_key(self):
- # Create the table.
- with connection.schema_editor() as editor:
- editor.create_model(Author)
- # Change UUIDField to primary key.
- old_field = Author._meta.get_field("uuid")
- new_field = UUIDField(primary_key=True)
- new_field.set_attributes_from_name("uuid")
- new_field.model = Author
- with connection.schema_editor() as editor:
- editor.remove_field(Author, Author._meta.get_field("id"))
- editor.alter_field(Author, old_field, new_field, strict=True)
- # Redundant unique constraint is not added.
- count = self.get_constraints_count(
- Author._meta.db_table,
- Author._meta.get_field("uuid").column,
- None,
- )
- self.assertLessEqual(count["uniques"], 1)
- @isolate_apps("schema")
- def test_alter_primary_key_quoted_db_table(self):
- class Foo(Model):
- class Meta:
- app_label = "schema"
- db_table = '"foo"'
- with connection.schema_editor() as editor:
- editor.create_model(Foo)
- self.isolated_local_models = [Foo]
- old_field = Foo._meta.get_field("id")
- new_field = BigAutoField(primary_key=True)
- new_field.model = Foo
- new_field.set_attributes_from_name("id")
- with connection.schema_editor() as editor:
- editor.alter_field(Foo, old_field, new_field, strict=True)
- Foo.objects.create()
- def test_alter_text_field(self):
- # Regression for "BLOB/TEXT column 'info' can't have a default value")
- # on MySQL.
- # Create the table
- with connection.schema_editor() as editor:
- editor.create_model(Note)
- old_field = Note._meta.get_field("info")
- new_field = TextField(blank=True)
- new_field.set_attributes_from_name("info")
- with connection.schema_editor() as editor:
- editor.alter_field(Note, old_field, new_field, strict=True)
- def test_alter_text_field_to_not_null_with_default_value(self):
- with connection.schema_editor() as editor:
- editor.create_model(Note)
- old_field = Note._meta.get_field("address")
- new_field = TextField(blank=True, default="", null=False)
- new_field.set_attributes_from_name("address")
- with connection.schema_editor() as editor:
- editor.alter_field(Note, old_field, new_field, strict=True)
- @skipUnlessDBFeature("can_defer_constraint_checks", "can_rollback_ddl")
- def test_alter_fk_checks_deferred_constraints(self):
- """
- #25492 - Altering a foreign key's structure and data in the same
- transaction.
- """
- with connection.schema_editor() as editor:
- editor.create_model(Node)
- old_field = Node._meta.get_field("parent")
- new_field = ForeignKey(Node, CASCADE)
- new_field.set_attributes_from_name("parent")
- parent = Node.objects.create()
- with connection.schema_editor() as editor:
- # Update the parent FK to create a deferred constraint check.
- Node.objects.update(parent=parent)
- editor.alter_field(Node, old_field, new_field, strict=True)
- @isolate_apps("schema")
- def test_alter_null_with_default_value_deferred_constraints(self):
- class Publisher(Model):
- class Meta:
- app_label = "schema"
- class Article(Model):
- publisher = ForeignKey(Publisher, CASCADE)
- title = CharField(max_length=50, null=True)
- description = CharField(max_length=100, null=True)
- class Meta:
- app_label = "schema"
- with connection.schema_editor() as editor:
- editor.create_model(Publisher)
- editor.create_model(Article)
- self.isolated_local_models = [Article, Publisher]
- publisher = Publisher.objects.create()
- Article.objects.create(publisher=publisher)
- old_title = Article._meta.get_field("title")
- new_title = CharField(max_length=50, null=False, default="")
- new_title.set_attributes_from_name("title")
- old_description = Article._meta.get_field("description")
- new_description = CharField(max_length=100, null=False, default="")
- new_description.set_attributes_from_name("description")
- with connection.schema_editor() as editor:
- editor.alter_field(Article, old_title, new_title, strict=True)
- editor.alter_field(Article, old_description, new_description, strict=True)
- def test_alter_text_field_to_date_field(self):
- """
- #25002 - Test conversion of text field to date field.
- """
- with connection.schema_editor() as editor:
- editor.create_model(Note)
- Note.objects.create(info="1988-05-05")
- old_field = Note._meta.get_field("info")
- new_field = DateField(blank=True)
- new_field.set_attributes_from_name("info")
- with connection.schema_editor() as editor:
- editor.alter_field(Note, old_field, new_field, strict=True)
- # Make sure the field isn't nullable
- columns = self.column_classes(Note)
- self.assertFalse(columns["info"][1][6])
- def test_alter_text_field_to_datetime_field(self):
- """
- #25002 - Test conversion of text field to datetime field.
- """
- with connection.schema_editor() as editor:
- editor.create_model(Note)
- Note.objects.create(info="1988-05-05 3:16:17.4567")
- old_field = Note._meta.get_field("info")
- new_field = DateTimeField(blank=True)
- new_field.set_attributes_from_name("info")
- with connection.schema_editor() as editor:
- editor.alter_field(Note, old_field, new_field, strict=True)
- # Make sure the field isn't nullable
- columns = self.column_classes(Note)
- self.assertFalse(columns["info"][1][6])
- def test_alter_text_field_to_time_field(self):
- """
- #25002 - Test conversion of text field to time field.
- """
- with connection.schema_editor() as editor:
- editor.create_model(Note)
- Note.objects.create(info="3:16:17.4567")
- old_field = Note._meta.get_field("info")
- new_field = TimeField(blank=True)
- new_field.set_attributes_from_name("info")
- with connection.schema_editor() as editor:
- editor.alter_field(Note, old_field, new_field, strict=True)
- # Make sure the field isn't nullable
- columns = self.column_classes(Note)
- self.assertFalse(columns["info"][1][6])
- @skipIfDBFeature("interprets_empty_strings_as_nulls")
- def test_alter_textual_field_keep_null_status(self):
- """
- Changing a field type shouldn't affect the not null status.
- """
- with connection.schema_editor() as editor:
- editor.create_model(Note)
- with self.assertRaises(IntegrityError):
- Note.objects.create(info=None)
- old_field = Note._meta.get_field("info")
- new_field = CharField(max_length=50)
- new_field.set_attributes_from_name("info")
- with connection.schema_editor() as editor:
- editor.alter_field(Note, old_field, new_field, strict=True)
- with self.assertRaises(IntegrityError):
- Note.objects.create(info=None)
- @skipUnlessDBFeature("interprets_empty_strings_as_nulls")
- def test_alter_textual_field_not_null_to_null(self):
- """
- Nullability for textual fields is preserved on databases that
- interpret empty strings as NULLs.
- """
- with connection.schema_editor() as editor:
- editor.create_model(Author)
- columns = self.column_classes(Author)
- # Field is nullable.
- self.assertTrue(columns["uuid"][1][6])
- # Change to NOT NULL.
- old_field = Author._meta.get_field("uuid")
- new_field = SlugField(null=False, blank=True)
- new_field.set_attributes_from_name("uuid")
- with connection.schema_editor() as editor:
- editor.alter_field(Author, old_field, new_field, strict=True)
- columns = self.column_classes(Author)
- # Nullability is preserved.
- self.assertTrue(columns["uuid"][1][6])
- def test_alter_numeric_field_keep_null_status(self):
- """
- Changing a field type shouldn't affect the not null status.
- """
- with connection.schema_editor() as editor:
- editor.create_model(UniqueTest)
- with self.assertRaises(IntegrityError):
- UniqueTest.objects.create(year=None, slug="aaa")
- old_field = UniqueTest._meta.get_field("year")
- new_field = BigIntegerField()
- new_field.set_attributes_from_name("year")
- with connection.schema_editor() as editor:
- editor.alter_field(UniqueTest, old_field, new_field, strict=True)
- with self.assertRaises(IntegrityError):
- UniqueTest.objects.create(year=None, slug="bbb")
- def test_alter_null_to_not_null(self):
- """
- #23609 - Tests handling of default values when altering from NULL to NOT NULL.
- """
- # Create the table
- with connection.schema_editor() as editor:
- editor.create_model(Author)
- # Ensure the field is right to begin with
- columns = self.column_classes(Author)
- self.assertTrue(columns["height"][1][6])
- # Create some test data
- Author.objects.create(name="Not null author", height=12)
- Author.objects.create(name="Null author")
- # Verify null value
- self.assertEqual(Author.objects.get(name="Not null author").height, 12)
- self.assertIsNone(Author.objects.get(name="Null author").height)
- # Alter the height field to NOT NULL with default
- old_field = Author._meta.get_field("height")
- new_field = PositiveIntegerField(default=42)
- new_field.set_attributes_from_name("height")
- with connection.schema_editor() as editor:
- editor.alter_field(Author, old_field, new_field, strict=True)
- columns = self.column_classes(Author)
- self.assertFalse(columns["height"][1][6])
- # Verify default value
- self.assertEqual(Author.objects.get(name="Not null author").height, 12)
- self.assertEqual(Author.objects.get(name="Null author").height, 42)
- def test_alter_charfield_to_null(self):
- """
- #24307 - Should skip an alter statement on databases with
- interprets_empty_strings_as_nulls when changing a CharField to null.
- """
- # Create the table
- with connection.schema_editor() as editor:
- editor.create_model(Author)
- # Change the CharField to null
- old_field = Author._meta.get_field("name")
- new_field = copy(old_field)
- new_field.null = True
- with connection.schema_editor() as editor:
- editor.alter_field(Author, old_field, new_field, strict=True)
- @unittest.skipUnless(connection.vendor == "postgresql", "PostgreSQL specific")
- def test_alter_char_field_decrease_length(self):
- # Create the table.
- with connection.schema_editor() as editor:
- editor.create_model(Author)
- Author.objects.create(name="x" * 255)
- # Change max_length of CharField.
- old_field = Author._meta.get_field("name")
- new_field = CharField(max_length=254)
- new_field.set_attributes_from_name("name")
- with connection.schema_editor() as editor:
- msg = "value too long for type character varying(254)"
- with self.assertRaisesMessage(DataError, msg):
- editor.alter_field(Author, old_field, new_field, strict=True)
- @unittest.skipUnless(connection.vendor == "postgresql", "PostgreSQL specific")
- def test_alter_field_with_custom_db_type(self):
- from django.contrib.postgres.fields import ArrayField
- class Foo(Model):
- field = ArrayField(CharField(max_length=255))
- class Meta:
- app_label = "schema"
- with connection.schema_editor() as editor:
- editor.create_model(Foo)
- self.isolated_local_models = [Foo]
- old_field = Foo._meta.get_field("field")
- new_field = ArrayField(CharField(max_length=16))
- new_field.set_attributes_from_name("field")
- new_field.model = Foo
- with connection.schema_editor() as editor:
- editor.alter_field(Foo, old_field, new_field, strict=True)
- @isolate_apps("schema")
- @unittest.skipUnless(connection.vendor == "postgresql", "PostgreSQL specific")
- def test_alter_array_field_decrease_base_field_length(self):
- from django.contrib.postgres.fields import ArrayField
- class ArrayModel(Model):
- field = ArrayField(CharField(max_length=16))
- class Meta:
- app_label = "schema"
- with connection.schema_editor() as editor:
- editor.create_model(ArrayModel)
- self.isolated_local_models = [ArrayModel]
- ArrayModel.objects.create(field=["x" * 16])
- old_field = ArrayModel._meta.get_field("field")
- new_field = ArrayField(CharField(max_length=15))
- new_field.set_attributes_from_name("field")
- new_field.model = ArrayModel
- with connection.schema_editor() as editor:
- msg = "value too long for type character varying(15)"
- with self.assertRaisesMessage(DataError, msg):
- editor.alter_field(ArrayModel, old_field, new_field, strict=True)
- @isolate_apps("schema")
- @unittest.skipUnless(connection.vendor == "postgresql", "PostgreSQL specific")
- def test_alter_array_field_decrease_nested_base_field_length(self):
- from django.contrib.postgres.fields import ArrayField
- class ArrayModel(Model):
- field = ArrayField(ArrayField(CharField(max_length=16)))
- class Meta:
- app_label = "schema"
- with connection.schema_editor() as editor:
- editor.create_model(ArrayModel)
- self.isolated_local_models = [ArrayModel]
- ArrayModel.objects.create(field=[["x" * 16]])
- old_field = ArrayModel._meta.get_field("field")
- new_field = ArrayField(ArrayField(CharField(max_length=15)))
- new_field.set_attributes_from_name("field")
- new_field.model = ArrayModel
- with connection.schema_editor() as editor:
- msg = "value too long for type character varying(15)"
- with self.assertRaisesMessage(DataError, msg):
- editor.alter_field(ArrayModel, old_field, new_field, strict=True)
- def test_alter_textfield_to_null(self):
- """
- #24307 - Should skip an alter statement on databases with
- interprets_empty_strings_as_nulls when changing a TextField to null.
- """
- # Create the table
- with connection.schema_editor() as editor:
- editor.create_model(Note)
- # Change the TextField to null
- old_field = Note._meta.get_field("info")
- new_field = copy(old_field)
- new_field.null = True
- with connection.schema_editor() as editor:
- editor.alter_field(Note, old_field, new_field, strict=True)
- def test_alter_null_to_not_null_keeping_default(self):
- """
- #23738 - Can change a nullable field with default to non-nullable
- with the same default.
- """
- # Create the table
- with connection.schema_editor() as editor:
- editor.create_model(AuthorWithDefaultHeight)
- # Ensure the field is right to begin with
- columns = self.column_classes(AuthorWithDefaultHeight)
- self.assertTrue(columns["height"][1][6])
- # Alter the height field to NOT NULL keeping the previous default
- old_field = AuthorWithDefaultHeight._meta.get_field("height")
- new_field = PositiveIntegerField(default=42)
- new_field.set_attributes_from_name("height")
- with connection.schema_editor() as editor:
- editor.alter_field(
- AuthorWithDefaultHeight, old_field, new_field, strict=True
- )
- columns = self.column_classes(AuthorWithDefaultHeight)
- self.assertFalse(columns["height"][1][6])
- @skipUnlessDBFeature("supports_foreign_keys")
- def test_alter_fk(self):
- """
- Tests altering of FKs
- """
- # Create the table
- with connection.schema_editor() as editor:
- editor.create_model(Author)
- editor.create_model(Book)
- # Ensure the field is right to begin with
- columns = self.column_classes(Book)
- self.assertEqual(
- columns["author_id"][0],
- connection.features.introspected_field_types["IntegerField"],
- )
- self.assertForeignKeyExists(Book, "author_id", "schema_author")
- # Alter the FK
- old_field = Book._meta.get_field("author")
- new_field = ForeignKey(Author, CASCADE, editable=False)
- new_field.set_attributes_from_name("author")
- with connection.schema_editor() as editor:
- editor.alter_field(Book, old_field, new_field, strict=True)
- columns = self.column_classes(Book)
- self.assertEqual(
- columns["author_id"][0],
- connection.features.introspected_field_types["IntegerField"],
- )
- self.assertForeignKeyExists(Book, "author_id", "schema_author")
- @skipUnlessDBFeature("supports_foreign_keys")
- def test_alter_to_fk(self):
- """
- #24447 - Tests adding a FK constraint for an existing column
- """
- class LocalBook(Model):
- author = IntegerField()
- title = CharField(max_length=100, db_index=True)
- pub_date = DateTimeField()
- class Meta:
- app_label = "schema"
- apps = new_apps
- self.local_models = [LocalBook]
- # Create the tables
- with connection.schema_editor() as editor:
- editor.create_model(Author)
- editor.create_model(LocalBook)
- # Ensure no FK constraint exists
- constraints = self.get_constraints(LocalBook._meta.db_table)
- for details in constraints.values():
- if details["foreign_key"]:
- self.fail(
- "Found an unexpected FK constraint to %s" % details["columns"]
- )
- old_field = LocalBook._meta.get_field("author")
- new_field = ForeignKey(Author, CASCADE)
- new_field.set_attributes_from_name("author")
- with connection.schema_editor() as editor:
- editor.alter_field(LocalBook, old_field, new_field, strict=True)
- self.assertForeignKeyExists(LocalBook, "author_id", "schema_author")
- @skipUnlessDBFeature("supports_foreign_keys", "can_introspect_foreign_keys")
- def test_alter_o2o_to_fk(self):
- """
- #24163 - Tests altering of OneToOneField to ForeignKey
- """
- # Create the table
- with connection.schema_editor() as editor:
- editor.create_model(Author)
- editor.create_model(BookWithO2O)
- # Ensure the field is right to begin with
- columns = self.column_classes(BookWithO2O)
- self.assertEqual(
- columns["author_id"][0],
- connection.features.introspected_field_types["IntegerField"],
- )
- # Ensure the field is unique
- author = Author.objects.create(name="Joe")
- BookWithO2O.objects.create(
- author=author, title="Django 1", pub_date=datetime.datetime.now()
- )
- with self.assertRaises(IntegrityError):
- BookWithO2O.objects.create(
- author=author, title="Django 2", pub_date=datetime.datetime.now()
- )
- BookWithO2O.objects.all().delete()
- self.assertForeignKeyExists(BookWithO2O, "author_id", "schema_author")
- # Alter the OneToOneField to ForeignKey
- old_field = BookWithO2O._meta.get_field("author")
- new_field = ForeignKey(Author, CASCADE)
- new_field.set_attributes_from_name("author")
- with connection.schema_editor() as editor:
- editor.alter_field(BookWithO2O, old_field, new_field, strict=True)
- columns = self.column_classes(Book)
- self.assertEqual(
- columns["author_id"][0],
- connection.features.introspected_field_types["IntegerField"],
- )
- # Ensure the field is not unique anymore
- Book.objects.create(
- author=author, title="Django 1", pub_date=datetime.datetime.now()
- )
- Book.objects.create(
- author=author, title="Django 2", pub_date=datetime.datetime.now()
- )
- self.assertForeignKeyExists(Book, "author_id", "schema_author")
- @skipUnlessDBFeature("supports_foreign_keys", "can_introspect_foreign_keys")
- def test_alter_fk_to_o2o(self):
- """
- #24163 - Tests altering of ForeignKey to OneToOneField
- """
- # Create the table
- with connection.schema_editor() as editor:
- editor.create_model(Author)
- editor.create_model(Book)
- # Ensure the field is right to begin with
- columns = self.column_classes(Book)
- self.assertEqual(
- columns["author_id"][0],
- connection.features.introspected_field_types["IntegerField"],
- )
- # Ensure the field is not unique
- author = Author.objects.create(name="Joe")
- Book.objects.create(
- author=author, title="Django 1", pub_date=datetime.datetime.now()
- )
- Book.objects.create(
- author=author, title="Django 2", pub_date=datetime.datetime.now()
- )
- Book.objects.all().delete()
- self.assertForeignKeyExists(Book, "author_id", "schema_author")
- # Alter the ForeignKey to OneToOneField
- old_field = Book._meta.get_field("author")
- new_field = OneToOneField(Author, CASCADE)
- new_field.set_attributes_from_name("author")
- with connection.schema_editor() as editor:
- editor.alter_field(Book, old_field, new_field, strict=True)
- columns = self.column_classes(BookWithO2O)
- self.assertEqual(
- columns["author_id"][0],
- connection.features.introspected_field_types["IntegerField"],
- )
- # Ensure the field is unique now
- BookWithO2O.objects.create(
- author=author, title="Django 1", pub_date=datetime.datetime.now()
- )
- with self.assertRaises(IntegrityError):
- BookWithO2O.objects.create(
- author=author, title="Django 2", pub_date=datetime.datetime.now()
- )
- self.assertForeignKeyExists(BookWithO2O, "author_id", "schema_author")
- def test_alter_field_fk_to_o2o(self):
- with connection.schema_editor() as editor:
- editor.create_model(Author)
- editor.create_model(Book)
- expected_fks = (
- 1
- if connection.features.supports_foreign_keys
- and connection.features.can_introspect_foreign_keys
- else 0
- )
- expected_indexes = 1 if connection.features.indexes_foreign_keys else 0
- # Check the index is right to begin with.
- counts = self.get_constraints_count(
- Book._meta.db_table,
- Book._meta.get_field("author").column,
- (Author._meta.db_table, Author._meta.pk.column),
- )
- self.assertEqual(
- counts,
- {"fks": expected_fks, "uniques": 0, "indexes": expected_indexes},
- )
- old_field = Book._meta.get_field("author")
- new_field = OneToOneField(Author, CASCADE)
- new_field.set_attributes_from_name("author")
- with connection.schema_editor() as editor:
- editor.alter_field(Book, old_field, new_field)
- counts = self.get_constraints_count(
- Book._meta.db_table,
- Book._meta.get_field("author").column,
- (Author._meta.db_table, Author._meta.pk.column),
- )
- # The index on ForeignKey is replaced with a unique constraint for
- # OneToOneField.
- self.assertEqual(counts, {"fks": expected_fks, "uniques": 1, "indexes": 0})
- def test_alter_field_fk_keeps_index(self):
- with connection.schema_editor() as editor:
- editor.create_model(Author)
- editor.create_model(Book)
- expected_fks = (
- 1
- if connection.features.supports_foreign_keys
- and connection.features.can_introspect_foreign_keys
- else 0
- )
- expected_indexes = 1 if connection.features.indexes_foreign_keys else 0
- # Check the index is right to begin with.
- counts = self.get_constraints_count(
- Book._meta.db_table,
- Book._meta.get_field("author").column,
- (Author._meta.db_table, Author._meta.pk.column),
- )
- self.assertEqual(
- counts,
- {"fks": expected_fks, "uniques": 0, "indexes": expected_indexes},
- )
- old_field = Book._meta.get_field("author")
- # on_delete changed from CASCADE.
- new_field = ForeignKey(Author, PROTECT)
- new_field.set_attributes_from_name("author")
- with connection.schema_editor() as editor:
- editor.alter_field(Book, old_field, new_field, strict=True)
- counts = self.get_constraints_count(
- Book._meta.db_table,
- Book._meta.get_field("author").column,
- (Author._meta.db_table, Author._meta.pk.column),
- )
- # The index remains.
- self.assertEqual(
- counts,
- {"fks": expected_fks, "uniques": 0, "indexes": expected_indexes},
- )
- def test_alter_field_o2o_to_fk(self):
- with connection.schema_editor() as editor:
- editor.create_model(Author)
- editor.create_model(BookWithO2O)
- expected_fks = (
- 1
- if connection.features.supports_foreign_keys
- and connection.features.can_introspect_foreign_keys
- else 0
- )
- # Check the unique constraint is right to begin with.
- counts = self.get_constraints_count(
- BookWithO2O._meta.db_table,
- BookWithO2O._meta.get_field("author").column,
- (Author._meta.db_table, Author._meta.pk.column),
- )
- self.assertEqual(counts, {"fks": expected_fks, "uniques": 1, "indexes": 0})
- old_field = BookWithO2O._meta.get_field("author")
- new_field = ForeignKey(Author, CASCADE)
- new_field.set_attributes_from_name("author")
- with connection.schema_editor() as editor:
- editor.alter_field(BookWithO2O, old_field, new_field)
- counts = self.get_constraints_count(
- BookWithO2O._meta.db_table,
- BookWithO2O._meta.get_field("author").column,
- (Author._meta.db_table, Author._meta.pk.column),
- )
- # The unique constraint on OneToOneField is replaced with an index for
- # ForeignKey.
- self.assertEqual(counts, {"fks": expected_fks, "uniques": 0, "indexes": 1})
- def test_alter_field_o2o_keeps_unique(self):
- with connection.schema_editor() as editor:
- editor.create_model(Author)
- editor.create_model(BookWithO2O)
- expected_fks = (
- 1
- if connection.features.supports_foreign_keys
- and connection.features.can_introspect_foreign_keys
- else 0
- )
- # Check the unique constraint is right to begin with.
- counts = self.get_constraints_count(
- BookWithO2O._meta.db_table,
- BookWithO2O._meta.get_field("author").column,
- (Author._meta.db_table, Author._meta.pk.column),
- )
- self.assertEqual(counts, {"fks": expected_fks, "uniques": 1, "indexes": 0})
- old_field = BookWithO2O._meta.get_field("author")
- # on_delete changed from CASCADE.
- new_field = OneToOneField(Author, PROTECT)
- new_field.set_attributes_from_name("author")
- with connection.schema_editor() as editor:
- editor.alter_field(BookWithO2O, old_field, new_field, strict=True)
- counts = self.get_constraints_count(
- BookWithO2O._meta.db_table,
- BookWithO2O._meta.get_field("author").column,
- (Author._meta.db_table, Author._meta.pk.column),
- )
- # The unique constraint remains.
- self.assertEqual(counts, {"fks": expected_fks, "uniques": 1, "indexes": 0})
- @skipUnlessDBFeature("ignores_table_name_case")
- def test_alter_db_table_case(self):
- # Create the table
- with connection.schema_editor() as editor:
- editor.create_model(Author)
- # Alter the case of the table
- old_table_name = Author._meta.db_table
- with connection.schema_editor() as editor:
- editor.alter_db_table(Author, old_table_name, old_table_name.upper())
- def test_alter_implicit_id_to_explicit(self):
- """
- Should be able to convert an implicit "id" field to an explicit "id"
- primary key field.
- """
- with connection.schema_editor() as editor:
- editor.create_model(Author)
- old_field = Author._meta.get_field("id")
- new_field = AutoField(primary_key=True)
- new_field.set_attributes_from_name("id")
- new_field.model = Author
- with connection.schema_editor() as editor:
- editor.alter_field(Author, old_field, new_field, strict=True)
- # This will fail if DROP DEFAULT is inadvertently executed on this
- # field which drops the id sequence, at least on PostgreSQL.
- Author.objects.create(name="Foo")
- Author.objects.create(name="Bar")
- def test_alter_autofield_pk_to_bigautofield_pk(self):
- with connection.schema_editor() as editor:
- editor.create_model(Author)
- old_field = Author._meta.get_field("id")
- new_field = BigAutoField(primary_key=True)
- new_field.set_attributes_from_name("id")
- new_field.model = Author
- with connection.schema_editor() as editor:
- editor.alter_field(Author, old_field, new_field, strict=True)
- Author.objects.create(name="Foo", pk=1)
- with connection.cursor() as cursor:
- sequence_reset_sqls = connection.ops.sequence_reset_sql(
- no_style(), [Author]
- )
- if sequence_reset_sqls:
- cursor.execute(sequence_reset_sqls[0])
- self.assertIsNotNone(Author.objects.create(name="Bar"))
- def test_alter_autofield_pk_to_smallautofield_pk(self):
- with connection.schema_editor() as editor:
- editor.create_model(Author)
- old_field = Author._meta.get_field("id")
- new_field = SmallAutoField(primary_key=True)
- new_field.set_attributes_from_name("id")
- new_field.model = Author
- with connection.schema_editor() as editor:
- editor.alter_field(Author, old_field, new_field, strict=True)
- Author.objects.create(name="Foo", pk=1)
- with connection.cursor() as cursor:
- sequence_reset_sqls = connection.ops.sequence_reset_sql(
- no_style(), [Author]
- )
- if sequence_reset_sqls:
- cursor.execute(sequence_reset_sqls[0])
- self.assertIsNotNone(Author.objects.create(name="Bar"))
- def test_alter_int_pk_to_autofield_pk(self):
- """
- Should be able to rename an IntegerField(primary_key=True) to
- AutoField(primary_key=True).
- """
- with connection.schema_editor() as editor:
- editor.create_model(IntegerPK)
- old_field = IntegerPK._meta.get_field("i")
- new_field = AutoField(primary_key=True)
- new_field.model = IntegerPK
- new_field.set_attributes_from_name("i")
- with connection.schema_editor() as editor:
- editor.alter_field(IntegerPK, old_field, new_field, strict=True)
- # A model representing the updated model.
- class IntegerPKToAutoField(Model):
- i = AutoField(primary_key=True)
- j = IntegerField(unique=True)
- class Meta:
- app_label = "schema"
- apps = new_apps
- db_table = IntegerPK._meta.db_table
- # An id (i) is generated by the database.
- obj = IntegerPKToAutoField.objects.create(j=1)
- self.assertIsNotNone(obj.i)
- def test_alter_int_pk_to_bigautofield_pk(self):
- """
- Should be able to rename an IntegerField(primary_key=True) to
- BigAutoField(primary_key=True).
- """
- with connection.schema_editor() as editor:
- editor.create_model(IntegerPK)
- old_field = IntegerPK._meta.get_field("i")
- new_field = BigAutoField(primary_key=True)
- new_field.model = IntegerPK
- new_field.set_attributes_from_name("i")
- with connection.schema_editor() as editor:
- editor.alter_field(IntegerPK, old_field, new_field, strict=True)
- # A model representing the updated model.
- class IntegerPKToBigAutoField(Model):
- i = BigAutoField(primary_key=True)
- j = IntegerField(unique=True)
- class Meta:
- app_label = "schema"
- apps = new_apps
- db_table = IntegerPK._meta.db_table
- # An id (i) is generated by the database.
- obj = IntegerPKToBigAutoField.objects.create(j=1)
- self.assertIsNotNone(obj.i)
- @isolate_apps("schema")
- def test_alter_smallint_pk_to_smallautofield_pk(self):
- """
- Should be able to rename an SmallIntegerField(primary_key=True) to
- SmallAutoField(primary_key=True).
- """
- class SmallIntegerPK(Model):
- i = SmallIntegerField(primary_key=True)
- class Meta:
- app_label = "schema"
- with connection.schema_editor() as editor:
- editor.create_model(SmallIntegerPK)
- self.isolated_local_models = [SmallIntegerPK]
- old_field = SmallIntegerPK._meta.get_field("i")
- new_field = SmallAutoField(primary_key=True)
- new_field.model = SmallIntegerPK
- new_field.set_attributes_from_name("i")
- with connection.schema_editor() as editor:
- editor.alter_field(SmallIntegerPK, old_field, new_field, strict=True)
- def test_alter_int_pk_to_int_unique(self):
- """
- Should be able to rename an IntegerField(primary_key=True) to
- IntegerField(unique=True).
- """
- with connection.schema_editor() as editor:
- editor.create_model(IntegerPK)
- # Delete the old PK
- old_field = IntegerPK._meta.get_field("i")
- new_field = IntegerField(unique=True)
- new_field.model = IntegerPK
- new_field.set_attributes_from_name("i")
- with connection.schema_editor() as editor:
- editor.alter_field(IntegerPK, old_field, new_field, strict=True)
- # The primary key constraint is gone. Result depends on database:
- # 'id' for SQLite, None for others (must not be 'i').
- self.assertIn(self.get_primary_key(IntegerPK._meta.db_table), ("id", None))
- # Set up a model class as it currently stands. The original IntegerPK
- # class is now out of date and some backends make use of the whole
- # model class when modifying a field (such as sqlite3 when remaking a
- # table) so an outdated model class leads to incorrect results.
- class Transitional(Model):
- i = IntegerField(unique=True)
- j = IntegerField(unique=True)
- class Meta:
- app_label = "schema"
- apps = new_apps
- db_table = "INTEGERPK"
- # model requires a new PK
- old_field = Transitional._meta.get_field("j")
- new_field = IntegerField(primary_key=True)
- new_field.model = Transitional
- new_field.set_attributes_from_name("j")
- with connection.schema_editor() as editor:
- editor.alter_field(Transitional, old_field, new_field, strict=True)
- # Create a model class representing the updated model.
- class IntegerUnique(Model):
- i = IntegerField(unique=True)
- j = IntegerField(primary_key=True)
- class Meta:
- app_label = "schema"
- apps = new_apps
- db_table = "INTEGERPK"
- # Ensure unique constraint works.
- IntegerUnique.objects.create(i=1, j=1)
- with self.assertRaises(IntegrityError):
- IntegerUnique.objects.create(i=1, j=2)
- def test_rename(self):
- """
- Tests simple altering of fields
- """
- # Create the table
- with connection.schema_editor() as editor:
- editor.create_model(Author)
- # Ensure the field is right to begin with
- columns = self.column_classes(Author)
- self.assertEqual(
- columns["name"][0],
- connection.features.introspected_field_types["CharField"],
- )
- self.assertNotIn("display_name", columns)
- # Alter the name field's name
- old_field = Author._meta.get_field("name")
- new_field = CharField(max_length=254)
- new_field.set_attributes_from_name("display_name")
- with connection.schema_editor() as editor:
- editor.alter_field(Author, old_field, new_field, strict=True)
- columns = self.column_classes(Author)
- self.assertEqual(
- columns["display_name"][0],
- connection.features.introspected_field_types["CharField"],
- )
- self.assertNotIn("name", columns)
- @isolate_apps("schema")
- def test_rename_referenced_field(self):
- class Author(Model):
- name = CharField(max_length=255, unique=True)
- class Meta:
- app_label = "schema"
- class Book(Model):
- author = ForeignKey(Author, CASCADE, to_field="name")
- class Meta:
- app_label = "schema"
- with connection.schema_editor() as editor:
- editor.create_model(Author)
- editor.create_model(Book)
- new_field = CharField(max_length=255, unique=True)
- new_field.set_attributes_from_name("renamed")
- with connection.schema_editor(
- atomic=connection.features.supports_atomic_references_rename
- ) as editor:
- editor.alter_field(Author, Author._meta.get_field("name"), new_field)
- # Ensure the foreign key reference was updated.
- self.assertForeignKeyExists(Book, "author_id", "schema_author", "renamed")
- @skipIfDBFeature("interprets_empty_strings_as_nulls")
- def test_rename_keep_null_status(self):
- """
- Renaming a field shouldn't affect the not null status.
- """
- with connection.schema_editor() as editor:
- editor.create_model(Note)
- with self.assertRaises(IntegrityError):
- Note.objects.create(info=None)
- old_field = Note._meta.get_field("info")
- new_field = TextField()
- new_field.set_attributes_from_name("detail_info")
- with connection.schema_editor() as editor:
- editor.alter_field(Note, old_field, new_field, strict=True)
- columns = self.column_classes(Note)
- self.assertEqual(columns["detail_info"][0], "TextField")
- self.assertNotIn("info", columns)
- with self.assertRaises(IntegrityError):
- NoteRename.objects.create(detail_info=None)
- def _test_m2m_create(self, M2MFieldClass):
- """
- Tests M2M fields on models during creation
- """
- class LocalBookWithM2M(Model):
- author = ForeignKey(Author, CASCADE)
- title = CharField(max_length=100, db_index=True)
- pub_date = DateTimeField()
- tags = M2MFieldClass("TagM2MTest", related_name="books")
- class Meta:
- app_label = "schema"
- apps = new_apps
- self.local_models = [LocalBookWithM2M]
- # Create the tables
- with connection.schema_editor() as editor:
- editor.create_model(Author)
- editor.create_model(TagM2MTest)
- editor.create_model(LocalBookWithM2M)
- # Ensure there is now an m2m table there
- columns = self.column_classes(
- LocalBookWithM2M._meta.get_field("tags").remote_field.through
- )
- self.assertEqual(
- columns["tagm2mtest_id"][0],
- connection.features.introspected_field_types["IntegerField"],
- )
- def test_m2m_create(self):
- self._test_m2m_create(ManyToManyField)
- def test_m2m_create_custom(self):
- self._test_m2m_create(CustomManyToManyField)
- def test_m2m_create_inherited(self):
- self._test_m2m_create(InheritedManyToManyField)
- def _test_m2m_create_through(self, M2MFieldClass):
- """
- Tests M2M fields on models during creation with through models
- """
- class LocalTagThrough(Model):
- book = ForeignKey("schema.LocalBookWithM2MThrough", CASCADE)
- tag = ForeignKey("schema.TagM2MTest", CASCADE)
- class Meta:
- app_label = "schema"
- apps = new_apps
- class LocalBookWithM2MThrough(Model):
- tags = M2MFieldClass(
- "TagM2MTest", related_name="books", through=LocalTagThrough
- )
- class Meta:
- app_label = "schema"
- apps = new_apps
- self.local_models = [LocalTagThrough, LocalBookWithM2MThrough]
- # Create the tables
- with connection.schema_editor() as editor:
- editor.create_model(LocalTagThrough)
- editor.create_model(TagM2MTest)
- editor.create_model(LocalBookWithM2MThrough)
- # Ensure there is now an m2m table there
- columns = self.column_classes(LocalTagThrough)
- self.assertEqual(
- columns["book_id"][0],
- connection.features.introspected_field_types["IntegerField"],
- )
- self.assertEqual(
- columns["tag_id"][0],
- connection.features.introspected_field_types["IntegerField"],
- )
- def test_m2m_create_through(self):
- self._test_m2m_create_through(ManyToManyField)
- def test_m2m_create_through_custom(self):
- self._test_m2m_create_through(CustomManyToManyField)
- def test_m2m_create_through_inherited(self):
- self._test_m2m_create_through(InheritedManyToManyField)
- def test_m2m_through_remove(self):
- class LocalAuthorNoteThrough(Model):
- book = ForeignKey("schema.Author", CASCADE)
- tag = ForeignKey("self", CASCADE)
- class Meta:
- app_label = "schema"
- apps = new_apps
- class LocalNoteWithM2MThrough(Model):
- authors = ManyToManyField("schema.Author", through=LocalAuthorNoteThrough)
- class Meta:
- app_label = "schema"
- apps = new_apps
- self.local_models = [LocalAuthorNoteThrough, LocalNoteWithM2MThrough]
- # Create the tables.
- with connection.schema_editor() as editor:
- editor.create_model(Author)
- editor.create_model(LocalAuthorNoteThrough)
- editor.create_model(LocalNoteWithM2MThrough)
- # Remove the through parameter.
- old_field = LocalNoteWithM2MThrough._meta.get_field("authors")
- new_field = ManyToManyField("Author")
- new_field.set_attributes_from_name("authors")
- msg = (
- f"Cannot alter field {old_field} into {new_field} - they are not "
- f"compatible types (you cannot alter to or from M2M fields, or add or "
- f"remove through= on M2M fields)"
- )
- with connection.schema_editor() as editor:
- with self.assertRaisesMessage(ValueError, msg):
- editor.alter_field(LocalNoteWithM2MThrough, old_field, new_field)
- def _test_m2m(self, M2MFieldClass):
- """
- Tests adding/removing M2M fields on models
- """
- class LocalAuthorWithM2M(Model):
- name = CharField(max_length=255)
- class Meta:
- app_label = "schema"
- apps = new_apps
- self.local_models = [LocalAuthorWithM2M]
- # Create the tables
- with connection.schema_editor() as editor:
- editor.create_model(LocalAuthorWithM2M)
- editor.create_model(TagM2MTest)
- # Create an M2M field
- new_field = M2MFieldClass("schema.TagM2MTest", related_name="authors")
- new_field.contribute_to_class(LocalAuthorWithM2M, "tags")
- # Ensure there's no m2m table there
- with self.assertRaises(DatabaseError):
- self.column_classes(new_field.remote_field.through)
- # Add the field
- with connection.schema_editor() as editor:
- editor.add_field(LocalAuthorWithM2M, new_field)
- # Ensure there is now an m2m table there
- columns = self.column_classes(new_field.remote_field.through)
- self.assertEqual(
- columns["tagm2mtest_id"][0],
- connection.features.introspected_field_types["IntegerField"],
- )
- # "Alter" the field. This should not rename the DB table to itself.
- with connection.schema_editor() as editor:
- editor.alter_field(LocalAuthorWithM2M, new_field, new_field, strict=True)
- # Remove the M2M table again
- with connection.schema_editor() as editor:
- editor.remove_field(LocalAuthorWithM2M, new_field)
- # Ensure there's no m2m table there
- with self.assertRaises(DatabaseError):
- self.column_classes(new_field.remote_field.through)
- # Make sure the model state is coherent with the table one now that
- # we've removed the tags field.
- opts = LocalAuthorWithM2M._meta
- opts.local_many_to_many.remove(new_field)
- del new_apps.all_models["schema"][
- new_field.remote_field.through._meta.model_name
- ]
- opts._expire_cache()
- def test_m2m(self):
- self._test_m2m(ManyToManyField)
- def test_m2m_custom(self):
- self._test_m2m(CustomManyToManyField)
- def test_m2m_inherited(self):
- self._test_m2m(InheritedManyToManyField)
- def _test_m2m_through_alter(self, M2MFieldClass):
- """
- Tests altering M2Ms with explicit through models (should no-op)
- """
- class LocalAuthorTag(Model):
- author = ForeignKey("schema.LocalAuthorWithM2MThrough", CASCADE)
- tag = ForeignKey("schema.TagM2MTest", CASCADE)
- class Meta:
- app_label = "schema"
- apps = new_apps
- class LocalAuthorWithM2MThrough(Model):
- name = CharField(max_length=255)
- tags = M2MFieldClass(
- "schema.TagM2MTest", related_name="authors", through=LocalAuthorTag
- )
- class Meta:
- app_label = "schema"
- apps = new_apps
- self.local_models = [LocalAuthorTag, LocalAuthorWithM2MThrough]
- # Create the tables
- with connection.schema_editor() as editor:
- editor.create_model(LocalAuthorTag)
- editor.create_model(LocalAuthorWithM2MThrough)
- editor.create_model(TagM2MTest)
- # Ensure the m2m table is there
- self.assertEqual(len(self.column_classes(LocalAuthorTag)), 3)
- # "Alter" the field's blankness. This should not actually do anything.
- old_field = LocalAuthorWithM2MThrough._meta.get_field("tags")
- new_field = M2MFieldClass(
- "schema.TagM2MTest", related_name="authors", through=LocalAuthorTag
- )
- new_field.contribute_to_class(LocalAuthorWithM2MThrough, "tags")
- with connection.schema_editor() as editor:
- editor.alter_field(
- LocalAuthorWithM2MThrough, old_field, new_field, strict=True
- )
- # Ensure the m2m table is still there
- self.assertEqual(len(self.column_classes(LocalAuthorTag)), 3)
- def test_m2m_through_alter(self):
- self._test_m2m_through_alter(ManyToManyField)
- def test_m2m_through_alter_custom(self):
- self._test_m2m_through_alter(CustomManyToManyField)
- def test_m2m_through_alter_inherited(self):
- self._test_m2m_through_alter(InheritedManyToManyField)
- def _test_m2m_repoint(self, M2MFieldClass):
- """
- Tests repointing M2M fields
- """
- class LocalBookWithM2M(Model):
- author = ForeignKey(Author, CASCADE)
- title = CharField(max_length=100, db_index=True)
- pub_date = DateTimeField()
- tags = M2MFieldClass("TagM2MTest", related_name="books")
- class Meta:
- app_label = "schema"
- apps = new_apps
- self.local_models = [LocalBookWithM2M]
- # Create the tables
- with connection.schema_editor() as editor:
- editor.create_model(Author)
- editor.create_model(LocalBookWithM2M)
- editor.create_model(TagM2MTest)
- editor.create_model(UniqueTest)
- # Ensure the M2M exists and points to TagM2MTest
- if connection.features.supports_foreign_keys:
- self.assertForeignKeyExists(
- LocalBookWithM2M._meta.get_field("tags").remote_field.through,
- "tagm2mtest_id",
- "schema_tagm2mtest",
- )
- # Repoint the M2M
- old_field = LocalBookWithM2M._meta.get_field("tags")
- new_field = M2MFieldClass(UniqueTest)
- new_field.contribute_to_class(LocalBookWithM2M, "uniques")
- with connection.schema_editor() as editor:
- editor.alter_field(LocalBookWithM2M, old_field, new_field, strict=True)
- # Ensure old M2M is gone
- with self.assertRaises(DatabaseError):
- self.column_classes(
- LocalBookWithM2M._meta.get_field("tags").remote_field.through
- )
- # This model looks like the new model and is used for teardown.
- opts = LocalBookWithM2M._meta
- opts.local_many_to_many.remove(old_field)
- # Ensure the new M2M exists and points to UniqueTest
- if connection.features.supports_foreign_keys:
- self.assertForeignKeyExists(
- new_field.remote_field.through, "uniquetest_id", "schema_uniquetest"
- )
- def test_m2m_repoint(self):
- self._test_m2m_repoint(ManyToManyField)
- def test_m2m_repoint_custom(self):
- self._test_m2m_repoint(CustomManyToManyField)
- def test_m2m_repoint_inherited(self):
- self._test_m2m_repoint(InheritedManyToManyField)
- @isolate_apps("schema")
- def test_m2m_rename_field_in_target_model(self):
- class LocalTagM2MTest(Model):
- title = CharField(max_length=255)
- class Meta:
- app_label = "schema"
- class LocalM2M(Model):
- tags = ManyToManyField(LocalTagM2MTest)
- class Meta:
- app_label = "schema"
- # Create the tables.
- with connection.schema_editor() as editor:
- editor.create_model(LocalM2M)
- editor.create_model(LocalTagM2MTest)
- self.isolated_local_models = [LocalM2M, LocalTagM2MTest]
- # Ensure the m2m table is there.
- self.assertEqual(len(self.column_classes(LocalM2M)), 1)
- # Alter a field in LocalTagM2MTest.
- old_field = LocalTagM2MTest._meta.get_field("title")
- new_field = CharField(max_length=254)
- new_field.contribute_to_class(LocalTagM2MTest, "title1")
- # @isolate_apps() and inner models are needed to have the model
- # relations populated, otherwise this doesn't act as a regression test.
- self.assertEqual(len(new_field.model._meta.related_objects), 1)
- with connection.schema_editor() as editor:
- editor.alter_field(LocalTagM2MTest, old_field, new_field, strict=True)
- # Ensure the m2m table is still there.
- self.assertEqual(len(self.column_classes(LocalM2M)), 1)
- @skipUnlessDBFeature(
- "supports_column_check_constraints", "can_introspect_check_constraints"
- )
- def test_check_constraints(self):
- """
- Tests creating/deleting CHECK constraints
- """
- # Create the tables
- with connection.schema_editor() as editor:
- editor.create_model(Author)
- # Ensure the constraint exists
- constraints = self.get_constraints(Author._meta.db_table)
- if not any(
- details["columns"] == ["height"] and details["check"]
- for details in constraints.values()
- ):
- self.fail("No check constraint for height found")
- # Alter the column to remove it
- old_field = Author._meta.get_field("height")
- new_field = IntegerField(null=True, blank=True)
- new_field.set_attributes_from_name("height")
- with connection.schema_editor() as editor:
- editor.alter_field(Author, old_field, new_field, strict=True)
- constraints = self.get_constraints(Author._meta.db_table)
- for details in constraints.values():
- if details["columns"] == ["height"] and details["check"]:
- self.fail("Check constraint for height found")
- # Alter the column to re-add it
- new_field2 = Author._meta.get_field("height")
- with connection.schema_editor() as editor:
- editor.alter_field(Author, new_field, new_field2, strict=True)
- constraints = self.get_constraints(Author._meta.db_table)
- if not any(
- details["columns"] == ["height"] and details["check"]
- for details in constraints.values()
- ):
- self.fail("No check constraint for height found")
- @skipUnlessDBFeature(
- "supports_column_check_constraints", "can_introspect_check_constraints"
- )
- @isolate_apps("schema")
- def test_check_constraint_timedelta_param(self):
- class DurationModel(Model):
- duration = DurationField()
- class Meta:
- app_label = "schema"
- with connection.schema_editor() as editor:
- editor.create_model(DurationModel)
- self.isolated_local_models = [DurationModel]
- constraint_name = "duration_gte_5_minutes"
- constraint = CheckConstraint(
- check=Q(duration__gt=datetime.timedelta(minutes=5)),
- name=constraint_name,
- )
- DurationModel._meta.constraints = [constraint]
- with connection.schema_editor() as editor:
- editor.add_constraint(DurationModel, constraint)
- constraints = self.get_constraints(DurationModel._meta.db_table)
- self.assertIn(constraint_name, constraints)
- with self.assertRaises(IntegrityError), atomic():
- DurationModel.objects.create(duration=datetime.timedelta(minutes=4))
- DurationModel.objects.create(duration=datetime.timedelta(minutes=10))
- @skipUnlessDBFeature(
- "supports_column_check_constraints", "can_introspect_check_constraints"
- )
- def test_remove_field_check_does_not_remove_meta_constraints(self):
- with connection.schema_editor() as editor:
- editor.create_model(Author)
- # Add the custom check constraint
- constraint = CheckConstraint(
- check=Q(height__gte=0), name="author_height_gte_0_check"
- )
- custom_constraint_name = constraint.name
- Author._meta.constraints = [constraint]
- with connection.schema_editor() as editor:
- editor.add_constraint(Author, constraint)
- # Ensure the constraints exist
- constraints = self.get_constraints(Author._meta.db_table)
- self.assertIn(custom_constraint_name, constraints)
- other_constraints = [
- name
- for name, details in constraints.items()
- if details["columns"] == ["height"]
- and details["check"]
- and name != custom_constraint_name
- ]
- self.assertEqual(len(other_constraints), 1)
- # Alter the column to remove field check
- old_field = Author._meta.get_field("height")
- new_field = IntegerField(null=True, blank=True)
- new_field.set_attributes_from_name("height")
- with connection.schema_editor() as editor:
- editor.alter_field(Author, old_field, new_field, strict=True)
- constraints = self.get_constraints(Author._meta.db_table)
- self.assertIn(custom_constraint_name, constraints)
- other_constraints = [
- name
- for name, details in constraints.items()
- if details["columns"] == ["height"]
- and details["check"]
- and name != custom_constraint_name
- ]
- self.assertEqual(len(other_constraints), 0)
- # Alter the column to re-add field check
- new_field2 = Author._meta.get_field("height")
- with connection.schema_editor() as editor:
- editor.alter_field(Author, new_field, new_field2, strict=True)
- constraints = self.get_constraints(Author._meta.db_table)
- self.assertIn(custom_constraint_name, constraints)
- other_constraints = [
- name
- for name, details in constraints.items()
- if details["columns"] == ["height"]
- and details["check"]
- and name != custom_constraint_name
- ]
- self.assertEqual(len(other_constraints), 1)
- # Drop the check constraint
- with connection.schema_editor() as editor:
- Author._meta.constraints = []
- editor.remove_constraint(Author, constraint)
- def test_unique(self):
- """
- Tests removing and adding unique constraints to a single column.
- """
- # Create the table
- with connection.schema_editor() as editor:
- editor.create_model(Tag)
- # Ensure the field is unique to begin with
- Tag.objects.create(title="foo", slug="foo")
- with self.assertRaises(IntegrityError):
- Tag.objects.create(title="bar", slug="foo")
- Tag.objects.all().delete()
- # Alter the slug field to be non-unique
- old_field = Tag._meta.get_field("slug")
- new_field = SlugField(unique=False)
- new_field.set_attributes_from_name("slug")
- with connection.schema_editor() as editor:
- editor.alter_field(Tag, old_field, new_field, strict=True)
- # Ensure the field is no longer unique
- Tag.objects.create(title="foo", slug="foo")
- Tag.objects.create(title="bar", slug="foo")
- Tag.objects.all().delete()
- # Alter the slug field to be unique
- new_field2 = SlugField(unique=True)
- new_field2.set_attributes_from_name("slug")
- with connection.schema_editor() as editor:
- editor.alter_field(Tag, new_field, new_field2, strict=True)
- # Ensure the field is unique again
- Tag.objects.create(title="foo", slug="foo")
- with self.assertRaises(IntegrityError):
- Tag.objects.create(title="bar", slug="foo")
- Tag.objects.all().delete()
- # Rename the field
- new_field3 = SlugField(unique=True)
- new_field3.set_attributes_from_name("slug2")
- with connection.schema_editor() as editor:
- editor.alter_field(Tag, new_field2, new_field3, strict=True)
- # Ensure the field is still unique
- TagUniqueRename.objects.create(title="foo", slug2="foo")
- with self.assertRaises(IntegrityError):
- TagUniqueRename.objects.create(title="bar", slug2="foo")
- Tag.objects.all().delete()
- def test_unique_name_quoting(self):
- old_table_name = TagUniqueRename._meta.db_table
- try:
- with connection.schema_editor() as editor:
- editor.create_model(TagUniqueRename)
- editor.alter_db_table(TagUniqueRename, old_table_name, "unique-table")
- TagUniqueRename._meta.db_table = "unique-table"
- # This fails if the unique index name isn't quoted.
- editor.alter_unique_together(TagUniqueRename, [], (("title", "slug2"),))
- finally:
- with connection.schema_editor() as editor:
- editor.delete_model(TagUniqueRename)
- TagUniqueRename._meta.db_table = old_table_name
- @isolate_apps("schema")
- @skipUnlessDBFeature("supports_foreign_keys")
- def test_unique_no_unnecessary_fk_drops(self):
- """
- If AlterField isn't selective about dropping foreign key constraints
- when modifying a field with a unique constraint, the AlterField
- incorrectly drops and recreates the Book.author foreign key even though
- it doesn't restrict the field being changed (#29193).
- """
- class Author(Model):
- name = CharField(max_length=254, unique=True)
- class Meta:
- app_label = "schema"
- class Book(Model):
- author = ForeignKey(Author, CASCADE)
- class Meta:
- app_label = "schema"
- with connection.schema_editor() as editor:
- editor.create_model(Author)
- editor.create_model(Book)
- new_field = CharField(max_length=255, unique=True)
- new_field.model = Author
- new_field.set_attributes_from_name("name")
- with self.assertLogs("django.db.backends.schema", "DEBUG") as cm:
- with connection.schema_editor() as editor:
- editor.alter_field(Author, Author._meta.get_field("name"), new_field)
- # One SQL statement is executed to alter the field.
- self.assertEqual(len(cm.records), 1)
- @isolate_apps("schema")
- def test_unique_and_reverse_m2m(self):
- """
- AlterField can modify a unique field when there's a reverse M2M
- relation on the model.
- """
- class Tag(Model):
- title = CharField(max_length=255)
- slug = SlugField(unique=True)
- class Meta:
- app_label = "schema"
- class Book(Model):
- tags = ManyToManyField(Tag, related_name="books")
- class Meta:
- app_label = "schema"
- self.isolated_local_models = [Book._meta.get_field("tags").remote_field.through]
- with connection.schema_editor() as editor:
- editor.create_model(Tag)
- editor.create_model(Book)
- new_field = SlugField(max_length=75, unique=True)
- new_field.model = Tag
- new_field.set_attributes_from_name("slug")
- with self.assertLogs("django.db.backends.schema", "DEBUG") as cm:
- with connection.schema_editor() as editor:
- editor.alter_field(Tag, Tag._meta.get_field("slug"), new_field)
- # One SQL statement is executed to alter the field.
- self.assertEqual(len(cm.records), 1)
- # Ensure that the field is still unique.
- Tag.objects.create(title="foo", slug="foo")
- with self.assertRaises(IntegrityError):
- Tag.objects.create(title="bar", slug="foo")
- @skipUnlessDBFeature("allows_multiple_constraints_on_same_fields")
- def test_remove_field_unique_does_not_remove_meta_constraints(self):
- with connection.schema_editor() as editor:
- editor.create_model(AuthorWithUniqueName)
- self.local_models = [AuthorWithUniqueName]
- # Add the custom unique constraint
- constraint = UniqueConstraint(fields=["name"], name="author_name_uniq")
- custom_constraint_name = constraint.name
- AuthorWithUniqueName._meta.constraints = [constraint]
- with connection.schema_editor() as editor:
- editor.add_constraint(AuthorWithUniqueName, constraint)
- # Ensure the constraints exist
- constraints = self.get_constraints(AuthorWithUniqueName._meta.db_table)
- self.assertIn(custom_constraint_name, constraints)
- other_constraints = [
- name
- for name, details in constraints.items()
- if details["columns"] == ["name"]
- and details["unique"]
- and name != custom_constraint_name
- ]
- self.assertEqual(len(other_constraints), 1)
- # Alter the column to remove field uniqueness
- old_field = AuthorWithUniqueName._meta.get_field("name")
- new_field = CharField(max_length=255)
- new_field.set_attributes_from_name("name")
- with connection.schema_editor() as editor:
- editor.alter_field(AuthorWithUniqueName, old_field, new_field, strict=True)
- constraints = self.get_constraints(AuthorWithUniqueName._meta.db_table)
- self.assertIn(custom_constraint_name, constraints)
- other_constraints = [
- name
- for name, details in constraints.items()
- if details["columns"] == ["name"]
- and details["unique"]
- and name != custom_constraint_name
- ]
- self.assertEqual(len(other_constraints), 0)
- # Alter the column to re-add field uniqueness
- new_field2 = AuthorWithUniqueName._meta.get_field("name")
- with connection.schema_editor() as editor:
- editor.alter_field(AuthorWithUniqueName, new_field, new_field2, strict=True)
- constraints = self.get_constraints(AuthorWithUniqueName._meta.db_table)
- self.assertIn(custom_constraint_name, constraints)
- other_constraints = [
- name
- for name, details in constraints.items()
- if details["columns"] == ["name"]
- and details["unique"]
- and name != custom_constraint_name
- ]
- self.assertEqual(len(other_constraints), 1)
- # Drop the unique constraint
- with connection.schema_editor() as editor:
- AuthorWithUniqueName._meta.constraints = []
- editor.remove_constraint(AuthorWithUniqueName, constraint)
- def test_unique_together(self):
- """
- Tests removing and adding unique_together constraints on a model.
- """
- # Create the table
- with connection.schema_editor() as editor:
- editor.create_model(UniqueTest)
- # Ensure the fields are unique to begin with
- UniqueTest.objects.create(year=2012, slug="foo")
- UniqueTest.objects.create(year=2011, slug="foo")
- UniqueTest.objects.create(year=2011, slug="bar")
- with self.assertRaises(IntegrityError):
- UniqueTest.objects.create(year=2012, slug="foo")
- UniqueTest.objects.all().delete()
- # Alter the model to its non-unique-together companion
- with connection.schema_editor() as editor:
- editor.alter_unique_together(
- UniqueTest, UniqueTest._meta.unique_together, []
- )
- # Ensure the fields are no longer unique
- UniqueTest.objects.create(year=2012, slug="foo")
- UniqueTest.objects.create(year=2012, slug="foo")
- UniqueTest.objects.all().delete()
- # Alter it back
- new_field2 = SlugField(unique=True)
- new_field2.set_attributes_from_name("slug")
- with connection.schema_editor() as editor:
- editor.alter_unique_together(
- UniqueTest, [], UniqueTest._meta.unique_together
- )
- # Ensure the fields are unique again
- UniqueTest.objects.create(year=2012, slug="foo")
- with self.assertRaises(IntegrityError):
- UniqueTest.objects.create(year=2012, slug="foo")
- UniqueTest.objects.all().delete()
- def test_unique_together_with_fk(self):
- """
- Tests removing and adding unique_together constraints that include
- a foreign key.
- """
- # Create the table
- with connection.schema_editor() as editor:
- editor.create_model(Author)
- editor.create_model(Book)
- # Ensure the fields are unique to begin with
- self.assertEqual(Book._meta.unique_together, ())
- # Add the unique_together constraint
- with connection.schema_editor() as editor:
- editor.alter_unique_together(Book, [], [["author", "title"]])
- # Alter it back
- with connection.schema_editor() as editor:
- editor.alter_unique_together(Book, [["author", "title"]], [])
- def test_unique_together_with_fk_with_existing_index(self):
- """
- Tests removing and adding unique_together constraints that include
- a foreign key, where the foreign key is added after the model is
- created.
- """
- # Create the tables
- with connection.schema_editor() as editor:
- editor.create_model(Author)
- editor.create_model(BookWithoutAuthor)
- new_field = ForeignKey(Author, CASCADE)
- new_field.set_attributes_from_name("author")
- editor.add_field(BookWithoutAuthor, new_field)
- # Ensure the fields aren't unique to begin with
- self.assertEqual(Book._meta.unique_together, ())
- # Add the unique_together constraint
- with connection.schema_editor() as editor:
- editor.alter_unique_together(Book, [], [["author", "title"]])
- # Alter it back
- with connection.schema_editor() as editor:
- editor.alter_unique_together(Book, [["author", "title"]], [])
- @skipUnlessDBFeature("allows_multiple_constraints_on_same_fields")
- def test_remove_unique_together_does_not_remove_meta_constraints(self):
- with connection.schema_editor() as editor:
- editor.create_model(AuthorWithUniqueNameAndBirthday)
- self.local_models = [AuthorWithUniqueNameAndBirthday]
- # Add the custom unique constraint
- constraint = UniqueConstraint(
- fields=["name", "birthday"], name="author_name_birthday_uniq"
- )
- custom_constraint_name = constraint.name
- AuthorWithUniqueNameAndBirthday._meta.constraints = [constraint]
- with connection.schema_editor() as editor:
- editor.add_constraint(AuthorWithUniqueNameAndBirthday, constraint)
- # Ensure the constraints exist
- constraints = self.get_constraints(
- AuthorWithUniqueNameAndBirthday._meta.db_table
- )
- self.assertIn(custom_constraint_name, constraints)
- other_constraints = [
- name
- for name, details in constraints.items()
- if details["columns"] == ["name", "birthday"]
- and details["unique"]
- and name != custom_constraint_name
- ]
- self.assertEqual(len(other_constraints), 1)
- # Remove unique together
- unique_together = AuthorWithUniqueNameAndBirthday._meta.unique_together
- with connection.schema_editor() as editor:
- editor.alter_unique_together(
- AuthorWithUniqueNameAndBirthday, unique_together, []
- )
- constraints = self.get_constraints(
- AuthorWithUniqueNameAndBirthday._meta.db_table
- )
- self.assertIn(custom_constraint_name, constraints)
- other_constraints = [
- name
- for name, details in constraints.items()
- if details["columns"] == ["name", "birthday"]
- and details["unique"]
- and name != custom_constraint_name
- ]
- self.assertEqual(len(other_constraints), 0)
- # Re-add unique together
- with connection.schema_editor() as editor:
- editor.alter_unique_together(
- AuthorWithUniqueNameAndBirthday, [], unique_together
- )
- constraints = self.get_constraints(
- AuthorWithUniqueNameAndBirthday._meta.db_table
- )
- self.assertIn(custom_constraint_name, constraints)
- other_constraints = [
- name
- for name, details in constraints.items()
- if details["columns"] == ["name", "birthday"]
- and details["unique"]
- and name != custom_constraint_name
- ]
- self.assertEqual(len(other_constraints), 1)
- # Drop the unique constraint
- with connection.schema_editor() as editor:
- AuthorWithUniqueNameAndBirthday._meta.constraints = []
- editor.remove_constraint(AuthorWithUniqueNameAndBirthday, constraint)
- def test_unique_constraint(self):
- with connection.schema_editor() as editor:
- editor.create_model(Author)
- constraint = UniqueConstraint(fields=["name"], name="name_uq")
- # Add constraint.
- with connection.schema_editor() as editor:
- editor.add_constraint(Author, constraint)
- sql = constraint.create_sql(Author, editor)
- table = Author._meta.db_table
- self.assertIs(sql.references_table(table), True)
- self.assertIs(sql.references_column(table, "name"), True)
- # Remove constraint.
- with connection.schema_editor() as editor:
- editor.remove_constraint(Author, constraint)
- self.assertNotIn(constraint.name, self.get_constraints(table))
- @skipUnlessDBFeature("supports_expression_indexes")
- def test_func_unique_constraint(self):
- with connection.schema_editor() as editor:
- editor.create_model(Author)
- constraint = UniqueConstraint(Upper("name").desc(), name="func_upper_uq")
- # Add constraint.
- with connection.schema_editor() as editor:
- editor.add_constraint(Author, constraint)
- sql = constraint.create_sql(Author, editor)
- table = Author._meta.db_table
- constraints = self.get_constraints(table)
- if connection.features.supports_index_column_ordering:
- self.assertIndexOrder(table, constraint.name, ["DESC"])
- self.assertIn(constraint.name, constraints)
- self.assertIs(constraints[constraint.name]["unique"], True)
- # SQL contains a database function.
- self.assertIs(sql.references_column(table, "name"), True)
- self.assertIn("UPPER(%s)" % editor.quote_name("name"), str(sql))
- # Remove constraint.
- with connection.schema_editor() as editor:
- editor.remove_constraint(Author, constraint)
- self.assertNotIn(constraint.name, self.get_constraints(table))
- @skipUnlessDBFeature("supports_expression_indexes")
- def test_composite_func_unique_constraint(self):
- with connection.schema_editor() as editor:
- editor.create_model(Author)
- editor.create_model(BookWithSlug)
- constraint = UniqueConstraint(
- Upper("title"),
- Lower("slug"),
- name="func_upper_lower_unq",
- )
- # Add constraint.
- with connection.schema_editor() as editor:
- editor.add_constraint(BookWithSlug, constraint)
- sql = constraint.create_sql(BookWithSlug, editor)
- table = BookWithSlug._meta.db_table
- constraints = self.get_constraints(table)
- self.assertIn(constraint.name, constraints)
- self.assertIs(constraints[constraint.name]["unique"], True)
- # SQL contains database functions.
- self.assertIs(sql.references_column(table, "title"), True)
- self.assertIs(sql.references_column(table, "slug"), True)
- sql = str(sql)
- self.assertIn("UPPER(%s)" % editor.quote_name("title"), sql)
- self.assertIn("LOWER(%s)" % editor.quote_name("slug"), sql)
- self.assertLess(sql.index("UPPER"), sql.index("LOWER"))
- # Remove constraint.
- with connection.schema_editor() as editor:
- editor.remove_constraint(BookWithSlug, constraint)
- self.assertNotIn(constraint.name, self.get_constraints(table))
- @skipUnlessDBFeature("supports_expression_indexes")
- def test_unique_constraint_field_and_expression(self):
- with connection.schema_editor() as editor:
- editor.create_model(Author)
- constraint = UniqueConstraint(
- F("height").desc(),
- "uuid",
- Lower("name").asc(),
- name="func_f_lower_field_unq",
- )
- # Add constraint.
- with connection.schema_editor() as editor:
- editor.add_constraint(Author, constraint)
- sql = constraint.create_sql(Author, editor)
- table = Author._meta.db_table
- if connection.features.supports_index_column_ordering:
- self.assertIndexOrder(table, constraint.name, ["DESC", "ASC", "ASC"])
- constraints = self.get_constraints(table)
- self.assertIs(constraints[constraint.name]["unique"], True)
- self.assertEqual(len(constraints[constraint.name]["columns"]), 3)
- self.assertEqual(constraints[constraint.name]["columns"][1], "uuid")
- # SQL contains database functions and columns.
- self.assertIs(sql.references_column(table, "height"), True)
- self.assertIs(sql.references_column(table, "name"), True)
- self.assertIs(sql.references_column(table, "uuid"), True)
- self.assertIn("LOWER(%s)" % editor.quote_name("name"), str(sql))
- # Remove constraint.
- with connection.schema_editor() as editor:
- editor.remove_constraint(Author, constraint)
- self.assertNotIn(constraint.name, self.get_constraints(table))
- @skipUnlessDBFeature("supports_expression_indexes", "supports_partial_indexes")
- def test_func_unique_constraint_partial(self):
- with connection.schema_editor() as editor:
- editor.create_model(Author)
- constraint = UniqueConstraint(
- Upper("name"),
- name="func_upper_cond_weight_uq",
- condition=Q(weight__isnull=False),
- )
- # Add constraint.
- with connection.schema_editor() as editor:
- editor.add_constraint(Author, constraint)
- sql = constraint.create_sql(Author, editor)
- table = Author._meta.db_table
- constraints = self.get_constraints(table)
- self.assertIn(constraint.name, constraints)
- self.assertIs(constraints[constraint.name]["unique"], True)
- self.assertIs(sql.references_column(table, "name"), True)
- self.assertIn("UPPER(%s)" % editor.quote_name("name"), str(sql))
- self.assertIn(
- "WHERE %s IS NOT NULL" % editor.quote_name("weight"),
- str(sql),
- )
- # Remove constraint.
- with connection.schema_editor() as editor:
- editor.remove_constraint(Author, constraint)
- self.assertNotIn(constraint.name, self.get_constraints(table))
- @skipUnlessDBFeature("supports_expression_indexes", "supports_covering_indexes")
- def test_func_unique_constraint_covering(self):
- with connection.schema_editor() as editor:
- editor.create_model(Author)
- constraint = UniqueConstraint(
- Upper("name"),
- name="func_upper_covering_uq",
- include=["weight", "height"],
- )
- # Add constraint.
- with connection.schema_editor() as editor:
- editor.add_constraint(Author, constraint)
- sql = constraint.create_sql(Author, editor)
- table = Author._meta.db_table
- constraints = self.get_constraints(table)
- self.assertIn(constraint.name, constraints)
- self.assertIs(constraints[constraint.name]["unique"], True)
- self.assertEqual(
- constraints[constraint.name]["columns"],
- [None, "weight", "height"],
- )
- self.assertIs(sql.references_column(table, "name"), True)
- self.assertIs(sql.references_column(table, "weight"), True)
- self.assertIs(sql.references_column(table, "height"), True)
- self.assertIn("UPPER(%s)" % editor.quote_name("name"), str(sql))
- self.assertIn(
- "INCLUDE (%s, %s)"
- % (
- editor.quote_name("weight"),
- editor.quote_name("height"),
- ),
- str(sql),
- )
- # Remove constraint.
- with connection.schema_editor() as editor:
- editor.remove_constraint(Author, constraint)
- self.assertNotIn(constraint.name, self.get_constraints(table))
- @skipUnlessDBFeature("supports_expression_indexes")
- def test_func_unique_constraint_lookups(self):
- with connection.schema_editor() as editor:
- editor.create_model(Author)
- with register_lookup(CharField, Lower), register_lookup(IntegerField, Abs):
- constraint = UniqueConstraint(
- F("name__lower"),
- F("weight__abs"),
- name="func_lower_abs_lookup_uq",
- )
- # Add constraint.
- with connection.schema_editor() as editor:
- editor.add_constraint(Author, constraint)
- sql = constraint.create_sql(Author, editor)
- table = Author._meta.db_table
- constraints = self.get_constraints(table)
- self.assertIn(constraint.name, constraints)
- self.assertIs(constraints[constraint.name]["unique"], True)
- # SQL contains columns.
- self.assertIs(sql.references_column(table, "name"), True)
- self.assertIs(sql.references_column(table, "weight"), True)
- # Remove constraint.
- with connection.schema_editor() as editor:
- editor.remove_constraint(Author, constraint)
- self.assertNotIn(constraint.name, self.get_constraints(table))
- @skipUnlessDBFeature("supports_expression_indexes")
- def test_func_unique_constraint_collate(self):
- collation = connection.features.test_collations.get("non_default")
- if not collation:
- self.skipTest("This backend does not support case-insensitive collations.")
- with connection.schema_editor() as editor:
- editor.create_model(Author)
- editor.create_model(BookWithSlug)
- constraint = UniqueConstraint(
- Collate(F("title"), collation=collation).desc(),
- Collate("slug", collation=collation),
- name="func_collate_uq",
- )
- # Add constraint.
- with connection.schema_editor() as editor:
- editor.add_constraint(BookWithSlug, constraint)
- sql = constraint.create_sql(BookWithSlug, editor)
- table = BookWithSlug._meta.db_table
- constraints = self.get_constraints(table)
- self.assertIn(constraint.name, constraints)
- self.assertIs(constraints[constraint.name]["unique"], True)
- if connection.features.supports_index_column_ordering:
- self.assertIndexOrder(table, constraint.name, ["DESC", "ASC"])
- # SQL contains columns and a collation.
- self.assertIs(sql.references_column(table, "title"), True)
- self.assertIs(sql.references_column(table, "slug"), True)
- self.assertIn("COLLATE %s" % editor.quote_name(collation), str(sql))
- # Remove constraint.
- with connection.schema_editor() as editor:
- editor.remove_constraint(BookWithSlug, constraint)
- self.assertNotIn(constraint.name, self.get_constraints(table))
- @skipIfDBFeature("supports_expression_indexes")
- def test_func_unique_constraint_unsupported(self):
- # UniqueConstraint is ignored on databases that don't support indexes on
- # expressions.
- with connection.schema_editor() as editor:
- editor.create_model(Author)
- constraint = UniqueConstraint(F("name"), name="func_name_uq")
- with connection.schema_editor() as editor, self.assertNumQueries(0):
- self.assertIsNone(editor.add_constraint(Author, constraint))
- self.assertIsNone(editor.remove_constraint(Author, constraint))
- @skipUnlessDBFeature("supports_expression_indexes")
- def test_func_unique_constraint_nonexistent_field(self):
- constraint = UniqueConstraint(Lower("nonexistent"), name="func_nonexistent_uq")
- msg = (
- "Cannot resolve keyword 'nonexistent' into field. Choices are: "
- "height, id, name, uuid, weight"
- )
- with self.assertRaisesMessage(FieldError, msg):
- with connection.schema_editor() as editor:
- editor.add_constraint(Author, constraint)
- @skipUnlessDBFeature("supports_expression_indexes")
- def test_func_unique_constraint_nondeterministic(self):
- with connection.schema_editor() as editor:
- editor.create_model(Author)
- constraint = UniqueConstraint(Random(), name="func_random_uq")
- with connection.schema_editor() as editor:
- with self.assertRaises(DatabaseError):
- editor.add_constraint(Author, constraint)
- @ignore_warnings(category=RemovedInDjango51Warning)
- def test_index_together(self):
- """
- Tests removing and adding index_together constraints on a model.
- """
- # Create the table
- with connection.schema_editor() as editor:
- editor.create_model(Tag)
- # Ensure there's no index on the year/slug columns first
- self.assertIs(
- any(
- c["index"]
- for c in self.get_constraints("schema_tag").values()
- if c["columns"] == ["slug", "title"]
- ),
- False,
- )
- # Alter the model to add an index
- with connection.schema_editor() as editor:
- editor.alter_index_together(Tag, [], [("slug", "title")])
- # Ensure there is now an index
- self.assertIs(
- any(
- c["index"]
- for c in self.get_constraints("schema_tag").values()
- if c["columns"] == ["slug", "title"]
- ),
- True,
- )
- # Alter it back
- new_field2 = SlugField(unique=True)
- new_field2.set_attributes_from_name("slug")
- with connection.schema_editor() as editor:
- editor.alter_index_together(Tag, [("slug", "title")], [])
- # Ensure there's no index
- self.assertIs(
- any(
- c["index"]
- for c in self.get_constraints("schema_tag").values()
- if c["columns"] == ["slug", "title"]
- ),
- False,
- )
- @ignore_warnings(category=RemovedInDjango51Warning)
- def test_index_together_with_fk(self):
- """
- Tests removing and adding index_together constraints that include
- a foreign key.
- """
- # Create the table
- with connection.schema_editor() as editor:
- editor.create_model(Author)
- editor.create_model(Book)
- # Ensure the fields are unique to begin with
- self.assertEqual(Book._meta.index_together, ())
- # Add the unique_together constraint
- with connection.schema_editor() as editor:
- editor.alter_index_together(Book, [], [["author", "title"]])
- # Alter it back
- with connection.schema_editor() as editor:
- editor.alter_index_together(Book, [["author", "title"]], [])
- @ignore_warnings(category=RemovedInDjango51Warning)
- @isolate_apps("schema")
- def test_create_index_together(self):
- """
- Tests creating models with index_together already defined
- """
- class TagIndexed(Model):
- title = CharField(max_length=255)
- slug = SlugField(unique=True)
- class Meta:
- app_label = "schema"
- index_together = [["slug", "title"]]
- # Create the table
- with connection.schema_editor() as editor:
- editor.create_model(TagIndexed)
- self.isolated_local_models = [TagIndexed]
- # Ensure there is an index
- self.assertIs(
- any(
- c["index"]
- for c in self.get_constraints("schema_tagindexed").values()
- if c["columns"] == ["slug", "title"]
- ),
- True,
- )
- @skipUnlessDBFeature("allows_multiple_constraints_on_same_fields")
- @ignore_warnings(category=RemovedInDjango51Warning)
- @isolate_apps("schema")
- def test_remove_index_together_does_not_remove_meta_indexes(self):
- class AuthorWithIndexedNameAndBirthday(Model):
- name = CharField(max_length=255)
- birthday = DateField()
- class Meta:
- app_label = "schema"
- index_together = [["name", "birthday"]]
- with connection.schema_editor() as editor:
- editor.create_model(AuthorWithIndexedNameAndBirthday)
- self.isolated_local_models = [AuthorWithIndexedNameAndBirthday]
- # Add the custom index
- index = Index(fields=["name", "birthday"], name="author_name_birthday_idx")
- custom_index_name = index.name
- AuthorWithIndexedNameAndBirthday._meta.indexes = [index]
- with connection.schema_editor() as editor:
- editor.add_index(AuthorWithIndexedNameAndBirthday, index)
- # Ensure the indexes exist
- constraints = self.get_constraints(
- AuthorWithIndexedNameAndBirthday._meta.db_table
- )
- self.assertIn(custom_index_name, constraints)
- other_constraints = [
- name
- for name, details in constraints.items()
- if details["columns"] == ["name", "birthday"]
- and details["index"]
- and name != custom_index_name
- ]
- self.assertEqual(len(other_constraints), 1)
- # Remove index together
- index_together = AuthorWithIndexedNameAndBirthday._meta.index_together
- with connection.schema_editor() as editor:
- editor.alter_index_together(
- AuthorWithIndexedNameAndBirthday, index_together, []
- )
- constraints = self.get_constraints(
- AuthorWithIndexedNameAndBirthday._meta.db_table
- )
- self.assertIn(custom_index_name, constraints)
- other_constraints = [
- name
- for name, details in constraints.items()
- if details["columns"] == ["name", "birthday"]
- and details["index"]
- and name != custom_index_name
- ]
- self.assertEqual(len(other_constraints), 0)
- # Re-add index together
- with connection.schema_editor() as editor:
- editor.alter_index_together(
- AuthorWithIndexedNameAndBirthday, [], index_together
- )
- constraints = self.get_constraints(
- AuthorWithIndexedNameAndBirthday._meta.db_table
- )
- self.assertIn(custom_index_name, constraints)
- other_constraints = [
- name
- for name, details in constraints.items()
- if details["columns"] == ["name", "birthday"]
- and details["index"]
- and name != custom_index_name
- ]
- self.assertEqual(len(other_constraints), 1)
- # Drop the index
- with connection.schema_editor() as editor:
- AuthorWithIndexedNameAndBirthday._meta.indexes = []
- editor.remove_index(AuthorWithIndexedNameAndBirthday, index)
- @isolate_apps("schema")
- def test_db_table(self):
- """
- Tests renaming of the table
- """
- class Author(Model):
- name = CharField(max_length=255)
- class Meta:
- app_label = "schema"
- class Book(Model):
- author = ForeignKey(Author, CASCADE)
- class Meta:
- app_label = "schema"
- # Create the table and one referring it.
- with connection.schema_editor() as editor:
- editor.create_model(Author)
- editor.create_model(Book)
- # Ensure the table is there to begin with
- columns = self.column_classes(Author)
- self.assertEqual(
- columns["name"][0],
- connection.features.introspected_field_types["CharField"],
- )
- # Alter the table
- with connection.schema_editor(
- atomic=connection.features.supports_atomic_references_rename
- ) as editor:
- editor.alter_db_table(Author, "schema_author", "schema_otherauthor")
- Author._meta.db_table = "schema_otherauthor"
- columns = self.column_classes(Author)
- self.assertEqual(
- columns["name"][0],
- connection.features.introspected_field_types["CharField"],
- )
- # Ensure the foreign key reference was updated
- self.assertForeignKeyExists(Book, "author_id", "schema_otherauthor")
- # Alter the table again
- with connection.schema_editor(
- atomic=connection.features.supports_atomic_references_rename
- ) as editor:
- editor.alter_db_table(Author, "schema_otherauthor", "schema_author")
- # Ensure the table is still there
- Author._meta.db_table = "schema_author"
- columns = self.column_classes(Author)
- self.assertEqual(
- columns["name"][0],
- connection.features.introspected_field_types["CharField"],
- )
- def test_add_remove_index(self):
- """
- Tests index addition and removal
- """
- # Create the table
- with connection.schema_editor() as editor:
- editor.create_model(Author)
- # Ensure the table is there and has no index
- self.assertNotIn("title", self.get_indexes(Author._meta.db_table))
- # Add the index
- index = Index(fields=["name"], name="author_title_idx")
- with connection.schema_editor() as editor:
- editor.add_index(Author, index)
- self.assertIn("name", self.get_indexes(Author._meta.db_table))
- # Drop the index
- with connection.schema_editor() as editor:
- editor.remove_index(Author, index)
- self.assertNotIn("name", self.get_indexes(Author._meta.db_table))
- def test_remove_db_index_doesnt_remove_custom_indexes(self):
- """
- Changing db_index to False doesn't remove indexes from Meta.indexes.
- """
- with connection.schema_editor() as editor:
- editor.create_model(AuthorWithIndexedName)
- self.local_models = [AuthorWithIndexedName]
- # Ensure the table has its index
- self.assertIn("name", self.get_indexes(AuthorWithIndexedName._meta.db_table))
- # Add the custom index
- index = Index(fields=["-name"], name="author_name_idx")
- author_index_name = index.name
- with connection.schema_editor() as editor:
- db_index_name = editor._create_index_name(
- table_name=AuthorWithIndexedName._meta.db_table,
- column_names=("name",),
- )
- try:
- AuthorWithIndexedName._meta.indexes = [index]
- with connection.schema_editor() as editor:
- editor.add_index(AuthorWithIndexedName, index)
- old_constraints = self.get_constraints(AuthorWithIndexedName._meta.db_table)
- self.assertIn(author_index_name, old_constraints)
- self.assertIn(db_index_name, old_constraints)
- # Change name field to db_index=False
- old_field = AuthorWithIndexedName._meta.get_field("name")
- new_field = CharField(max_length=255)
- new_field.set_attributes_from_name("name")
- with connection.schema_editor() as editor:
- editor.alter_field(
- AuthorWithIndexedName, old_field, new_field, strict=True
- )
- new_constraints = self.get_constraints(AuthorWithIndexedName._meta.db_table)
- self.assertNotIn(db_index_name, new_constraints)
- # The index from Meta.indexes is still in the database.
- self.assertIn(author_index_name, new_constraints)
- # Drop the index
- with connection.schema_editor() as editor:
- editor.remove_index(AuthorWithIndexedName, index)
- finally:
- AuthorWithIndexedName._meta.indexes = []
- def test_order_index(self):
- """
- Indexes defined with ordering (ASC/DESC) defined on column
- """
- with connection.schema_editor() as editor:
- editor.create_model(Author)
- # The table doesn't have an index
- self.assertNotIn("title", self.get_indexes(Author._meta.db_table))
- index_name = "author_name_idx"
- # Add the index
- index = Index(fields=["name", "-weight"], name=index_name)
- with connection.schema_editor() as editor:
- editor.add_index(Author, index)
- if connection.features.supports_index_column_ordering:
- self.assertIndexOrder(Author._meta.db_table, index_name, ["ASC", "DESC"])
- # Drop the index
- with connection.schema_editor() as editor:
- editor.remove_index(Author, index)
- def test_indexes(self):
- """
- Tests creation/altering of indexes
- """
- # Create the table
- with connection.schema_editor() as editor:
- editor.create_model(Author)
- editor.create_model(Book)
- # Ensure the table is there and has the right index
- self.assertIn(
- "title",
- self.get_indexes(Book._meta.db_table),
- )
- # Alter to remove the index
- old_field = Book._meta.get_field("title")
- new_field = CharField(max_length=100, db_index=False)
- new_field.set_attributes_from_name("title")
- with connection.schema_editor() as editor:
- editor.alter_field(Book, old_field, new_field, strict=True)
- # Ensure the table is there and has no index
- self.assertNotIn(
- "title",
- self.get_indexes(Book._meta.db_table),
- )
- # Alter to re-add the index
- new_field2 = Book._meta.get_field("title")
- with connection.schema_editor() as editor:
- editor.alter_field(Book, new_field, new_field2, strict=True)
- # Ensure the table is there and has the index again
- self.assertIn(
- "title",
- self.get_indexes(Book._meta.db_table),
- )
- # Add a unique column, verify that creates an implicit index
- new_field3 = BookWithSlug._meta.get_field("slug")
- with connection.schema_editor() as editor:
- editor.add_field(Book, new_field3)
- self.assertIn(
- "slug",
- self.get_uniques(Book._meta.db_table),
- )
- # Remove the unique, check the index goes with it
- new_field4 = CharField(max_length=20, unique=False)
- new_field4.set_attributes_from_name("slug")
- with connection.schema_editor() as editor:
- editor.alter_field(BookWithSlug, new_field3, new_field4, strict=True)
- self.assertNotIn(
- "slug",
- self.get_uniques(Book._meta.db_table),
- )
- def test_text_field_with_db_index(self):
- with connection.schema_editor() as editor:
- editor.create_model(AuthorTextFieldWithIndex)
- # The text_field index is present if the database supports it.
- assertion = (
- self.assertIn
- if connection.features.supports_index_on_text_field
- else self.assertNotIn
- )
- assertion(
- "text_field", self.get_indexes(AuthorTextFieldWithIndex._meta.db_table)
- )
- def _index_expressions_wrappers(self):
- index_expression = IndexExpression()
- index_expression.set_wrapper_classes(connection)
- return ", ".join(
- [
- wrapper_cls.__qualname__
- for wrapper_cls in index_expression.wrapper_classes
- ]
- )
- @skipUnlessDBFeature("supports_expression_indexes")
- def test_func_index_multiple_wrapper_references(self):
- index = Index(OrderBy(F("name").desc(), descending=True), name="name")
- msg = (
- "Multiple references to %s can't be used in an indexed expression."
- % self._index_expressions_wrappers()
- )
- with connection.schema_editor() as editor:
- with self.assertRaisesMessage(ValueError, msg):
- editor.add_index(Author, index)
- @skipUnlessDBFeature("supports_expression_indexes")
- def test_func_index_invalid_topmost_expressions(self):
- index = Index(Upper(F("name").desc()), name="name")
- msg = (
- "%s must be topmost expressions in an indexed expression."
- % self._index_expressions_wrappers()
- )
- with connection.schema_editor() as editor:
- with self.assertRaisesMessage(ValueError, msg):
- editor.add_index(Author, index)
- @skipUnlessDBFeature("supports_expression_indexes")
- def test_func_index(self):
- with connection.schema_editor() as editor:
- editor.create_model(Author)
- index = Index(Lower("name").desc(), name="func_lower_idx")
- # Add index.
- with connection.schema_editor() as editor:
- editor.add_index(Author, index)
- sql = index.create_sql(Author, editor)
- table = Author._meta.db_table
- if connection.features.supports_index_column_ordering:
- self.assertIndexOrder(table, index.name, ["DESC"])
- # SQL contains a database function.
- self.assertIs(sql.references_column(table, "name"), True)
- self.assertIn("LOWER(%s)" % editor.quote_name("name"), str(sql))
- # Remove index.
- with connection.schema_editor() as editor:
- editor.remove_index(Author, index)
- self.assertNotIn(index.name, self.get_constraints(table))
- @skipUnlessDBFeature("supports_expression_indexes")
- def test_func_index_f(self):
- with connection.schema_editor() as editor:
- editor.create_model(Tag)
- index = Index("slug", F("title").desc(), name="func_f_idx")
- # Add index.
- with connection.schema_editor() as editor:
- editor.add_index(Tag, index)
- sql = index.create_sql(Tag, editor)
- table = Tag._meta.db_table
- self.assertIn(index.name, self.get_constraints(table))
- if connection.features.supports_index_column_ordering:
- self.assertIndexOrder(Tag._meta.db_table, index.name, ["ASC", "DESC"])
- # SQL contains columns.
- self.assertIs(sql.references_column(table, "slug"), True)
- self.assertIs(sql.references_column(table, "title"), True)
- # Remove index.
- with connection.schema_editor() as editor:
- editor.remove_index(Tag, index)
- self.assertNotIn(index.name, self.get_constraints(table))
- @skipUnlessDBFeature("supports_expression_indexes")
- def test_func_index_lookups(self):
- with connection.schema_editor() as editor:
- editor.create_model(Author)
- with register_lookup(CharField, Lower), register_lookup(IntegerField, Abs):
- index = Index(
- F("name__lower"),
- F("weight__abs"),
- name="func_lower_abs_lookup_idx",
- )
- # Add index.
- with connection.schema_editor() as editor:
- editor.add_index(Author, index)
- sql = index.create_sql(Author, editor)
- table = Author._meta.db_table
- self.assertIn(index.name, self.get_constraints(table))
- # SQL contains columns.
- self.assertIs(sql.references_column(table, "name"), True)
- self.assertIs(sql.references_column(table, "weight"), True)
- # Remove index.
- with connection.schema_editor() as editor:
- editor.remove_index(Author, index)
- self.assertNotIn(index.name, self.get_constraints(table))
- @skipUnlessDBFeature("supports_expression_indexes")
- def test_composite_func_index(self):
- with connection.schema_editor() as editor:
- editor.create_model(Author)
- index = Index(Lower("name"), Upper("name"), name="func_lower_upper_idx")
- # Add index.
- with connection.schema_editor() as editor:
- editor.add_index(Author, index)
- sql = index.create_sql(Author, editor)
- table = Author._meta.db_table
- self.assertIn(index.name, self.get_constraints(table))
- # SQL contains database functions.
- self.assertIs(sql.references_column(table, "name"), True)
- sql = str(sql)
- self.assertIn("LOWER(%s)" % editor.quote_name("name"), sql)
- self.assertIn("UPPER(%s)" % editor.quote_name("name"), sql)
- self.assertLess(sql.index("LOWER"), sql.index("UPPER"))
- # Remove index.
- with connection.schema_editor() as editor:
- editor.remove_index(Author, index)
- self.assertNotIn(index.name, self.get_constraints(table))
- @skipUnlessDBFeature("supports_expression_indexes")
- def test_composite_func_index_field_and_expression(self):
- with connection.schema_editor() as editor:
- editor.create_model(Author)
- editor.create_model(Book)
- index = Index(
- F("author").desc(),
- Lower("title").asc(),
- "pub_date",
- name="func_f_lower_field_idx",
- )
- # Add index.
- with connection.schema_editor() as editor:
- editor.add_index(Book, index)
- sql = index.create_sql(Book, editor)
- table = Book._meta.db_table
- constraints = self.get_constraints(table)
- if connection.features.supports_index_column_ordering:
- self.assertIndexOrder(table, index.name, ["DESC", "ASC", "ASC"])
- self.assertEqual(len(constraints[index.name]["columns"]), 3)
- self.assertEqual(constraints[index.name]["columns"][2], "pub_date")
- # SQL contains database functions and columns.
- self.assertIs(sql.references_column(table, "author_id"), True)
- self.assertIs(sql.references_column(table, "title"), True)
- self.assertIs(sql.references_column(table, "pub_date"), True)
- self.assertIn("LOWER(%s)" % editor.quote_name("title"), str(sql))
- # Remove index.
- with connection.schema_editor() as editor:
- editor.remove_index(Book, index)
- self.assertNotIn(index.name, self.get_constraints(table))
- @skipUnlessDBFeature("supports_expression_indexes")
- @isolate_apps("schema")
- def test_func_index_f_decimalfield(self):
- class Node(Model):
- value = DecimalField(max_digits=5, decimal_places=2)
- class Meta:
- app_label = "schema"
- with connection.schema_editor() as editor:
- editor.create_model(Node)
- index = Index(F("value"), name="func_f_decimalfield_idx")
- # Add index.
- with connection.schema_editor() as editor:
- editor.add_index(Node, index)
- sql = index.create_sql(Node, editor)
- table = Node._meta.db_table
- self.assertIn(index.name, self.get_constraints(table))
- self.assertIs(sql.references_column(table, "value"), True)
- # SQL doesn't contain casting.
- self.assertNotIn("CAST", str(sql))
- # Remove index.
- with connection.schema_editor() as editor:
- editor.remove_index(Node, index)
- self.assertNotIn(index.name, self.get_constraints(table))
- @skipUnlessDBFeature("supports_expression_indexes")
- def test_func_index_cast(self):
- with connection.schema_editor() as editor:
- editor.create_model(Author)
- index = Index(Cast("weight", FloatField()), name="func_cast_idx")
- # Add index.
- with connection.schema_editor() as editor:
- editor.add_index(Author, index)
- sql = index.create_sql(Author, editor)
- table = Author._meta.db_table
- self.assertIn(index.name, self.get_constraints(table))
- self.assertIs(sql.references_column(table, "weight"), True)
- # Remove index.
- with connection.schema_editor() as editor:
- editor.remove_index(Author, index)
- self.assertNotIn(index.name, self.get_constraints(table))
- @skipUnlessDBFeature("supports_expression_indexes")
- def test_func_index_collate(self):
- collation = connection.features.test_collations.get("non_default")
- if not collation:
- self.skipTest("This backend does not support case-insensitive collations.")
- with connection.schema_editor() as editor:
- editor.create_model(Author)
- editor.create_model(BookWithSlug)
- index = Index(
- Collate(F("title"), collation=collation).desc(),
- Collate("slug", collation=collation),
- name="func_collate_idx",
- )
- # Add index.
- with connection.schema_editor() as editor:
- editor.add_index(BookWithSlug, index)
- sql = index.create_sql(BookWithSlug, editor)
- table = Book._meta.db_table
- self.assertIn(index.name, self.get_constraints(table))
- if connection.features.supports_index_column_ordering:
- self.assertIndexOrder(table, index.name, ["DESC", "ASC"])
- # SQL contains columns and a collation.
- self.assertIs(sql.references_column(table, "title"), True)
- self.assertIs(sql.references_column(table, "slug"), True)
- self.assertIn("COLLATE %s" % editor.quote_name(collation), str(sql))
- # Remove index.
- with connection.schema_editor() as editor:
- editor.remove_index(Book, index)
- self.assertNotIn(index.name, self.get_constraints(table))
- @skipUnlessDBFeature("supports_expression_indexes")
- @skipIfDBFeature("collate_as_index_expression")
- def test_func_index_collate_f_ordered(self):
- collation = connection.features.test_collations.get("non_default")
- if not collation:
- self.skipTest("This backend does not support case-insensitive collations.")
- with connection.schema_editor() as editor:
- editor.create_model(Author)
- index = Index(
- Collate(F("name").desc(), collation=collation),
- name="func_collate_f_desc_idx",
- )
- # Add index.
- with connection.schema_editor() as editor:
- editor.add_index(Author, index)
- sql = index.create_sql(Author, editor)
- table = Author._meta.db_table
- self.assertIn(index.name, self.get_constraints(table))
- if connection.features.supports_index_column_ordering:
- self.assertIndexOrder(table, index.name, ["DESC"])
- # SQL contains columns and a collation.
- self.assertIs(sql.references_column(table, "name"), True)
- self.assertIn("COLLATE %s" % editor.quote_name(collation), str(sql))
- # Remove index.
- with connection.schema_editor() as editor:
- editor.remove_index(Author, index)
- self.assertNotIn(index.name, self.get_constraints(table))
- @skipUnlessDBFeature("supports_expression_indexes")
- def test_func_index_calc(self):
- with connection.schema_editor() as editor:
- editor.create_model(Author)
- index = Index(F("height") / (F("weight") + Value(5)), name="func_calc_idx")
- # Add index.
- with connection.schema_editor() as editor:
- editor.add_index(Author, index)
- sql = index.create_sql(Author, editor)
- table = Author._meta.db_table
- self.assertIn(index.name, self.get_constraints(table))
- # SQL contains columns and expressions.
- self.assertIs(sql.references_column(table, "height"), True)
- self.assertIs(sql.references_column(table, "weight"), True)
- sql = str(sql)
- self.assertIs(
- sql.index(editor.quote_name("height"))
- < sql.index("/")
- < sql.index(editor.quote_name("weight"))
- < sql.index("+")
- < sql.index("5"),
- True,
- )
- # Remove index.
- with connection.schema_editor() as editor:
- editor.remove_index(Author, index)
- self.assertNotIn(index.name, self.get_constraints(table))
- @skipUnlessDBFeature("supports_expression_indexes", "supports_json_field")
- @isolate_apps("schema")
- def test_func_index_json_key_transform(self):
- class JSONModel(Model):
- field = JSONField()
- class Meta:
- app_label = "schema"
- with connection.schema_editor() as editor:
- editor.create_model(JSONModel)
- self.isolated_local_models = [JSONModel]
- index = Index("field__some_key", name="func_json_key_idx")
- with connection.schema_editor() as editor:
- editor.add_index(JSONModel, index)
- sql = index.create_sql(JSONModel, editor)
- table = JSONModel._meta.db_table
- self.assertIn(index.name, self.get_constraints(table))
- self.assertIs(sql.references_column(table, "field"), True)
- with connection.schema_editor() as editor:
- editor.remove_index(JSONModel, index)
- self.assertNotIn(index.name, self.get_constraints(table))
- @skipUnlessDBFeature("supports_expression_indexes", "supports_json_field")
- @isolate_apps("schema")
- def test_func_index_json_key_transform_cast(self):
- class JSONModel(Model):
- field = JSONField()
- class Meta:
- app_label = "schema"
- with connection.schema_editor() as editor:
- editor.create_model(JSONModel)
- self.isolated_local_models = [JSONModel]
- index = Index(
- Cast(KeyTextTransform("some_key", "field"), IntegerField()),
- name="func_json_key_cast_idx",
- )
- with connection.schema_editor() as editor:
- editor.add_index(JSONModel, index)
- sql = index.create_sql(JSONModel, editor)
- table = JSONModel._meta.db_table
- self.assertIn(index.name, self.get_constraints(table))
- self.assertIs(sql.references_column(table, "field"), True)
- with connection.schema_editor() as editor:
- editor.remove_index(JSONModel, index)
- self.assertNotIn(index.name, self.get_constraints(table))
- @skipIfDBFeature("supports_expression_indexes")
- def test_func_index_unsupported(self):
- # Index is ignored on databases that don't support indexes on
- # expressions.
- with connection.schema_editor() as editor:
- editor.create_model(Author)
- index = Index(F("name"), name="random_idx")
- with connection.schema_editor() as editor, self.assertNumQueries(0):
- self.assertIsNone(editor.add_index(Author, index))
- self.assertIsNone(editor.remove_index(Author, index))
- @skipUnlessDBFeature("supports_expression_indexes")
- def test_func_index_nonexistent_field(self):
- index = Index(Lower("nonexistent"), name="func_nonexistent_idx")
- msg = (
- "Cannot resolve keyword 'nonexistent' into field. Choices are: "
- "height, id, name, uuid, weight"
- )
- with self.assertRaisesMessage(FieldError, msg):
- with connection.schema_editor() as editor:
- editor.add_index(Author, index)
- @skipUnlessDBFeature("supports_expression_indexes")
- def test_func_index_nondeterministic(self):
- with connection.schema_editor() as editor:
- editor.create_model(Author)
- index = Index(Random(), name="func_random_idx")
- with connection.schema_editor() as editor:
- with self.assertRaises(DatabaseError):
- editor.add_index(Author, index)
- def test_primary_key(self):
- """
- Tests altering of the primary key
- """
- # Create the table
- with connection.schema_editor() as editor:
- editor.create_model(Tag)
- # Ensure the table is there and has the right PK
- self.assertEqual(self.get_primary_key(Tag._meta.db_table), "id")
- # Alter to change the PK
- id_field = Tag._meta.get_field("id")
- old_field = Tag._meta.get_field("slug")
- new_field = SlugField(primary_key=True)
- new_field.set_attributes_from_name("slug")
- new_field.model = Tag
- with connection.schema_editor() as editor:
- editor.remove_field(Tag, id_field)
- editor.alter_field(Tag, old_field, new_field)
- # Ensure the PK changed
- self.assertNotIn(
- "id",
- self.get_indexes(Tag._meta.db_table),
- )
- self.assertEqual(self.get_primary_key(Tag._meta.db_table), "slug")
- def test_alter_primary_key_the_same_name(self):
- with connection.schema_editor() as editor:
- editor.create_model(Thing)
- old_field = Thing._meta.get_field("when")
- new_field = CharField(max_length=2, primary_key=True)
- new_field.set_attributes_from_name("when")
- new_field.model = Thing
- with connection.schema_editor() as editor:
- editor.alter_field(Thing, old_field, new_field, strict=True)
- self.assertEqual(self.get_primary_key(Thing._meta.db_table), "when")
- with connection.schema_editor() as editor:
- editor.alter_field(Thing, new_field, old_field, strict=True)
- self.assertEqual(self.get_primary_key(Thing._meta.db_table), "when")
- def test_context_manager_exit(self):
- """
- Ensures transaction is correctly closed when an error occurs
- inside a SchemaEditor context.
- """
- class SomeError(Exception):
- pass
- try:
- with connection.schema_editor():
- raise SomeError
- except SomeError:
- self.assertFalse(connection.in_atomic_block)
- @skipIfDBFeature("can_rollback_ddl")
- def test_unsupported_transactional_ddl_disallowed(self):
- message = (
- "Executing DDL statements while in a transaction on databases "
- "that can't perform a rollback is prohibited."
- )
- with atomic(), connection.schema_editor() as editor:
- with self.assertRaisesMessage(TransactionManagementError, message):
- editor.execute(
- editor.sql_create_table % {"table": "foo", "definition": ""}
- )
- @skipUnlessDBFeature("supports_foreign_keys", "indexes_foreign_keys")
- def test_foreign_key_index_long_names_regression(self):
- """
- Regression test for #21497.
- Only affects databases that supports foreign keys.
- """
- # Create the table
- with connection.schema_editor() as editor:
- editor.create_model(AuthorWithEvenLongerName)
- editor.create_model(BookWithLongName)
- # Find the properly shortened column name
- column_name = connection.ops.quote_name(
- "author_foreign_key_with_really_long_field_name_id"
- )
- column_name = column_name[1:-1].lower() # unquote, and, for Oracle, un-upcase
- # Ensure the table is there and has an index on the column
- self.assertIn(
- column_name,
- self.get_indexes(BookWithLongName._meta.db_table),
- )
- @skipUnlessDBFeature("supports_foreign_keys")
- def test_add_foreign_key_long_names(self):
- """
- Regression test for #23009.
- Only affects databases that supports foreign keys.
- """
- # Create the initial tables
- with connection.schema_editor() as editor:
- editor.create_model(AuthorWithEvenLongerName)
- editor.create_model(BookWithLongName)
- # Add a second FK, this would fail due to long ref name before the fix
- new_field = ForeignKey(
- AuthorWithEvenLongerName, CASCADE, related_name="something"
- )
- new_field.set_attributes_from_name(
- "author_other_really_long_named_i_mean_so_long_fk"
- )
- with connection.schema_editor() as editor:
- editor.add_field(BookWithLongName, new_field)
- @isolate_apps("schema")
- @skipUnlessDBFeature("supports_foreign_keys")
- def test_add_foreign_key_quoted_db_table(self):
- class Author(Model):
- class Meta:
- db_table = '"table_author_double_quoted"'
- app_label = "schema"
- class Book(Model):
- author = ForeignKey(Author, CASCADE)
- class Meta:
- app_label = "schema"
- with connection.schema_editor() as editor:
- editor.create_model(Author)
- editor.create_model(Book)
- self.isolated_local_models = [Author]
- if connection.vendor == "mysql":
- self.assertForeignKeyExists(
- Book, "author_id", '"table_author_double_quoted"'
- )
- else:
- self.assertForeignKeyExists(Book, "author_id", "table_author_double_quoted")
- def test_add_foreign_object(self):
- with connection.schema_editor() as editor:
- editor.create_model(BookForeignObj)
- self.local_models = [BookForeignObj]
- new_field = ForeignObject(
- Author, on_delete=CASCADE, from_fields=["author_id"], to_fields=["id"]
- )
- new_field.set_attributes_from_name("author")
- with connection.schema_editor() as editor:
- editor.add_field(BookForeignObj, new_field)
- def test_creation_deletion_reserved_names(self):
- """
- Tries creating a model's table, and then deleting it when it has a
- SQL reserved name.
- """
- # Create the table
- with connection.schema_editor() as editor:
- try:
- editor.create_model(Thing)
- except OperationalError as e:
- self.fail(
- "Errors when applying initial migration for a model "
- "with a table named after an SQL reserved word: %s" % e
- )
- # The table is there
- list(Thing.objects.all())
- # Clean up that table
- with connection.schema_editor() as editor:
- editor.delete_model(Thing)
- # The table is gone
- with self.assertRaises(DatabaseError):
- list(Thing.objects.all())
- def test_remove_constraints_capital_letters(self):
- """
- #23065 - Constraint names must be quoted if they contain capital letters.
- """
- def get_field(*args, field_class=IntegerField, **kwargs):
- kwargs["db_column"] = "CamelCase"
- field = field_class(*args, **kwargs)
- field.set_attributes_from_name("CamelCase")
- return field
- model = Author
- field = get_field()
- table = model._meta.db_table
- column = field.column
- identifier_converter = connection.introspection.identifier_converter
- with connection.schema_editor() as editor:
- editor.create_model(model)
- editor.add_field(model, field)
- constraint_name = "CamelCaseIndex"
- expected_constraint_name = identifier_converter(constraint_name)
- editor.execute(
- editor.sql_create_index
- % {
- "table": editor.quote_name(table),
- "name": editor.quote_name(constraint_name),
- "using": "",
- "columns": editor.quote_name(column),
- "extra": "",
- "condition": "",
- "include": "",
- }
- )
- self.assertIn(
- expected_constraint_name, self.get_constraints(model._meta.db_table)
- )
- editor.alter_field(model, get_field(db_index=True), field, strict=True)
- self.assertNotIn(
- expected_constraint_name, self.get_constraints(model._meta.db_table)
- )
- constraint_name = "CamelCaseUniqConstraint"
- expected_constraint_name = identifier_converter(constraint_name)
- editor.execute(editor._create_unique_sql(model, [field], constraint_name))
- self.assertIn(
- expected_constraint_name, self.get_constraints(model._meta.db_table)
- )
- editor.alter_field(model, get_field(unique=True), field, strict=True)
- self.assertNotIn(
- expected_constraint_name, self.get_constraints(model._meta.db_table)
- )
- if editor.sql_create_fk and connection.features.can_introspect_foreign_keys:
- constraint_name = "CamelCaseFKConstraint"
- expected_constraint_name = identifier_converter(constraint_name)
- editor.execute(
- editor.sql_create_fk
- % {
- "table": editor.quote_name(table),
- "name": editor.quote_name(constraint_name),
- "column": editor.quote_name(column),
- "to_table": editor.quote_name(table),
- "to_column": editor.quote_name(model._meta.auto_field.column),
- "deferrable": connection.ops.deferrable_sql(),
- }
- )
- self.assertIn(
- expected_constraint_name, self.get_constraints(model._meta.db_table)
- )
- editor.alter_field(
- model,
- get_field(Author, CASCADE, field_class=ForeignKey),
- field,
- strict=True,
- )
- self.assertNotIn(
- expected_constraint_name, self.get_constraints(model._meta.db_table)
- )
- def test_add_field_use_effective_default(self):
- """
- #23987 - effective_default() should be used as the field default when
- adding a new field.
- """
- # Create the table
- with connection.schema_editor() as editor:
- editor.create_model(Author)
- # Ensure there's no surname field
- columns = self.column_classes(Author)
- self.assertNotIn("surname", columns)
- # Create a row
- Author.objects.create(name="Anonymous1")
- # Add new CharField to ensure default will be used from effective_default
- new_field = CharField(max_length=15, blank=True)
- new_field.set_attributes_from_name("surname")
- with connection.schema_editor() as editor:
- editor.add_field(Author, new_field)
- # Ensure field was added with the right default
- with connection.cursor() as cursor:
- cursor.execute("SELECT surname FROM schema_author;")
- item = cursor.fetchall()[0]
- self.assertEqual(
- item[0],
- None if connection.features.interprets_empty_strings_as_nulls else "",
- )
- def test_add_field_default_dropped(self):
- # Create the table
- with connection.schema_editor() as editor:
- editor.create_model(Author)
- # Ensure there's no surname field
- columns = self.column_classes(Author)
- self.assertNotIn("surname", columns)
- # Create a row
- Author.objects.create(name="Anonymous1")
- # Add new CharField with a default
- new_field = CharField(max_length=15, blank=True, default="surname default")
- new_field.set_attributes_from_name("surname")
- with connection.schema_editor() as editor:
- editor.add_field(Author, new_field)
- # Ensure field was added with the right default
- with connection.cursor() as cursor:
- cursor.execute("SELECT surname FROM schema_author;")
- item = cursor.fetchall()[0]
- self.assertEqual(item[0], "surname default")
- # And that the default is no longer set in the database.
- field = next(
- f
- for f in connection.introspection.get_table_description(
- cursor, "schema_author"
- )
- if f.name == "surname"
- )
- if connection.features.can_introspect_default:
- self.assertIsNone(field.default)
- def test_add_field_default_nullable(self):
- with connection.schema_editor() as editor:
- editor.create_model(Author)
- # Add new nullable CharField with a default.
- new_field = CharField(max_length=15, blank=True, null=True, default="surname")
- new_field.set_attributes_from_name("surname")
- with connection.schema_editor() as editor:
- editor.add_field(Author, new_field)
- Author.objects.create(name="Anonymous1")
- with connection.cursor() as cursor:
- cursor.execute("SELECT surname FROM schema_author;")
- item = cursor.fetchall()[0]
- self.assertIsNone(item[0])
- field = next(
- f
- for f in connection.introspection.get_table_description(
- cursor,
- "schema_author",
- )
- if f.name == "surname"
- )
- # Field is still nullable.
- self.assertTrue(field.null_ok)
- # The database default is no longer set.
- if connection.features.can_introspect_default:
- self.assertIn(field.default, ["NULL", None])
- def test_add_textfield_default_nullable(self):
- with connection.schema_editor() as editor:
- editor.create_model(Author)
- # Add new nullable TextField with a default.
- new_field = TextField(blank=True, null=True, default="text")
- new_field.set_attributes_from_name("description")
- with connection.schema_editor() as editor:
- editor.add_field(Author, new_field)
- Author.objects.create(name="Anonymous1")
- with connection.cursor() as cursor:
- cursor.execute("SELECT description FROM schema_author;")
- item = cursor.fetchall()[0]
- self.assertIsNone(item[0])
- field = next(
- f
- for f in connection.introspection.get_table_description(
- cursor,
- "schema_author",
- )
- if f.name == "description"
- )
- # Field is still nullable.
- self.assertTrue(field.null_ok)
- # The database default is no longer set.
- if connection.features.can_introspect_default:
- self.assertIn(field.default, ["NULL", None])
- def test_alter_field_default_dropped(self):
- # Create the table
- with connection.schema_editor() as editor:
- editor.create_model(Author)
- # Create a row
- Author.objects.create(name="Anonymous1")
- self.assertIsNone(Author.objects.get().height)
- old_field = Author._meta.get_field("height")
- # The default from the new field is used in updating existing rows.
- new_field = IntegerField(blank=True, default=42)
- new_field.set_attributes_from_name("height")
- with connection.schema_editor() as editor:
- editor.alter_field(Author, old_field, new_field, strict=True)
- self.assertEqual(Author.objects.get().height, 42)
- # The database default should be removed.
- with connection.cursor() as cursor:
- field = next(
- f
- for f in connection.introspection.get_table_description(
- cursor, "schema_author"
- )
- if f.name == "height"
- )
- if connection.features.can_introspect_default:
- self.assertIsNone(field.default)
- def test_alter_field_default_doesnt_perform_queries(self):
- """
- No queries are performed if a field default changes and the field's
- not changing from null to non-null.
- """
- with connection.schema_editor() as editor:
- editor.create_model(AuthorWithDefaultHeight)
- old_field = AuthorWithDefaultHeight._meta.get_field("height")
- new_default = old_field.default * 2
- new_field = PositiveIntegerField(null=True, blank=True, default=new_default)
- new_field.set_attributes_from_name("height")
- with connection.schema_editor() as editor, self.assertNumQueries(0):
- editor.alter_field(
- AuthorWithDefaultHeight, old_field, new_field, strict=True
- )
- @skipUnlessDBFeature("supports_foreign_keys")
- def test_alter_field_fk_attributes_noop(self):
- """
- No queries are performed when changing field attributes that don't
- affect the schema.
- """
- with connection.schema_editor() as editor:
- editor.create_model(Author)
- editor.create_model(Book)
- old_field = Book._meta.get_field("author")
- new_field = ForeignKey(
- Author,
- blank=True,
- editable=False,
- error_messages={"invalid": "error message"},
- help_text="help text",
- limit_choices_to={"limit": "choice"},
- on_delete=PROTECT,
- related_name="related_name",
- related_query_name="related_query_name",
- validators=[lambda x: x],
- verbose_name="verbose name",
- )
- new_field.set_attributes_from_name("author")
- with connection.schema_editor() as editor, self.assertNumQueries(0):
- editor.alter_field(Book, old_field, new_field, strict=True)
- with connection.schema_editor() as editor, self.assertNumQueries(0):
- editor.alter_field(Book, new_field, old_field, strict=True)
- def test_alter_field_choices_noop(self):
- with connection.schema_editor() as editor:
- editor.create_model(Author)
- old_field = Author._meta.get_field("name")
- new_field = CharField(
- choices=(("Jane", "Jane"), ("Joe", "Joe")),
- max_length=255,
- )
- new_field.set_attributes_from_name("name")
- with connection.schema_editor() as editor, self.assertNumQueries(0):
- editor.alter_field(Author, old_field, new_field, strict=True)
- with connection.schema_editor() as editor, self.assertNumQueries(0):
- editor.alter_field(Author, new_field, old_field, strict=True)
- def test_add_textfield_unhashable_default(self):
- # Create the table
- with connection.schema_editor() as editor:
- editor.create_model(Author)
- # Create a row
- Author.objects.create(name="Anonymous1")
- # Create a field that has an unhashable default
- new_field = TextField(default={})
- new_field.set_attributes_from_name("info")
- with connection.schema_editor() as editor:
- editor.add_field(Author, new_field)
- @unittest.skipUnless(connection.vendor == "postgresql", "PostgreSQL specific")
- def test_add_indexed_charfield(self):
- field = CharField(max_length=255, db_index=True)
- field.set_attributes_from_name("nom_de_plume")
- with connection.schema_editor() as editor:
- editor.create_model(Author)
- editor.add_field(Author, field)
- # Should create two indexes; one for like operator.
- self.assertEqual(
- self.get_constraints_for_column(Author, "nom_de_plume"),
- [
- "schema_author_nom_de_plume_7570a851",
- "schema_author_nom_de_plume_7570a851_like",
- ],
- )
- @unittest.skipUnless(connection.vendor == "postgresql", "PostgreSQL specific")
- def test_add_unique_charfield(self):
- field = CharField(max_length=255, unique=True)
- field.set_attributes_from_name("nom_de_plume")
- with connection.schema_editor() as editor:
- editor.create_model(Author)
- editor.add_field(Author, field)
- # Should create two indexes; one for like operator.
- self.assertEqual(
- self.get_constraints_for_column(Author, "nom_de_plume"),
- [
- "schema_author_nom_de_plume_7570a851_like",
- "schema_author_nom_de_plume_key",
- ],
- )
- @unittest.skipUnless(connection.vendor == "postgresql", "PostgreSQL specific")
- def test_alter_field_add_index_to_charfield(self):
- # Create the table and verify no initial indexes.
- with connection.schema_editor() as editor:
- editor.create_model(Author)
- self.assertEqual(self.get_constraints_for_column(Author, "name"), [])
- # Alter to add db_index=True and create 2 indexes.
- old_field = Author._meta.get_field("name")
- new_field = CharField(max_length=255, db_index=True)
- new_field.set_attributes_from_name("name")
- with connection.schema_editor() as editor:
- editor.alter_field(Author, old_field, new_field, strict=True)
- self.assertEqual(
- self.get_constraints_for_column(Author, "name"),
- ["schema_author_name_1fbc5617", "schema_author_name_1fbc5617_like"],
- )
- # Remove db_index=True to drop both indexes.
- with connection.schema_editor() as editor:
- editor.alter_field(Author, new_field, old_field, strict=True)
- self.assertEqual(self.get_constraints_for_column(Author, "name"), [])
- @unittest.skipUnless(connection.vendor == "postgresql", "PostgreSQL specific")
- def test_alter_field_add_unique_to_charfield(self):
- # Create the table and verify no initial indexes.
- with connection.schema_editor() as editor:
- editor.create_model(Author)
- self.assertEqual(self.get_constraints_for_column(Author, "name"), [])
- # Alter to add unique=True and create 2 indexes.
- old_field = Author._meta.get_field("name")
- new_field = CharField(max_length=255, unique=True)
- new_field.set_attributes_from_name("name")
- with connection.schema_editor() as editor:
- editor.alter_field(Author, old_field, new_field, strict=True)
- self.assertEqual(
- self.get_constraints_for_column(Author, "name"),
- ["schema_author_name_1fbc5617_like", "schema_author_name_1fbc5617_uniq"],
- )
- # Remove unique=True to drop both indexes.
- with connection.schema_editor() as editor:
- editor.alter_field(Author, new_field, old_field, strict=True)
- self.assertEqual(self.get_constraints_for_column(Author, "name"), [])
- @unittest.skipUnless(connection.vendor == "postgresql", "PostgreSQL specific")
- def test_alter_field_add_index_to_textfield(self):
- # Create the table and verify no initial indexes.
- with connection.schema_editor() as editor:
- editor.create_model(Note)
- self.assertEqual(self.get_constraints_for_column(Note, "info"), [])
- # Alter to add db_index=True and create 2 indexes.
- old_field = Note._meta.get_field("info")
- new_field = TextField(db_index=True)
- new_field.set_attributes_from_name("info")
- with connection.schema_editor() as editor:
- editor.alter_field(Note, old_field, new_field, strict=True)
- self.assertEqual(
- self.get_constraints_for_column(Note, "info"),
- ["schema_note_info_4b0ea695", "schema_note_info_4b0ea695_like"],
- )
- # Remove db_index=True to drop both indexes.
- with connection.schema_editor() as editor:
- editor.alter_field(Note, new_field, old_field, strict=True)
- self.assertEqual(self.get_constraints_for_column(Note, "info"), [])
- @unittest.skipUnless(connection.vendor == "postgresql", "PostgreSQL specific")
- def test_alter_field_add_unique_to_charfield_with_db_index(self):
- # Create the table and verify initial indexes.
- with connection.schema_editor() as editor:
- editor.create_model(BookWithoutAuthor)
- self.assertEqual(
- self.get_constraints_for_column(BookWithoutAuthor, "title"),
- ["schema_book_title_2dfb2dff", "schema_book_title_2dfb2dff_like"],
- )
- # Alter to add unique=True (should replace the index)
- old_field = BookWithoutAuthor._meta.get_field("title")
- new_field = CharField(max_length=100, db_index=True, unique=True)
- new_field.set_attributes_from_name("title")
- with connection.schema_editor() as editor:
- editor.alter_field(BookWithoutAuthor, old_field, new_field, strict=True)
- self.assertEqual(
- self.get_constraints_for_column(BookWithoutAuthor, "title"),
- ["schema_book_title_2dfb2dff_like", "schema_book_title_2dfb2dff_uniq"],
- )
- # Alter to remove unique=True (should drop unique index)
- new_field2 = CharField(max_length=100, db_index=True)
- new_field2.set_attributes_from_name("title")
- with connection.schema_editor() as editor:
- editor.alter_field(BookWithoutAuthor, new_field, new_field2, strict=True)
- self.assertEqual(
- self.get_constraints_for_column(BookWithoutAuthor, "title"),
- ["schema_book_title_2dfb2dff", "schema_book_title_2dfb2dff_like"],
- )
- @unittest.skipUnless(connection.vendor == "postgresql", "PostgreSQL specific")
- def test_alter_field_remove_unique_and_db_index_from_charfield(self):
- # Create the table and verify initial indexes.
- with connection.schema_editor() as editor:
- editor.create_model(BookWithoutAuthor)
- self.assertEqual(
- self.get_constraints_for_column(BookWithoutAuthor, "title"),
- ["schema_book_title_2dfb2dff", "schema_book_title_2dfb2dff_like"],
- )
- # Alter to add unique=True (should replace the index)
- old_field = BookWithoutAuthor._meta.get_field("title")
- new_field = CharField(max_length=100, db_index=True, unique=True)
- new_field.set_attributes_from_name("title")
- with connection.schema_editor() as editor:
- editor.alter_field(BookWithoutAuthor, old_field, new_field, strict=True)
- self.assertEqual(
- self.get_constraints_for_column(BookWithoutAuthor, "title"),
- ["schema_book_title_2dfb2dff_like", "schema_book_title_2dfb2dff_uniq"],
- )
- # Alter to remove both unique=True and db_index=True (should drop all indexes)
- new_field2 = CharField(max_length=100)
- new_field2.set_attributes_from_name("title")
- with connection.schema_editor() as editor:
- editor.alter_field(BookWithoutAuthor, new_field, new_field2, strict=True)
- self.assertEqual(
- self.get_constraints_for_column(BookWithoutAuthor, "title"), []
- )
- @unittest.skipUnless(connection.vendor == "postgresql", "PostgreSQL specific")
- def test_alter_field_swap_unique_and_db_index_with_charfield(self):
- # Create the table and verify initial indexes.
- with connection.schema_editor() as editor:
- editor.create_model(BookWithoutAuthor)
- self.assertEqual(
- self.get_constraints_for_column(BookWithoutAuthor, "title"),
- ["schema_book_title_2dfb2dff", "schema_book_title_2dfb2dff_like"],
- )
- # Alter to set unique=True and remove db_index=True (should replace the index)
- old_field = BookWithoutAuthor._meta.get_field("title")
- new_field = CharField(max_length=100, unique=True)
- new_field.set_attributes_from_name("title")
- with connection.schema_editor() as editor:
- editor.alter_field(BookWithoutAuthor, old_field, new_field, strict=True)
- self.assertEqual(
- self.get_constraints_for_column(BookWithoutAuthor, "title"),
- ["schema_book_title_2dfb2dff_like", "schema_book_title_2dfb2dff_uniq"],
- )
- # Alter to set db_index=True and remove unique=True (should restore index)
- new_field2 = CharField(max_length=100, db_index=True)
- new_field2.set_attributes_from_name("title")
- with connection.schema_editor() as editor:
- editor.alter_field(BookWithoutAuthor, new_field, new_field2, strict=True)
- self.assertEqual(
- self.get_constraints_for_column(BookWithoutAuthor, "title"),
- ["schema_book_title_2dfb2dff", "schema_book_title_2dfb2dff_like"],
- )
- @unittest.skipUnless(connection.vendor == "postgresql", "PostgreSQL specific")
- def test_alter_field_add_db_index_to_charfield_with_unique(self):
- # Create the table and verify initial indexes.
- with connection.schema_editor() as editor:
- editor.create_model(Tag)
- self.assertEqual(
- self.get_constraints_for_column(Tag, "slug"),
- ["schema_tag_slug_2c418ba3_like", "schema_tag_slug_key"],
- )
- # Alter to add db_index=True
- old_field = Tag._meta.get_field("slug")
- new_field = SlugField(db_index=True, unique=True)
- new_field.set_attributes_from_name("slug")
- with connection.schema_editor() as editor:
- editor.alter_field(Tag, old_field, new_field, strict=True)
- self.assertEqual(
- self.get_constraints_for_column(Tag, "slug"),
- ["schema_tag_slug_2c418ba3_like", "schema_tag_slug_key"],
- )
- # Alter to remove db_index=True
- new_field2 = SlugField(unique=True)
- new_field2.set_attributes_from_name("slug")
- with connection.schema_editor() as editor:
- editor.alter_field(Tag, new_field, new_field2, strict=True)
- self.assertEqual(
- self.get_constraints_for_column(Tag, "slug"),
- ["schema_tag_slug_2c418ba3_like", "schema_tag_slug_key"],
- )
- def test_alter_field_add_index_to_integerfield(self):
- # Create the table and verify no initial indexes.
- with connection.schema_editor() as editor:
- editor.create_model(Author)
- self.assertEqual(self.get_constraints_for_column(Author, "weight"), [])
- # Alter to add db_index=True and create index.
- old_field = Author._meta.get_field("weight")
- new_field = IntegerField(null=True, db_index=True)
- new_field.set_attributes_from_name("weight")
- with connection.schema_editor() as editor:
- editor.alter_field(Author, old_field, new_field, strict=True)
- self.assertEqual(
- self.get_constraints_for_column(Author, "weight"),
- ["schema_author_weight_587740f9"],
- )
- # Remove db_index=True to drop index.
- with connection.schema_editor() as editor:
- editor.alter_field(Author, new_field, old_field, strict=True)
- self.assertEqual(self.get_constraints_for_column(Author, "weight"), [])
- def test_alter_pk_with_self_referential_field(self):
- """
- Changing the primary key field name of a model with a self-referential
- foreign key (#26384).
- """
- with connection.schema_editor() as editor:
- editor.create_model(Node)
- old_field = Node._meta.get_field("node_id")
- new_field = AutoField(primary_key=True)
- new_field.set_attributes_from_name("id")
- with connection.schema_editor() as editor:
- editor.alter_field(Node, old_field, new_field, strict=True)
- self.assertForeignKeyExists(Node, "parent_id", Node._meta.db_table)
- @mock.patch("django.db.backends.base.schema.datetime")
- @mock.patch("django.db.backends.base.schema.timezone")
- def test_add_datefield_and_datetimefield_use_effective_default(
- self, mocked_datetime, mocked_tz
- ):
- """
- effective_default() should be used for DateField, DateTimeField, and
- TimeField if auto_now or auto_now_add is set (#25005).
- """
- now = datetime.datetime(month=1, day=1, year=2000, hour=1, minute=1)
- now_tz = datetime.datetime(
- month=1, day=1, year=2000, hour=1, minute=1, tzinfo=datetime.timezone.utc
- )
- mocked_datetime.now = mock.MagicMock(return_value=now)
- mocked_tz.now = mock.MagicMock(return_value=now_tz)
- # Create the table
- with connection.schema_editor() as editor:
- editor.create_model(Author)
- # Check auto_now/auto_now_add attributes are not defined
- columns = self.column_classes(Author)
- self.assertNotIn("dob_auto_now", columns)
- self.assertNotIn("dob_auto_now_add", columns)
- self.assertNotIn("dtob_auto_now", columns)
- self.assertNotIn("dtob_auto_now_add", columns)
- self.assertNotIn("tob_auto_now", columns)
- self.assertNotIn("tob_auto_now_add", columns)
- # Create a row
- Author.objects.create(name="Anonymous1")
- # Ensure fields were added with the correct defaults
- dob_auto_now = DateField(auto_now=True)
- dob_auto_now.set_attributes_from_name("dob_auto_now")
- self.check_added_field_default(
- editor,
- Author,
- dob_auto_now,
- "dob_auto_now",
- now.date(),
- cast_function=lambda x: x.date(),
- )
- dob_auto_now_add = DateField(auto_now_add=True)
- dob_auto_now_add.set_attributes_from_name("dob_auto_now_add")
- self.check_added_field_default(
- editor,
- Author,
- dob_auto_now_add,
- "dob_auto_now_add",
- now.date(),
- cast_function=lambda x: x.date(),
- )
- dtob_auto_now = DateTimeField(auto_now=True)
- dtob_auto_now.set_attributes_from_name("dtob_auto_now")
- self.check_added_field_default(
- editor,
- Author,
- dtob_auto_now,
- "dtob_auto_now",
- now,
- )
- dt_tm_of_birth_auto_now_add = DateTimeField(auto_now_add=True)
- dt_tm_of_birth_auto_now_add.set_attributes_from_name("dtob_auto_now_add")
- self.check_added_field_default(
- editor,
- Author,
- dt_tm_of_birth_auto_now_add,
- "dtob_auto_now_add",
- now,
- )
- tob_auto_now = TimeField(auto_now=True)
- tob_auto_now.set_attributes_from_name("tob_auto_now")
- self.check_added_field_default(
- editor,
- Author,
- tob_auto_now,
- "tob_auto_now",
- now.time(),
- cast_function=lambda x: x.time(),
- )
- tob_auto_now_add = TimeField(auto_now_add=True)
- tob_auto_now_add.set_attributes_from_name("tob_auto_now_add")
- self.check_added_field_default(
- editor,
- Author,
- tob_auto_now_add,
- "tob_auto_now_add",
- now.time(),
- cast_function=lambda x: x.time(),
- )
- def test_namespaced_db_table_create_index_name(self):
- """
- Table names are stripped of their namespace/schema before being used to
- generate index names.
- """
- with connection.schema_editor() as editor:
- max_name_length = connection.ops.max_name_length() or 200
- namespace = "n" * max_name_length
- table_name = "t" * max_name_length
- namespaced_table_name = '"%s"."%s"' % (namespace, table_name)
- self.assertEqual(
- editor._create_index_name(table_name, []),
- editor._create_index_name(namespaced_table_name, []),
- )
- @unittest.skipUnless(
- connection.vendor == "oracle", "Oracle specific db_table syntax"
- )
- def test_creation_with_db_table_double_quotes(self):
- oracle_user = connection.creation._test_database_user()
- class Student(Model):
- name = CharField(max_length=30)
- class Meta:
- app_label = "schema"
- apps = new_apps
- db_table = '"%s"."DJANGO_STUDENT_TABLE"' % oracle_user
- class Document(Model):
- name = CharField(max_length=30)
- students = ManyToManyField(Student)
- class Meta:
- app_label = "schema"
- apps = new_apps
- db_table = '"%s"."DJANGO_DOCUMENT_TABLE"' % oracle_user
- self.isolated_local_models = [Student, Document]
- with connection.schema_editor() as editor:
- editor.create_model(Student)
- editor.create_model(Document)
- doc = Document.objects.create(name="Test Name")
- student = Student.objects.create(name="Some man")
- doc.students.add(student)
- @isolate_apps("schema")
- @unittest.skipUnless(
- connection.vendor == "postgresql", "PostgreSQL specific db_table syntax."
- )
- def test_namespaced_db_table_foreign_key_reference(self):
- with connection.cursor() as cursor:
- cursor.execute("CREATE SCHEMA django_schema_tests")
- def delete_schema():
- with connection.cursor() as cursor:
- cursor.execute("DROP SCHEMA django_schema_tests CASCADE")
- self.addCleanup(delete_schema)
- class Author(Model):
- class Meta:
- app_label = "schema"
- class Book(Model):
- class Meta:
- app_label = "schema"
- db_table = '"django_schema_tests"."schema_book"'
- author = ForeignKey(Author, CASCADE)
- author.set_attributes_from_name("author")
- with connection.schema_editor() as editor:
- editor.create_model(Author)
- editor.create_model(Book)
- editor.add_field(Book, author)
- def test_rename_table_renames_deferred_sql_references(self):
- atomic_rename = connection.features.supports_atomic_references_rename
- with connection.schema_editor(atomic=atomic_rename) as editor:
- editor.create_model(Author)
- editor.create_model(Book)
- editor.alter_db_table(Author, "schema_author", "schema_renamed_author")
- editor.alter_db_table(Author, "schema_book", "schema_renamed_book")
- try:
- self.assertGreater(len(editor.deferred_sql), 0)
- for statement in editor.deferred_sql:
- self.assertIs(statement.references_table("schema_author"), False)
- self.assertIs(statement.references_table("schema_book"), False)
- finally:
- editor.alter_db_table(Author, "schema_renamed_author", "schema_author")
- editor.alter_db_table(Author, "schema_renamed_book", "schema_book")
- def test_rename_column_renames_deferred_sql_references(self):
- with connection.schema_editor() as editor:
- editor.create_model(Author)
- editor.create_model(Book)
- old_title = Book._meta.get_field("title")
- new_title = CharField(max_length=100, db_index=True)
- new_title.set_attributes_from_name("renamed_title")
- editor.alter_field(Book, old_title, new_title)
- old_author = Book._meta.get_field("author")
- new_author = ForeignKey(Author, CASCADE)
- new_author.set_attributes_from_name("renamed_author")
- editor.alter_field(Book, old_author, new_author)
- self.assertGreater(len(editor.deferred_sql), 0)
- for statement in editor.deferred_sql:
- self.assertIs(statement.references_column("book", "title"), False)
- self.assertIs(statement.references_column("book", "author_id"), False)
- @isolate_apps("schema")
- def test_referenced_field_without_constraint_rename_inside_atomic_block(self):
- """
- Foreign keys without database level constraint don't prevent the field
- they reference from being renamed in an atomic block.
- """
- class Foo(Model):
- field = CharField(max_length=255, unique=True)
- class Meta:
- app_label = "schema"
- class Bar(Model):
- foo = ForeignKey(Foo, CASCADE, to_field="field", db_constraint=False)
- class Meta:
- app_label = "schema"
- self.isolated_local_models = [Foo, Bar]
- with connection.schema_editor() as editor:
- editor.create_model(Foo)
- editor.create_model(Bar)
- new_field = CharField(max_length=255, unique=True)
- new_field.set_attributes_from_name("renamed")
- with connection.schema_editor(atomic=True) as editor:
- editor.alter_field(Foo, Foo._meta.get_field("field"), new_field)
- @isolate_apps("schema")
- def test_referenced_table_without_constraint_rename_inside_atomic_block(self):
- """
- Foreign keys without database level constraint don't prevent the table
- they reference from being renamed in an atomic block.
- """
- class Foo(Model):
- field = CharField(max_length=255, unique=True)
- class Meta:
- app_label = "schema"
- class Bar(Model):
- foo = ForeignKey(Foo, CASCADE, to_field="field", db_constraint=False)
- class Meta:
- app_label = "schema"
- self.isolated_local_models = [Foo, Bar]
- with connection.schema_editor() as editor:
- editor.create_model(Foo)
- editor.create_model(Bar)
- new_field = CharField(max_length=255, unique=True)
- new_field.set_attributes_from_name("renamed")
- with connection.schema_editor(atomic=True) as editor:
- editor.alter_db_table(Foo, Foo._meta.db_table, "renamed_table")
- Foo._meta.db_table = "renamed_table"
- @isolate_apps("schema")
- @skipUnlessDBFeature("supports_collation_on_charfield")
- def test_db_collation_charfield(self):
- collation = connection.features.test_collations.get("non_default")
- if not collation:
- self.skipTest("Language collations are not supported.")
- class Foo(Model):
- field = CharField(max_length=255, db_collation=collation)
- class Meta:
- app_label = "schema"
- self.isolated_local_models = [Foo]
- with connection.schema_editor() as editor:
- editor.create_model(Foo)
- self.assertEqual(
- self.get_column_collation(Foo._meta.db_table, "field"),
- collation,
- )
- @isolate_apps("schema")
- @skipUnlessDBFeature("supports_collation_on_textfield")
- def test_db_collation_textfield(self):
- collation = connection.features.test_collations.get("non_default")
- if not collation:
- self.skipTest("Language collations are not supported.")
- class Foo(Model):
- field = TextField(db_collation=collation)
- class Meta:
- app_label = "schema"
- self.isolated_local_models = [Foo]
- with connection.schema_editor() as editor:
- editor.create_model(Foo)
- self.assertEqual(
- self.get_column_collation(Foo._meta.db_table, "field"),
- collation,
- )
- @skipUnlessDBFeature("supports_collation_on_charfield")
- def test_add_field_db_collation(self):
- collation = connection.features.test_collations.get("non_default")
- if not collation:
- self.skipTest("Language collations are not supported.")
- with connection.schema_editor() as editor:
- editor.create_model(Author)
- new_field = CharField(max_length=255, db_collation=collation)
- new_field.set_attributes_from_name("alias")
- with connection.schema_editor() as editor:
- editor.add_field(Author, new_field)
- columns = self.column_classes(Author)
- self.assertEqual(
- columns["alias"][0],
- connection.features.introspected_field_types["CharField"],
- )
- self.assertEqual(columns["alias"][1][8], collation)
- @skipUnlessDBFeature("supports_collation_on_charfield")
- def test_alter_field_db_collation(self):
- collation = connection.features.test_collations.get("non_default")
- if not collation:
- self.skipTest("Language collations are not supported.")
- with connection.schema_editor() as editor:
- editor.create_model(Author)
- old_field = Author._meta.get_field("name")
- new_field = CharField(max_length=255, db_collation=collation)
- new_field.set_attributes_from_name("name")
- new_field.model = Author
- with connection.schema_editor() as editor:
- editor.alter_field(Author, old_field, new_field, strict=True)
- self.assertEqual(
- self.get_column_collation(Author._meta.db_table, "name"),
- collation,
- )
- with connection.schema_editor() as editor:
- editor.alter_field(Author, new_field, old_field, strict=True)
- self.assertIsNone(self.get_column_collation(Author._meta.db_table, "name"))
- @skipUnlessDBFeature("supports_collation_on_charfield")
- def test_alter_primary_key_db_collation(self):
- collation = connection.features.test_collations.get("non_default")
- if not collation:
- self.skipTest("Language collations are not supported.")
- with connection.schema_editor() as editor:
- editor.create_model(Thing)
- old_field = Thing._meta.get_field("when")
- new_field = CharField(max_length=1, db_collation=collation, primary_key=True)
- new_field.set_attributes_from_name("when")
- new_field.model = Thing
- with connection.schema_editor() as editor:
- editor.alter_field(Thing, old_field, new_field, strict=True)
- self.assertEqual(self.get_primary_key(Thing._meta.db_table), "when")
- self.assertEqual(
- self.get_column_collation(Thing._meta.db_table, "when"),
- collation,
- )
- with connection.schema_editor() as editor:
- editor.alter_field(Thing, new_field, old_field, strict=True)
- self.assertEqual(self.get_primary_key(Thing._meta.db_table), "when")
- self.assertIsNone(self.get_column_collation(Thing._meta.db_table, "when"))
- @skipUnlessDBFeature(
- "supports_collation_on_charfield", "supports_collation_on_textfield"
- )
- def test_alter_field_type_and_db_collation(self):
- collation = connection.features.test_collations.get("non_default")
- if not collation:
- self.skipTest("Language collations are not supported.")
- with connection.schema_editor() as editor:
- editor.create_model(Note)
- old_field = Note._meta.get_field("info")
- new_field = CharField(max_length=255, db_collation=collation)
- new_field.set_attributes_from_name("info")
- new_field.model = Note
- with connection.schema_editor() as editor:
- editor.alter_field(Note, old_field, new_field, strict=True)
- columns = self.column_classes(Note)
- self.assertEqual(
- columns["info"][0],
- connection.features.introspected_field_types["CharField"],
- )
- self.assertEqual(columns["info"][1][8], collation)
- with connection.schema_editor() as editor:
- editor.alter_field(Note, new_field, old_field, strict=True)
- columns = self.column_classes(Note)
- self.assertEqual(columns["info"][0], "TextField")
- self.assertIsNone(columns["info"][1][8])
- @skipUnlessDBFeature(
- "supports_collation_on_charfield",
- "supports_non_deterministic_collations",
- )
- def test_ci_cs_db_collation(self):
- cs_collation = connection.features.test_collations.get("cs")
- ci_collation = connection.features.test_collations.get("ci")
- try:
- if connection.vendor == "mysql":
- cs_collation = "latin1_general_cs"
- elif connection.vendor == "postgresql":
- cs_collation = "en-x-icu"
- with connection.cursor() as cursor:
- cursor.execute(
- "CREATE COLLATION IF NOT EXISTS case_insensitive "
- "(provider = icu, locale = 'und-u-ks-level2', "
- "deterministic = false)"
- )
- ci_collation = "case_insensitive"
- # Create the table.
- with connection.schema_editor() as editor:
- editor.create_model(Author)
- # Case-insensitive collation.
- old_field = Author._meta.get_field("name")
- new_field_ci = CharField(max_length=255, db_collation=ci_collation)
- new_field_ci.set_attributes_from_name("name")
- new_field_ci.model = Author
- with connection.schema_editor() as editor:
- editor.alter_field(Author, old_field, new_field_ci, strict=True)
- Author.objects.create(name="ANDREW")
- self.assertIs(Author.objects.filter(name="Andrew").exists(), True)
- # Case-sensitive collation.
- new_field_cs = CharField(max_length=255, db_collation=cs_collation)
- new_field_cs.set_attributes_from_name("name")
- new_field_cs.model = Author
- with connection.schema_editor() as editor:
- editor.alter_field(Author, new_field_ci, new_field_cs, strict=True)
- self.assertIs(Author.objects.filter(name="Andrew").exists(), False)
- finally:
- if connection.vendor == "postgresql":
- with connection.cursor() as cursor:
- cursor.execute("DROP COLLATION IF EXISTS case_insensitive")
|