schema.py 80 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038
  1. import logging
  2. import operator
  3. from datetime import datetime
  4. from django.conf import settings
  5. from django.core.exceptions import FieldError
  6. from django.db.backends.ddl_references import (
  7. Columns,
  8. Expressions,
  9. ForeignKeyName,
  10. IndexName,
  11. Statement,
  12. Table,
  13. )
  14. from django.db.backends.utils import names_digest, split_identifier, truncate_name
  15. from django.db.models import NOT_PROVIDED, Deferrable, Index
  16. from django.db.models.sql import Query
  17. from django.db.transaction import TransactionManagementError, atomic
  18. from django.utils import timezone
  19. logger = logging.getLogger("django.db.backends.schema")
  20. def _is_relevant_relation(relation, altered_field):
  21. """
  22. When altering the given field, must constraints on its model from the given
  23. relation be temporarily dropped?
  24. """
  25. field = relation.field
  26. if field.many_to_many:
  27. # M2M reverse field
  28. return False
  29. if altered_field.primary_key and field.to_fields == [None]:
  30. # Foreign key constraint on the primary key, which is being altered.
  31. return True
  32. # Is the constraint targeting the field being altered?
  33. return altered_field.name in field.to_fields
  34. def _all_related_fields(model):
  35. # Related fields must be returned in a deterministic order.
  36. return sorted(
  37. model._meta._get_fields(
  38. forward=False,
  39. reverse=True,
  40. include_hidden=True,
  41. include_parents=False,
  42. ),
  43. key=operator.attrgetter("name"),
  44. )
  45. def _related_non_m2m_objects(old_field, new_field):
  46. # Filter out m2m objects from reverse relations.
  47. # Return (old_relation, new_relation) tuples.
  48. related_fields = zip(
  49. (
  50. obj
  51. for obj in _all_related_fields(old_field.model)
  52. if _is_relevant_relation(obj, old_field)
  53. ),
  54. (
  55. obj
  56. for obj in _all_related_fields(new_field.model)
  57. if _is_relevant_relation(obj, new_field)
  58. ),
  59. )
  60. for old_rel, new_rel in related_fields:
  61. yield old_rel, new_rel
  62. yield from _related_non_m2m_objects(
  63. old_rel.remote_field,
  64. new_rel.remote_field,
  65. )
  66. class BaseDatabaseSchemaEditor:
  67. """
  68. This class and its subclasses are responsible for emitting schema-changing
  69. statements to the databases - model creation/removal/alteration, field
  70. renaming, index fiddling, and so on.
  71. """
  72. # Overrideable SQL templates
  73. sql_create_table = "CREATE TABLE %(table)s (%(definition)s)"
  74. sql_rename_table = "ALTER TABLE %(old_table)s RENAME TO %(new_table)s"
  75. sql_retablespace_table = "ALTER TABLE %(table)s SET TABLESPACE %(new_tablespace)s"
  76. sql_delete_table = "DROP TABLE %(table)s CASCADE"
  77. sql_create_column = "ALTER TABLE %(table)s ADD COLUMN %(column)s %(definition)s"
  78. sql_alter_column = "ALTER TABLE %(table)s %(changes)s"
  79. sql_alter_column_type = "ALTER COLUMN %(column)s TYPE %(type)s%(collation)s"
  80. sql_alter_column_null = "ALTER COLUMN %(column)s DROP NOT NULL"
  81. sql_alter_column_not_null = "ALTER COLUMN %(column)s SET NOT NULL"
  82. sql_alter_column_default = "ALTER COLUMN %(column)s SET DEFAULT %(default)s"
  83. sql_alter_column_no_default = "ALTER COLUMN %(column)s DROP DEFAULT"
  84. sql_alter_column_no_default_null = sql_alter_column_no_default
  85. sql_delete_column = "ALTER TABLE %(table)s DROP COLUMN %(column)s CASCADE"
  86. sql_rename_column = (
  87. "ALTER TABLE %(table)s RENAME COLUMN %(old_column)s TO %(new_column)s"
  88. )
  89. sql_update_with_default = (
  90. "UPDATE %(table)s SET %(column)s = %(default)s WHERE %(column)s IS NULL"
  91. )
  92. sql_unique_constraint = "UNIQUE (%(columns)s)%(deferrable)s"
  93. sql_check_constraint = "CHECK (%(check)s)"
  94. sql_delete_constraint = "ALTER TABLE %(table)s DROP CONSTRAINT %(name)s"
  95. sql_constraint = "CONSTRAINT %(name)s %(constraint)s"
  96. sql_create_check = "ALTER TABLE %(table)s ADD CONSTRAINT %(name)s CHECK (%(check)s)"
  97. sql_delete_check = sql_delete_constraint
  98. sql_create_unique = (
  99. "ALTER TABLE %(table)s ADD CONSTRAINT %(name)s "
  100. "UNIQUE%(nulls_distinct)s (%(columns)s)%(deferrable)s"
  101. )
  102. sql_delete_unique = sql_delete_constraint
  103. sql_create_fk = (
  104. "ALTER TABLE %(table)s ADD CONSTRAINT %(name)s FOREIGN KEY (%(column)s) "
  105. "REFERENCES %(to_table)s (%(to_column)s)%(deferrable)s"
  106. )
  107. sql_create_inline_fk = None
  108. sql_create_column_inline_fk = None
  109. sql_delete_fk = sql_delete_constraint
  110. sql_create_index = (
  111. "CREATE INDEX %(name)s ON %(table)s "
  112. "(%(columns)s)%(include)s%(extra)s%(condition)s"
  113. )
  114. sql_create_unique_index = (
  115. "CREATE UNIQUE INDEX %(name)s ON %(table)s "
  116. "(%(columns)s)%(include)s%(nulls_distinct)s%(condition)s"
  117. )
  118. sql_rename_index = "ALTER INDEX %(old_name)s RENAME TO %(new_name)s"
  119. sql_delete_index = "DROP INDEX %(name)s"
  120. sql_create_pk = (
  121. "ALTER TABLE %(table)s ADD CONSTRAINT %(name)s PRIMARY KEY (%(columns)s)"
  122. )
  123. sql_delete_pk = sql_delete_constraint
  124. sql_delete_procedure = "DROP PROCEDURE %(procedure)s"
  125. sql_alter_table_comment = "COMMENT ON TABLE %(table)s IS %(comment)s"
  126. sql_alter_column_comment = "COMMENT ON COLUMN %(table)s.%(column)s IS %(comment)s"
  127. def __init__(self, connection, collect_sql=False, atomic=True):
  128. self.connection = connection
  129. self.collect_sql = collect_sql
  130. if self.collect_sql:
  131. self.collected_sql = []
  132. self.atomic_migration = self.connection.features.can_rollback_ddl and atomic
  133. # State-managing methods
  134. def __enter__(self):
  135. self.deferred_sql = []
  136. if self.atomic_migration:
  137. self.atomic = atomic(self.connection.alias)
  138. self.atomic.__enter__()
  139. return self
  140. def __exit__(self, exc_type, exc_value, traceback):
  141. if exc_type is None:
  142. for sql in self.deferred_sql:
  143. self.execute(sql, None)
  144. if self.atomic_migration:
  145. self.atomic.__exit__(exc_type, exc_value, traceback)
  146. # Core utility functions
  147. def execute(self, sql, params=()):
  148. """Execute the given SQL statement, with optional parameters."""
  149. # Don't perform the transactional DDL check if SQL is being collected
  150. # as it's not going to be executed anyway.
  151. if (
  152. not self.collect_sql
  153. and self.connection.in_atomic_block
  154. and not self.connection.features.can_rollback_ddl
  155. ):
  156. raise TransactionManagementError(
  157. "Executing DDL statements while in a transaction on databases "
  158. "that can't perform a rollback is prohibited."
  159. )
  160. # Account for non-string statement objects.
  161. sql = str(sql)
  162. # Log the command we're running, then run it
  163. logger.debug(
  164. "%s; (params %r)", sql, params, extra={"params": params, "sql": sql}
  165. )
  166. if self.collect_sql:
  167. ending = "" if sql.rstrip().endswith(";") else ";"
  168. if params is not None:
  169. self.collected_sql.append(
  170. (sql % tuple(map(self.quote_value, params))) + ending
  171. )
  172. else:
  173. self.collected_sql.append(sql + ending)
  174. else:
  175. with self.connection.cursor() as cursor:
  176. cursor.execute(sql, params)
  177. def quote_name(self, name):
  178. return self.connection.ops.quote_name(name)
  179. def table_sql(self, model):
  180. """Take a model and return its table definition."""
  181. # Add any unique_togethers (always deferred, as some fields might be
  182. # created afterward, like geometry fields with some backends).
  183. for field_names in model._meta.unique_together:
  184. fields = [model._meta.get_field(field) for field in field_names]
  185. self.deferred_sql.append(self._create_unique_sql(model, fields))
  186. # Create column SQL, add FK deferreds if needed.
  187. column_sqls = []
  188. params = []
  189. for field in model._meta.local_fields:
  190. # SQL.
  191. definition, extra_params = self.column_sql(model, field)
  192. if definition is None:
  193. continue
  194. # Check constraints can go on the column SQL here.
  195. db_params = field.db_parameters(connection=self.connection)
  196. if db_params["check"]:
  197. definition += " " + self.sql_check_constraint % db_params
  198. # Autoincrement SQL (for backends with inline variant).
  199. col_type_suffix = field.db_type_suffix(connection=self.connection)
  200. if col_type_suffix:
  201. definition += " %s" % col_type_suffix
  202. params.extend(extra_params)
  203. # FK.
  204. if field.remote_field and field.db_constraint:
  205. to_table = field.remote_field.model._meta.db_table
  206. to_column = field.remote_field.model._meta.get_field(
  207. field.remote_field.field_name
  208. ).column
  209. if self.sql_create_inline_fk:
  210. definition += " " + self.sql_create_inline_fk % {
  211. "to_table": self.quote_name(to_table),
  212. "to_column": self.quote_name(to_column),
  213. }
  214. elif self.connection.features.supports_foreign_keys:
  215. self.deferred_sql.append(
  216. self._create_fk_sql(
  217. model, field, "_fk_%(to_table)s_%(to_column)s"
  218. )
  219. )
  220. # Add the SQL to our big list.
  221. column_sqls.append(
  222. "%s %s"
  223. % (
  224. self.quote_name(field.column),
  225. definition,
  226. )
  227. )
  228. # Autoincrement SQL (for backends with post table definition
  229. # variant).
  230. if field.get_internal_type() in (
  231. "AutoField",
  232. "BigAutoField",
  233. "SmallAutoField",
  234. ):
  235. autoinc_sql = self.connection.ops.autoinc_sql(
  236. model._meta.db_table, field.column
  237. )
  238. if autoinc_sql:
  239. self.deferred_sql.extend(autoinc_sql)
  240. # The BaseConstraint DDL creation methods such as constraint_sql(),
  241. # create_sql(), and delete_sql(), were not designed in a way that
  242. # separate SQL from parameters which make their generated SQL unfit to
  243. # be used in a context where parametrization is delegated to the
  244. # backend.
  245. constraint_sqls = []
  246. if params:
  247. # If parameters are present (e.g. a DEFAULT clause on backends that
  248. # allow parametrization) defer constraint creation so they are not
  249. # mixed with SQL meant to be parametrized.
  250. for constraint in model._meta.constraints:
  251. self.deferred_sql.append(constraint.create_sql(model, self))
  252. else:
  253. constraint_sqls.extend(
  254. constraint.constraint_sql(model, self)
  255. for constraint in model._meta.constraints
  256. )
  257. sql = self.sql_create_table % {
  258. "table": self.quote_name(model._meta.db_table),
  259. "definition": ", ".join(
  260. str(statement)
  261. for statement in (*column_sqls, *constraint_sqls)
  262. if statement
  263. ),
  264. }
  265. if model._meta.db_tablespace:
  266. tablespace_sql = self.connection.ops.tablespace_sql(
  267. model._meta.db_tablespace
  268. )
  269. if tablespace_sql:
  270. sql += " " + tablespace_sql
  271. return sql, params
  272. # Field <-> database mapping functions
  273. def _iter_column_sql(
  274. self, column_db_type, params, model, field, field_db_params, include_default
  275. ):
  276. yield column_db_type
  277. if collation := field_db_params.get("collation"):
  278. yield self._collate_sql(collation)
  279. if self.connection.features.supports_comments_inline and field.db_comment:
  280. yield self._comment_sql(field.db_comment)
  281. # Work out nullability.
  282. null = field.null
  283. # Add database default.
  284. if field.db_default is not NOT_PROVIDED:
  285. default_sql, default_params = self.db_default_sql(field)
  286. yield f"DEFAULT {default_sql}"
  287. params.extend(default_params)
  288. include_default = False
  289. # Include a default value, if requested.
  290. include_default = (
  291. include_default
  292. and not self.skip_default(field)
  293. and
  294. # Don't include a default value if it's a nullable field and the
  295. # default cannot be dropped in the ALTER COLUMN statement (e.g.
  296. # MySQL longtext and longblob).
  297. not (null and self.skip_default_on_alter(field))
  298. )
  299. if include_default:
  300. default_value = self.effective_default(field)
  301. if default_value is not None:
  302. column_default = "DEFAULT " + self._column_default_sql(field)
  303. if self.connection.features.requires_literal_defaults:
  304. # Some databases can't take defaults as a parameter
  305. # (Oracle, SQLite). If this is the case, the individual
  306. # schema backend should implement prepare_default().
  307. yield column_default % self.prepare_default(default_value)
  308. else:
  309. yield column_default
  310. params.append(default_value)
  311. # Oracle treats the empty string ('') as null, so coerce the null
  312. # option whenever '' is a possible value.
  313. if (
  314. field.empty_strings_allowed
  315. and not field.primary_key
  316. and self.connection.features.interprets_empty_strings_as_nulls
  317. ):
  318. null = True
  319. if field.generated:
  320. generated_sql, generated_params = self._column_generated_sql(field)
  321. params.extend(generated_params)
  322. yield generated_sql
  323. elif not null:
  324. yield "NOT NULL"
  325. elif not self.connection.features.implied_column_null:
  326. yield "NULL"
  327. if field.primary_key:
  328. yield "PRIMARY KEY"
  329. elif field.unique:
  330. yield "UNIQUE"
  331. # Optionally add the tablespace if it's an implicitly indexed column.
  332. tablespace = field.db_tablespace or model._meta.db_tablespace
  333. if (
  334. tablespace
  335. and self.connection.features.supports_tablespaces
  336. and field.unique
  337. ):
  338. yield self.connection.ops.tablespace_sql(tablespace, inline=True)
  339. def column_sql(self, model, field, include_default=False):
  340. """
  341. Return the column definition for a field. The field must already have
  342. had set_attributes_from_name() called.
  343. """
  344. # Get the column's type and use that as the basis of the SQL.
  345. field_db_params = field.db_parameters(connection=self.connection)
  346. column_db_type = field_db_params["type"]
  347. # Check for fields that aren't actually columns (e.g. M2M).
  348. if column_db_type is None:
  349. return None, None
  350. params = []
  351. return (
  352. " ".join(
  353. # This appends to the params being returned.
  354. self._iter_column_sql(
  355. column_db_type,
  356. params,
  357. model,
  358. field,
  359. field_db_params,
  360. include_default,
  361. )
  362. ),
  363. params,
  364. )
  365. def skip_default(self, field):
  366. """
  367. Some backends don't accept default values for certain columns types
  368. (i.e. MySQL longtext and longblob).
  369. """
  370. return False
  371. def skip_default_on_alter(self, field):
  372. """
  373. Some backends don't accept default values for certain columns types
  374. (i.e. MySQL longtext and longblob) in the ALTER COLUMN statement.
  375. """
  376. return False
  377. def prepare_default(self, value):
  378. """
  379. Only used for backends which have requires_literal_defaults feature
  380. """
  381. raise NotImplementedError(
  382. "subclasses of BaseDatabaseSchemaEditor for backends which have "
  383. "requires_literal_defaults must provide a prepare_default() method"
  384. )
  385. def _column_default_sql(self, field):
  386. """
  387. Return the SQL to use in a DEFAULT clause. The resulting string should
  388. contain a '%s' placeholder for a default value.
  389. """
  390. return "%s"
  391. def db_default_sql(self, field):
  392. """Return the sql and params for the field's database default."""
  393. from django.db.models.expressions import Value
  394. db_default = field._db_default_expression
  395. sql = (
  396. self._column_default_sql(field) if isinstance(db_default, Value) else "(%s)"
  397. )
  398. query = Query(model=field.model)
  399. compiler = query.get_compiler(connection=self.connection)
  400. default_sql, params = compiler.compile(db_default)
  401. if self.connection.features.requires_literal_defaults:
  402. # Some databases don't support parameterized defaults (Oracle,
  403. # SQLite). If this is the case, the individual schema backend
  404. # should implement prepare_default().
  405. default_sql %= tuple(self.prepare_default(p) for p in params)
  406. params = []
  407. return sql % default_sql, params
  408. def _column_generated_sql(self, field):
  409. """Return the SQL to use in a GENERATED ALWAYS clause."""
  410. expression_sql, params = field.generated_sql(self.connection)
  411. persistency_sql = "STORED" if field.db_persist else "VIRTUAL"
  412. if self.connection.features.requires_literal_defaults:
  413. expression_sql = expression_sql % tuple(self.quote_value(p) for p in params)
  414. params = ()
  415. return f"GENERATED ALWAYS AS ({expression_sql}) {persistency_sql}", params
  416. @staticmethod
  417. def _effective_default(field):
  418. # This method allows testing its logic without a connection.
  419. if field.has_default():
  420. default = field.get_default()
  421. elif field.generated:
  422. default = None
  423. elif not field.null and field.blank and field.empty_strings_allowed:
  424. if field.get_internal_type() == "BinaryField":
  425. default = b""
  426. else:
  427. default = ""
  428. elif getattr(field, "auto_now", False) or getattr(field, "auto_now_add", False):
  429. internal_type = field.get_internal_type()
  430. if internal_type == "DateTimeField":
  431. default = timezone.now()
  432. else:
  433. default = datetime.now()
  434. if internal_type == "DateField":
  435. default = default.date()
  436. elif internal_type == "TimeField":
  437. default = default.time()
  438. else:
  439. default = None
  440. return default
  441. def effective_default(self, field):
  442. """Return a field's effective database default value."""
  443. return field.get_db_prep_save(self._effective_default(field), self.connection)
  444. def quote_value(self, value):
  445. """
  446. Return a quoted version of the value so it's safe to use in an SQL
  447. string. This is not safe against injection from user code; it is
  448. intended only for use in making SQL scripts or preparing default values
  449. for particularly tricky backends (defaults are not user-defined, though,
  450. so this is safe).
  451. """
  452. raise NotImplementedError()
  453. # Actions
  454. def create_model(self, model):
  455. """
  456. Create a table and any accompanying indexes or unique constraints for
  457. the given `model`.
  458. """
  459. sql, params = self.table_sql(model)
  460. # Prevent using [] as params, in the case a literal '%' is used in the
  461. # definition on backends that don't support parametrized DDL.
  462. self.execute(sql, params or None)
  463. if self.connection.features.supports_comments:
  464. # Add table comment.
  465. if model._meta.db_table_comment:
  466. self.alter_db_table_comment(model, None, model._meta.db_table_comment)
  467. # Add column comments.
  468. if not self.connection.features.supports_comments_inline:
  469. for field in model._meta.local_fields:
  470. if field.db_comment:
  471. field_db_params = field.db_parameters(
  472. connection=self.connection
  473. )
  474. field_type = field_db_params["type"]
  475. self.execute(
  476. *self._alter_column_comment_sql(
  477. model, field, field_type, field.db_comment
  478. )
  479. )
  480. # Add any field index (deferred as SQLite _remake_table needs it).
  481. self.deferred_sql.extend(self._model_indexes_sql(model))
  482. # Make M2M tables
  483. for field in model._meta.local_many_to_many:
  484. if field.remote_field.through._meta.auto_created:
  485. self.create_model(field.remote_field.through)
  486. def delete_model(self, model):
  487. """Delete a model from the database."""
  488. # Handle auto-created intermediary models
  489. for field in model._meta.local_many_to_many:
  490. if field.remote_field.through._meta.auto_created:
  491. self.delete_model(field.remote_field.through)
  492. # Delete the table
  493. self.execute(
  494. self.sql_delete_table
  495. % {
  496. "table": self.quote_name(model._meta.db_table),
  497. }
  498. )
  499. # Remove all deferred statements referencing the deleted table.
  500. for sql in list(self.deferred_sql):
  501. if isinstance(sql, Statement) and sql.references_table(
  502. model._meta.db_table
  503. ):
  504. self.deferred_sql.remove(sql)
  505. def add_index(self, model, index):
  506. """Add an index on a model."""
  507. if (
  508. index.contains_expressions
  509. and not self.connection.features.supports_expression_indexes
  510. ):
  511. return None
  512. # Index.create_sql returns interpolated SQL which makes params=None a
  513. # necessity to avoid escaping attempts on execution.
  514. self.execute(index.create_sql(model, self), params=None)
  515. def remove_index(self, model, index):
  516. """Remove an index from a model."""
  517. if (
  518. index.contains_expressions
  519. and not self.connection.features.supports_expression_indexes
  520. ):
  521. return None
  522. self.execute(index.remove_sql(model, self))
  523. def rename_index(self, model, old_index, new_index):
  524. if self.connection.features.can_rename_index:
  525. self.execute(
  526. self._rename_index_sql(model, old_index.name, new_index.name),
  527. params=None,
  528. )
  529. else:
  530. self.remove_index(model, old_index)
  531. self.add_index(model, new_index)
  532. def add_constraint(self, model, constraint):
  533. """Add a constraint to a model."""
  534. sql = constraint.create_sql(model, self)
  535. if sql:
  536. # Constraint.create_sql returns interpolated SQL which makes
  537. # params=None a necessity to avoid escaping attempts on execution.
  538. self.execute(sql, params=None)
  539. def remove_constraint(self, model, constraint):
  540. """Remove a constraint from a model."""
  541. sql = constraint.remove_sql(model, self)
  542. if sql:
  543. self.execute(sql)
  544. def alter_unique_together(self, model, old_unique_together, new_unique_together):
  545. """
  546. Deal with a model changing its unique_together. The input
  547. unique_togethers must be doubly-nested, not the single-nested
  548. ["foo", "bar"] format.
  549. """
  550. olds = {tuple(fields) for fields in old_unique_together}
  551. news = {tuple(fields) for fields in new_unique_together}
  552. # Deleted uniques
  553. for fields in olds.difference(news):
  554. self._delete_composed_index(
  555. model,
  556. fields,
  557. {"unique": True, "primary_key": False},
  558. self.sql_delete_unique,
  559. )
  560. # Created uniques
  561. for field_names in news.difference(olds):
  562. fields = [model._meta.get_field(field) for field in field_names]
  563. self.execute(self._create_unique_sql(model, fields))
  564. def alter_index_together(self, model, old_index_together, new_index_together):
  565. """
  566. Deal with a model changing its index_together. The input
  567. index_togethers must be doubly-nested, not the single-nested
  568. ["foo", "bar"] format.
  569. """
  570. olds = {tuple(fields) for fields in old_index_together}
  571. news = {tuple(fields) for fields in new_index_together}
  572. # Deleted indexes
  573. for fields in olds.difference(news):
  574. self._delete_composed_index(
  575. model,
  576. fields,
  577. {"index": True, "unique": False},
  578. self.sql_delete_index,
  579. )
  580. # Created indexes
  581. for field_names in news.difference(olds):
  582. fields = [model._meta.get_field(field) for field in field_names]
  583. self.execute(self._create_index_sql(model, fields=fields, suffix="_idx"))
  584. def _delete_composed_index(self, model, fields, constraint_kwargs, sql):
  585. meta_constraint_names = {
  586. constraint.name for constraint in model._meta.constraints
  587. }
  588. meta_index_names = {constraint.name for constraint in model._meta.indexes}
  589. columns = [model._meta.get_field(field).column for field in fields]
  590. constraint_names = self._constraint_names(
  591. model,
  592. columns,
  593. exclude=meta_constraint_names | meta_index_names,
  594. **constraint_kwargs,
  595. )
  596. if (
  597. constraint_kwargs.get("unique") is True
  598. and constraint_names
  599. and self.connection.features.allows_multiple_constraints_on_same_fields
  600. ):
  601. # Constraint matching the unique_together name.
  602. default_name = str(
  603. self._unique_constraint_name(model._meta.db_table, columns, quote=False)
  604. )
  605. if default_name in constraint_names:
  606. constraint_names = [default_name]
  607. if len(constraint_names) != 1:
  608. raise ValueError(
  609. "Found wrong number (%s) of constraints for %s(%s)"
  610. % (
  611. len(constraint_names),
  612. model._meta.db_table,
  613. ", ".join(columns),
  614. )
  615. )
  616. self.execute(self._delete_constraint_sql(sql, model, constraint_names[0]))
  617. def alter_db_table(self, model, old_db_table, new_db_table):
  618. """Rename the table a model points to."""
  619. if old_db_table == new_db_table or (
  620. self.connection.features.ignores_table_name_case
  621. and old_db_table.lower() == new_db_table.lower()
  622. ):
  623. return
  624. self.execute(
  625. self.sql_rename_table
  626. % {
  627. "old_table": self.quote_name(old_db_table),
  628. "new_table": self.quote_name(new_db_table),
  629. }
  630. )
  631. # Rename all references to the old table name.
  632. for sql in self.deferred_sql:
  633. if isinstance(sql, Statement):
  634. sql.rename_table_references(old_db_table, new_db_table)
  635. def alter_db_table_comment(self, model, old_db_table_comment, new_db_table_comment):
  636. if self.sql_alter_table_comment and self.connection.features.supports_comments:
  637. self.execute(
  638. self.sql_alter_table_comment
  639. % {
  640. "table": self.quote_name(model._meta.db_table),
  641. "comment": self.quote_value(new_db_table_comment or ""),
  642. }
  643. )
  644. def alter_db_tablespace(self, model, old_db_tablespace, new_db_tablespace):
  645. """Move a model's table between tablespaces."""
  646. self.execute(
  647. self.sql_retablespace_table
  648. % {
  649. "table": self.quote_name(model._meta.db_table),
  650. "old_tablespace": self.quote_name(old_db_tablespace),
  651. "new_tablespace": self.quote_name(new_db_tablespace),
  652. }
  653. )
  654. def add_field(self, model, field):
  655. """
  656. Create a field on a model. Usually involves adding a column, but may
  657. involve adding a table instead (for M2M fields).
  658. """
  659. # Special-case implicit M2M tables
  660. if field.many_to_many and field.remote_field.through._meta.auto_created:
  661. return self.create_model(field.remote_field.through)
  662. # Get the column's definition
  663. definition, params = self.column_sql(model, field, include_default=True)
  664. # It might not actually have a column behind it
  665. if definition is None:
  666. return
  667. if col_type_suffix := field.db_type_suffix(connection=self.connection):
  668. definition += f" {col_type_suffix}"
  669. # Check constraints can go on the column SQL here
  670. db_params = field.db_parameters(connection=self.connection)
  671. if db_params["check"]:
  672. definition += " " + self.sql_check_constraint % db_params
  673. if (
  674. field.remote_field
  675. and self.connection.features.supports_foreign_keys
  676. and field.db_constraint
  677. ):
  678. constraint_suffix = "_fk_%(to_table)s_%(to_column)s"
  679. # Add FK constraint inline, if supported.
  680. if self.sql_create_column_inline_fk:
  681. to_table = field.remote_field.model._meta.db_table
  682. to_column = field.remote_field.model._meta.get_field(
  683. field.remote_field.field_name
  684. ).column
  685. namespace, _ = split_identifier(model._meta.db_table)
  686. definition += " " + self.sql_create_column_inline_fk % {
  687. "name": self._fk_constraint_name(model, field, constraint_suffix),
  688. "namespace": (
  689. "%s." % self.quote_name(namespace) if namespace else ""
  690. ),
  691. "column": self.quote_name(field.column),
  692. "to_table": self.quote_name(to_table),
  693. "to_column": self.quote_name(to_column),
  694. "deferrable": self.connection.ops.deferrable_sql(),
  695. }
  696. # Otherwise, add FK constraints later.
  697. else:
  698. self.deferred_sql.append(
  699. self._create_fk_sql(model, field, constraint_suffix)
  700. )
  701. # Build the SQL and run it
  702. sql = self.sql_create_column % {
  703. "table": self.quote_name(model._meta.db_table),
  704. "column": self.quote_name(field.column),
  705. "definition": definition,
  706. }
  707. # Prevent using [] as params, in the case a literal '%' is used in the
  708. # definition on backends that don't support parametrized DDL.
  709. self.execute(sql, params or None)
  710. # Drop the default if we need to
  711. if (
  712. field.db_default is NOT_PROVIDED
  713. and not self.skip_default_on_alter(field)
  714. and self.effective_default(field) is not None
  715. ):
  716. changes_sql, params = self._alter_column_default_sql(
  717. model, None, field, drop=True
  718. )
  719. sql = self.sql_alter_column % {
  720. "table": self.quote_name(model._meta.db_table),
  721. "changes": changes_sql,
  722. }
  723. self.execute(sql, params)
  724. # Add field comment, if required.
  725. if (
  726. field.db_comment
  727. and self.connection.features.supports_comments
  728. and not self.connection.features.supports_comments_inline
  729. ):
  730. field_type = db_params["type"]
  731. self.execute(
  732. *self._alter_column_comment_sql(
  733. model, field, field_type, field.db_comment
  734. )
  735. )
  736. # Add an index, if required
  737. self.deferred_sql.extend(self._field_indexes_sql(model, field))
  738. # Reset connection if required
  739. if self.connection.features.connection_persists_old_columns:
  740. self.connection.close()
  741. def remove_field(self, model, field):
  742. """
  743. Remove a field from a model. Usually involves deleting a column,
  744. but for M2Ms may involve deleting a table.
  745. """
  746. # Special-case implicit M2M tables
  747. if field.many_to_many and field.remote_field.through._meta.auto_created:
  748. return self.delete_model(field.remote_field.through)
  749. # It might not actually have a column behind it
  750. if field.db_parameters(connection=self.connection)["type"] is None:
  751. return
  752. # Drop any FK constraints, MySQL requires explicit deletion
  753. if field.remote_field:
  754. fk_names = self._constraint_names(model, [field.column], foreign_key=True)
  755. for fk_name in fk_names:
  756. self.execute(self._delete_fk_sql(model, fk_name))
  757. # Delete the column
  758. sql = self.sql_delete_column % {
  759. "table": self.quote_name(model._meta.db_table),
  760. "column": self.quote_name(field.column),
  761. }
  762. self.execute(sql)
  763. # Reset connection if required
  764. if self.connection.features.connection_persists_old_columns:
  765. self.connection.close()
  766. # Remove all deferred statements referencing the deleted column.
  767. for sql in list(self.deferred_sql):
  768. if isinstance(sql, Statement) and sql.references_column(
  769. model._meta.db_table, field.column
  770. ):
  771. self.deferred_sql.remove(sql)
  772. def alter_field(self, model, old_field, new_field, strict=False):
  773. """
  774. Allow a field's type, uniqueness, nullability, default, column,
  775. constraints, etc. to be modified.
  776. `old_field` is required to compute the necessary changes.
  777. If `strict` is True, raise errors if the old column does not match
  778. `old_field` precisely.
  779. """
  780. if not self._field_should_be_altered(old_field, new_field):
  781. return
  782. # Ensure this field is even column-based
  783. old_db_params = old_field.db_parameters(connection=self.connection)
  784. old_type = old_db_params["type"]
  785. new_db_params = new_field.db_parameters(connection=self.connection)
  786. new_type = new_db_params["type"]
  787. modifying_generated_field = False
  788. if (old_type is None and old_field.remote_field is None) or (
  789. new_type is None and new_field.remote_field is None
  790. ):
  791. raise ValueError(
  792. "Cannot alter field %s into %s - they do not properly define "
  793. "db_type (are you using a badly-written custom field?)"
  794. % (old_field, new_field),
  795. )
  796. elif (
  797. old_type is None
  798. and new_type is None
  799. and (
  800. old_field.remote_field.through
  801. and new_field.remote_field.through
  802. and old_field.remote_field.through._meta.auto_created
  803. and new_field.remote_field.through._meta.auto_created
  804. )
  805. ):
  806. return self._alter_many_to_many(model, old_field, new_field, strict)
  807. elif (
  808. old_type is None
  809. and new_type is None
  810. and (
  811. old_field.remote_field.through
  812. and new_field.remote_field.through
  813. and not old_field.remote_field.through._meta.auto_created
  814. and not new_field.remote_field.through._meta.auto_created
  815. )
  816. ):
  817. # Both sides have through models; this is a no-op.
  818. return
  819. elif old_type is None or new_type is None:
  820. raise ValueError(
  821. "Cannot alter field %s into %s - they are not compatible types "
  822. "(you cannot alter to or from M2M fields, or add or remove "
  823. "through= on M2M fields)" % (old_field, new_field)
  824. )
  825. elif old_field.generated != new_field.generated or (
  826. new_field.generated and old_field.db_persist != new_field.db_persist
  827. ):
  828. modifying_generated_field = True
  829. elif new_field.generated:
  830. try:
  831. old_field_sql = old_field.generated_sql(self.connection)
  832. except FieldError:
  833. # Field used in a generated field was renamed.
  834. modifying_generated_field = True
  835. else:
  836. new_field_sql = new_field.generated_sql(self.connection)
  837. modifying_generated_field = old_field_sql != new_field_sql
  838. if modifying_generated_field:
  839. raise ValueError(
  840. f"Modifying GeneratedFields is not supported - the field {new_field} "
  841. "must be removed and re-added with the new definition."
  842. )
  843. self._alter_field(
  844. model,
  845. old_field,
  846. new_field,
  847. old_type,
  848. new_type,
  849. old_db_params,
  850. new_db_params,
  851. strict,
  852. )
  853. def _field_db_check(self, field, field_db_params):
  854. # Always check constraints with the same mocked column name to avoid
  855. # recreating constrains when the column is renamed.
  856. check_constraints = self.connection.data_type_check_constraints
  857. data = field.db_type_parameters(self.connection)
  858. data["column"] = "__column_name__"
  859. try:
  860. return check_constraints[field.get_internal_type()] % data
  861. except KeyError:
  862. return None
  863. def _alter_field(
  864. self,
  865. model,
  866. old_field,
  867. new_field,
  868. old_type,
  869. new_type,
  870. old_db_params,
  871. new_db_params,
  872. strict=False,
  873. ):
  874. """Perform a "physical" (non-ManyToMany) field update."""
  875. # Drop any FK constraints, we'll remake them later
  876. fks_dropped = set()
  877. if (
  878. self.connection.features.supports_foreign_keys
  879. and old_field.remote_field
  880. and old_field.db_constraint
  881. and self._field_should_be_altered(
  882. old_field,
  883. new_field,
  884. ignore={"db_comment"},
  885. )
  886. ):
  887. fk_names = self._constraint_names(
  888. model, [old_field.column], foreign_key=True
  889. )
  890. if strict and len(fk_names) != 1:
  891. raise ValueError(
  892. "Found wrong number (%s) of foreign key constraints for %s.%s"
  893. % (
  894. len(fk_names),
  895. model._meta.db_table,
  896. old_field.column,
  897. )
  898. )
  899. for fk_name in fk_names:
  900. fks_dropped.add((old_field.column,))
  901. self.execute(self._delete_fk_sql(model, fk_name))
  902. # Has unique been removed?
  903. if old_field.unique and (
  904. not new_field.unique or self._field_became_primary_key(old_field, new_field)
  905. ):
  906. # Find the unique constraint for this field
  907. meta_constraint_names = {
  908. constraint.name for constraint in model._meta.constraints
  909. }
  910. constraint_names = self._constraint_names(
  911. model,
  912. [old_field.column],
  913. unique=True,
  914. primary_key=False,
  915. exclude=meta_constraint_names,
  916. )
  917. if strict and len(constraint_names) != 1:
  918. raise ValueError(
  919. "Found wrong number (%s) of unique constraints for %s.%s"
  920. % (
  921. len(constraint_names),
  922. model._meta.db_table,
  923. old_field.column,
  924. )
  925. )
  926. for constraint_name in constraint_names:
  927. self.execute(self._delete_unique_sql(model, constraint_name))
  928. # Drop incoming FK constraints if the field is a primary key or unique,
  929. # which might be a to_field target, and things are going to change.
  930. old_collation = old_db_params.get("collation")
  931. new_collation = new_db_params.get("collation")
  932. drop_foreign_keys = (
  933. self.connection.features.supports_foreign_keys
  934. and (
  935. (old_field.primary_key and new_field.primary_key)
  936. or (old_field.unique and new_field.unique)
  937. )
  938. and ((old_type != new_type) or (old_collation != new_collation))
  939. )
  940. if drop_foreign_keys:
  941. # '_meta.related_field' also contains M2M reverse fields, these
  942. # will be filtered out
  943. for _old_rel, new_rel in _related_non_m2m_objects(old_field, new_field):
  944. rel_fk_names = self._constraint_names(
  945. new_rel.related_model, [new_rel.field.column], foreign_key=True
  946. )
  947. for fk_name in rel_fk_names:
  948. self.execute(self._delete_fk_sql(new_rel.related_model, fk_name))
  949. # Removed an index? (no strict check, as multiple indexes are possible)
  950. # Remove indexes if db_index switched to False or a unique constraint
  951. # will now be used in lieu of an index. The following lines from the
  952. # truth table show all True cases; the rest are False:
  953. #
  954. # old_field.db_index | old_field.unique | new_field.db_index | new_field.unique
  955. # ------------------------------------------------------------------------------
  956. # True | False | False | False
  957. # True | False | False | True
  958. # True | False | True | True
  959. if (
  960. old_field.db_index
  961. and not old_field.unique
  962. and (not new_field.db_index or new_field.unique)
  963. ):
  964. # Find the index for this field
  965. meta_index_names = {index.name for index in model._meta.indexes}
  966. # Retrieve only BTREE indexes since this is what's created with
  967. # db_index=True.
  968. index_names = self._constraint_names(
  969. model,
  970. [old_field.column],
  971. index=True,
  972. type_=Index.suffix,
  973. exclude=meta_index_names,
  974. )
  975. for index_name in index_names:
  976. # The only way to check if an index was created with
  977. # db_index=True or with Index(['field'], name='foo')
  978. # is to look at its name (refs #28053).
  979. self.execute(self._delete_index_sql(model, index_name))
  980. # Change check constraints?
  981. old_db_check = self._field_db_check(old_field, old_db_params)
  982. new_db_check = self._field_db_check(new_field, new_db_params)
  983. if old_db_check != new_db_check and old_db_check:
  984. meta_constraint_names = {
  985. constraint.name for constraint in model._meta.constraints
  986. }
  987. constraint_names = self._constraint_names(
  988. model,
  989. [old_field.column],
  990. check=True,
  991. exclude=meta_constraint_names,
  992. )
  993. if strict and len(constraint_names) != 1:
  994. raise ValueError(
  995. "Found wrong number (%s) of check constraints for %s.%s"
  996. % (
  997. len(constraint_names),
  998. model._meta.db_table,
  999. old_field.column,
  1000. )
  1001. )
  1002. for constraint_name in constraint_names:
  1003. self.execute(self._delete_check_sql(model, constraint_name))
  1004. # Have they renamed the column?
  1005. if old_field.column != new_field.column:
  1006. self.execute(
  1007. self._rename_field_sql(
  1008. model._meta.db_table, old_field, new_field, new_type
  1009. )
  1010. )
  1011. # Rename all references to the renamed column.
  1012. for sql in self.deferred_sql:
  1013. if isinstance(sql, Statement):
  1014. sql.rename_column_references(
  1015. model._meta.db_table, old_field.column, new_field.column
  1016. )
  1017. # Next, start accumulating actions to do
  1018. actions = []
  1019. null_actions = []
  1020. post_actions = []
  1021. # Type suffix change? (e.g. auto increment).
  1022. old_type_suffix = old_field.db_type_suffix(connection=self.connection)
  1023. new_type_suffix = new_field.db_type_suffix(connection=self.connection)
  1024. # Type, collation, or comment change?
  1025. if (
  1026. old_type != new_type
  1027. or old_type_suffix != new_type_suffix
  1028. or old_collation != new_collation
  1029. or (
  1030. self.connection.features.supports_comments
  1031. and old_field.db_comment != new_field.db_comment
  1032. )
  1033. ):
  1034. fragment, other_actions = self._alter_column_type_sql(
  1035. model, old_field, new_field, new_type, old_collation, new_collation
  1036. )
  1037. actions.append(fragment)
  1038. post_actions.extend(other_actions)
  1039. if new_field.db_default is not NOT_PROVIDED:
  1040. if (
  1041. old_field.db_default is NOT_PROVIDED
  1042. or new_field.db_default != old_field.db_default
  1043. ):
  1044. actions.append(
  1045. self._alter_column_database_default_sql(model, old_field, new_field)
  1046. )
  1047. elif old_field.db_default is not NOT_PROVIDED:
  1048. actions.append(
  1049. self._alter_column_database_default_sql(
  1050. model, old_field, new_field, drop=True
  1051. )
  1052. )
  1053. # When changing a column NULL constraint to NOT NULL with a given
  1054. # default value, we need to perform 4 steps:
  1055. # 1. Add a default for new incoming writes
  1056. # 2. Update existing NULL rows with new default
  1057. # 3. Replace NULL constraint with NOT NULL
  1058. # 4. Drop the default again.
  1059. # Default change?
  1060. needs_database_default = False
  1061. if (
  1062. old_field.null
  1063. and not new_field.null
  1064. and new_field.db_default is NOT_PROVIDED
  1065. ):
  1066. old_default = self.effective_default(old_field)
  1067. new_default = self.effective_default(new_field)
  1068. if (
  1069. not self.skip_default_on_alter(new_field)
  1070. and old_default != new_default
  1071. and new_default is not None
  1072. ):
  1073. needs_database_default = True
  1074. actions.append(
  1075. self._alter_column_default_sql(model, old_field, new_field)
  1076. )
  1077. # Nullability change?
  1078. if old_field.null != new_field.null:
  1079. fragment = self._alter_column_null_sql(model, old_field, new_field)
  1080. if fragment:
  1081. null_actions.append(fragment)
  1082. # Only if we have a default and there is a change from NULL to NOT NULL
  1083. four_way_default_alteration = (
  1084. new_field.has_default() or new_field.db_default is not NOT_PROVIDED
  1085. ) and (old_field.null and not new_field.null)
  1086. if actions or null_actions:
  1087. if not four_way_default_alteration:
  1088. # If we don't have to do a 4-way default alteration we can
  1089. # directly run a (NOT) NULL alteration
  1090. actions += null_actions
  1091. # Combine actions together if we can (e.g. postgres)
  1092. if self.connection.features.supports_combined_alters and actions:
  1093. sql, params = tuple(zip(*actions))
  1094. actions = [(", ".join(sql), sum(params, []))]
  1095. # Apply those actions
  1096. for sql, params in actions:
  1097. self.execute(
  1098. self.sql_alter_column
  1099. % {
  1100. "table": self.quote_name(model._meta.db_table),
  1101. "changes": sql,
  1102. },
  1103. params,
  1104. )
  1105. if four_way_default_alteration:
  1106. if new_field.db_default is NOT_PROVIDED:
  1107. default_sql = "%s"
  1108. params = [new_default]
  1109. else:
  1110. default_sql, params = self.db_default_sql(new_field)
  1111. # Update existing rows with default value
  1112. self.execute(
  1113. self.sql_update_with_default
  1114. % {
  1115. "table": self.quote_name(model._meta.db_table),
  1116. "column": self.quote_name(new_field.column),
  1117. "default": default_sql,
  1118. },
  1119. params,
  1120. )
  1121. # Since we didn't run a NOT NULL change before we need to do it
  1122. # now
  1123. for sql, params in null_actions:
  1124. self.execute(
  1125. self.sql_alter_column
  1126. % {
  1127. "table": self.quote_name(model._meta.db_table),
  1128. "changes": sql,
  1129. },
  1130. params,
  1131. )
  1132. if post_actions:
  1133. for sql, params in post_actions:
  1134. self.execute(sql, params)
  1135. # If primary_key changed to False, delete the primary key constraint.
  1136. if old_field.primary_key and not new_field.primary_key:
  1137. self._delete_primary_key(model, strict)
  1138. # Added a unique?
  1139. if self._unique_should_be_added(old_field, new_field):
  1140. self.execute(self._create_unique_sql(model, [new_field]))
  1141. # Added an index? Add an index if db_index switched to True or a unique
  1142. # constraint will no longer be used in lieu of an index. The following
  1143. # lines from the truth table show all True cases; the rest are False:
  1144. #
  1145. # old_field.db_index | old_field.unique | new_field.db_index | new_field.unique
  1146. # ------------------------------------------------------------------------------
  1147. # False | False | True | False
  1148. # False | True | True | False
  1149. # True | True | True | False
  1150. if (
  1151. (not old_field.db_index or old_field.unique)
  1152. and new_field.db_index
  1153. and not new_field.unique
  1154. ):
  1155. self.execute(self._create_index_sql(model, fields=[new_field]))
  1156. # Type alteration on primary key? Then we need to alter the column
  1157. # referring to us.
  1158. rels_to_update = []
  1159. if drop_foreign_keys:
  1160. rels_to_update.extend(_related_non_m2m_objects(old_field, new_field))
  1161. # Changed to become primary key?
  1162. if self._field_became_primary_key(old_field, new_field):
  1163. # Make the new one
  1164. self.execute(self._create_primary_key_sql(model, new_field))
  1165. # Update all referencing columns
  1166. rels_to_update.extend(_related_non_m2m_objects(old_field, new_field))
  1167. # Handle our type alters on the other end of rels from the PK stuff above
  1168. for old_rel, new_rel in rels_to_update:
  1169. rel_db_params = new_rel.field.db_parameters(connection=self.connection)
  1170. rel_type = rel_db_params["type"]
  1171. rel_collation = rel_db_params.get("collation")
  1172. old_rel_db_params = old_rel.field.db_parameters(connection=self.connection)
  1173. old_rel_collation = old_rel_db_params.get("collation")
  1174. fragment, other_actions = self._alter_column_type_sql(
  1175. new_rel.related_model,
  1176. old_rel.field,
  1177. new_rel.field,
  1178. rel_type,
  1179. old_rel_collation,
  1180. rel_collation,
  1181. )
  1182. self.execute(
  1183. self.sql_alter_column
  1184. % {
  1185. "table": self.quote_name(new_rel.related_model._meta.db_table),
  1186. "changes": fragment[0],
  1187. },
  1188. fragment[1],
  1189. )
  1190. for sql, params in other_actions:
  1191. self.execute(sql, params)
  1192. # Does it have a foreign key?
  1193. if (
  1194. self.connection.features.supports_foreign_keys
  1195. and new_field.remote_field
  1196. and (
  1197. fks_dropped or not old_field.remote_field or not old_field.db_constraint
  1198. )
  1199. and new_field.db_constraint
  1200. ):
  1201. self.execute(
  1202. self._create_fk_sql(model, new_field, "_fk_%(to_table)s_%(to_column)s")
  1203. )
  1204. # Rebuild FKs that pointed to us if we previously had to drop them
  1205. if drop_foreign_keys:
  1206. for _, rel in rels_to_update:
  1207. if rel.field.db_constraint:
  1208. self.execute(
  1209. self._create_fk_sql(rel.related_model, rel.field, "_fk")
  1210. )
  1211. # Does it have check constraints we need to add?
  1212. if old_db_check != new_db_check and new_db_check:
  1213. constraint_name = self._create_index_name(
  1214. model._meta.db_table, [new_field.column], suffix="_check"
  1215. )
  1216. self.execute(
  1217. self._create_check_sql(model, constraint_name, new_db_params["check"])
  1218. )
  1219. # Drop the default if we need to
  1220. # (Django usually does not use in-database defaults)
  1221. if needs_database_default:
  1222. changes_sql, params = self._alter_column_default_sql(
  1223. model, old_field, new_field, drop=True
  1224. )
  1225. sql = self.sql_alter_column % {
  1226. "table": self.quote_name(model._meta.db_table),
  1227. "changes": changes_sql,
  1228. }
  1229. self.execute(sql, params)
  1230. # Reset connection if required
  1231. if self.connection.features.connection_persists_old_columns:
  1232. self.connection.close()
  1233. def _alter_column_null_sql(self, model, old_field, new_field):
  1234. """
  1235. Hook to specialize column null alteration.
  1236. Return a (sql, params) fragment to set a column to null or non-null
  1237. as required by new_field, or None if no changes are required.
  1238. """
  1239. if (
  1240. self.connection.features.interprets_empty_strings_as_nulls
  1241. and new_field.empty_strings_allowed
  1242. ):
  1243. # The field is nullable in the database anyway, leave it alone.
  1244. return
  1245. else:
  1246. new_db_params = new_field.db_parameters(connection=self.connection)
  1247. sql = (
  1248. self.sql_alter_column_null
  1249. if new_field.null
  1250. else self.sql_alter_column_not_null
  1251. )
  1252. return (
  1253. sql
  1254. % {
  1255. "column": self.quote_name(new_field.column),
  1256. "type": new_db_params["type"],
  1257. },
  1258. [],
  1259. )
  1260. def _alter_column_default_sql(self, model, old_field, new_field, drop=False):
  1261. """
  1262. Hook to specialize column default alteration.
  1263. Return a (sql, params) fragment to add or drop (depending on the drop
  1264. argument) a default to new_field's column.
  1265. """
  1266. new_default = self.effective_default(new_field)
  1267. default = self._column_default_sql(new_field)
  1268. params = [new_default]
  1269. if drop:
  1270. params = []
  1271. elif self.connection.features.requires_literal_defaults:
  1272. # Some databases (Oracle) can't take defaults as a parameter
  1273. # If this is the case, the SchemaEditor for that database should
  1274. # implement prepare_default().
  1275. default = self.prepare_default(new_default)
  1276. params = []
  1277. new_db_params = new_field.db_parameters(connection=self.connection)
  1278. if drop:
  1279. if new_field.null:
  1280. sql = self.sql_alter_column_no_default_null
  1281. else:
  1282. sql = self.sql_alter_column_no_default
  1283. else:
  1284. sql = self.sql_alter_column_default
  1285. return (
  1286. sql
  1287. % {
  1288. "column": self.quote_name(new_field.column),
  1289. "type": new_db_params["type"],
  1290. "default": default,
  1291. },
  1292. params,
  1293. )
  1294. def _alter_column_database_default_sql(
  1295. self, model, old_field, new_field, drop=False
  1296. ):
  1297. """
  1298. Hook to specialize column database default alteration.
  1299. Return a (sql, params) fragment to add or drop (depending on the drop
  1300. argument) a default to new_field's column.
  1301. """
  1302. if drop:
  1303. sql = self.sql_alter_column_no_default
  1304. default_sql = ""
  1305. params = []
  1306. else:
  1307. sql = self.sql_alter_column_default
  1308. default_sql, params = self.db_default_sql(new_field)
  1309. new_db_params = new_field.db_parameters(connection=self.connection)
  1310. return (
  1311. sql
  1312. % {
  1313. "column": self.quote_name(new_field.column),
  1314. "type": new_db_params["type"],
  1315. "default": default_sql,
  1316. },
  1317. params,
  1318. )
  1319. def _alter_column_type_sql(
  1320. self, model, old_field, new_field, new_type, old_collation, new_collation
  1321. ):
  1322. """
  1323. Hook to specialize column type alteration for different backends,
  1324. for cases when a creation type is different to an alteration type
  1325. (e.g. SERIAL in PostgreSQL, PostGIS fields).
  1326. Return a 2-tuple of: an SQL fragment of (sql, params) to insert into
  1327. an ALTER TABLE statement and a list of extra (sql, params) tuples to
  1328. run once the field is altered.
  1329. """
  1330. other_actions = []
  1331. if collate_sql := self._collate_sql(
  1332. new_collation, old_collation, model._meta.db_table
  1333. ):
  1334. collate_sql = f" {collate_sql}"
  1335. else:
  1336. collate_sql = ""
  1337. # Comment change?
  1338. comment_sql = ""
  1339. if self.connection.features.supports_comments and not new_field.many_to_many:
  1340. if old_field.db_comment != new_field.db_comment:
  1341. # PostgreSQL and Oracle can't execute 'ALTER COLUMN ...' and
  1342. # 'COMMENT ON ...' at the same time.
  1343. sql, params = self._alter_column_comment_sql(
  1344. model, new_field, new_type, new_field.db_comment
  1345. )
  1346. if sql:
  1347. other_actions.append((sql, params))
  1348. if new_field.db_comment:
  1349. comment_sql = self._comment_sql(new_field.db_comment)
  1350. return (
  1351. (
  1352. self.sql_alter_column_type
  1353. % {
  1354. "column": self.quote_name(new_field.column),
  1355. "type": new_type,
  1356. "collation": collate_sql,
  1357. "comment": comment_sql,
  1358. },
  1359. [],
  1360. ),
  1361. other_actions,
  1362. )
  1363. def _alter_column_comment_sql(self, model, new_field, new_type, new_db_comment):
  1364. return (
  1365. self.sql_alter_column_comment
  1366. % {
  1367. "table": self.quote_name(model._meta.db_table),
  1368. "column": self.quote_name(new_field.column),
  1369. "comment": self._comment_sql(new_db_comment),
  1370. },
  1371. [],
  1372. )
  1373. def _comment_sql(self, comment):
  1374. return self.quote_value(comment or "")
  1375. def _alter_many_to_many(self, model, old_field, new_field, strict):
  1376. """Alter M2Ms to repoint their to= endpoints."""
  1377. # Rename the through table
  1378. if (
  1379. old_field.remote_field.through._meta.db_table
  1380. != new_field.remote_field.through._meta.db_table
  1381. ):
  1382. self.alter_db_table(
  1383. old_field.remote_field.through,
  1384. old_field.remote_field.through._meta.db_table,
  1385. new_field.remote_field.through._meta.db_table,
  1386. )
  1387. # Repoint the FK to the other side
  1388. self.alter_field(
  1389. new_field.remote_field.through,
  1390. # The field that points to the target model is needed, so we can
  1391. # tell alter_field to change it - this is m2m_reverse_field_name()
  1392. # (as opposed to m2m_field_name(), which points to our model).
  1393. old_field.remote_field.through._meta.get_field(
  1394. old_field.m2m_reverse_field_name()
  1395. ),
  1396. new_field.remote_field.through._meta.get_field(
  1397. new_field.m2m_reverse_field_name()
  1398. ),
  1399. )
  1400. self.alter_field(
  1401. new_field.remote_field.through,
  1402. # for self-referential models we need to alter field from the other end too
  1403. old_field.remote_field.through._meta.get_field(old_field.m2m_field_name()),
  1404. new_field.remote_field.through._meta.get_field(new_field.m2m_field_name()),
  1405. )
  1406. def _create_index_name(self, table_name, column_names, suffix=""):
  1407. """
  1408. Generate a unique name for an index/unique constraint.
  1409. The name is divided into 3 parts: the table name, the column names,
  1410. and a unique digest and suffix.
  1411. """
  1412. _, table_name = split_identifier(table_name)
  1413. hash_suffix_part = "%s%s" % (
  1414. names_digest(table_name, *column_names, length=8),
  1415. suffix,
  1416. )
  1417. max_length = self.connection.ops.max_name_length() or 200
  1418. # If everything fits into max_length, use that name.
  1419. index_name = "%s_%s_%s" % (table_name, "_".join(column_names), hash_suffix_part)
  1420. if len(index_name) <= max_length:
  1421. return index_name
  1422. # Shorten a long suffix.
  1423. if len(hash_suffix_part) > max_length / 3:
  1424. hash_suffix_part = hash_suffix_part[: max_length // 3]
  1425. other_length = (max_length - len(hash_suffix_part)) // 2 - 1
  1426. index_name = "%s_%s_%s" % (
  1427. table_name[:other_length],
  1428. "_".join(column_names)[:other_length],
  1429. hash_suffix_part,
  1430. )
  1431. # Prepend D if needed to prevent the name from starting with an
  1432. # underscore or a number (not permitted on Oracle).
  1433. if index_name[0] == "_" or index_name[0].isdigit():
  1434. index_name = "D%s" % index_name[:-1]
  1435. return index_name
  1436. def _get_index_tablespace_sql(self, model, fields, db_tablespace=None):
  1437. if db_tablespace is None:
  1438. if len(fields) == 1 and fields[0].db_tablespace:
  1439. db_tablespace = fields[0].db_tablespace
  1440. elif settings.DEFAULT_INDEX_TABLESPACE:
  1441. db_tablespace = settings.DEFAULT_INDEX_TABLESPACE
  1442. elif model._meta.db_tablespace:
  1443. db_tablespace = model._meta.db_tablespace
  1444. if db_tablespace is not None:
  1445. return " " + self.connection.ops.tablespace_sql(db_tablespace)
  1446. return ""
  1447. def _index_condition_sql(self, condition):
  1448. if condition:
  1449. return " WHERE " + condition
  1450. return ""
  1451. def _index_include_sql(self, model, columns):
  1452. if not columns or not self.connection.features.supports_covering_indexes:
  1453. return ""
  1454. return Statement(
  1455. " INCLUDE (%(columns)s)",
  1456. columns=Columns(model._meta.db_table, columns, self.quote_name),
  1457. )
  1458. def _create_index_sql(
  1459. self,
  1460. model,
  1461. *,
  1462. fields=None,
  1463. name=None,
  1464. suffix="",
  1465. using="",
  1466. db_tablespace=None,
  1467. col_suffixes=(),
  1468. sql=None,
  1469. opclasses=(),
  1470. condition=None,
  1471. include=None,
  1472. expressions=None,
  1473. ):
  1474. """
  1475. Return the SQL statement to create the index for one or several fields
  1476. or expressions. `sql` can be specified if the syntax differs from the
  1477. standard (GIS indexes, ...).
  1478. """
  1479. fields = fields or []
  1480. expressions = expressions or []
  1481. compiler = Query(model, alias_cols=False).get_compiler(
  1482. connection=self.connection,
  1483. )
  1484. tablespace_sql = self._get_index_tablespace_sql(
  1485. model, fields, db_tablespace=db_tablespace
  1486. )
  1487. columns = [field.column for field in fields]
  1488. sql_create_index = sql or self.sql_create_index
  1489. table = model._meta.db_table
  1490. def create_index_name(*args, **kwargs):
  1491. nonlocal name
  1492. if name is None:
  1493. name = self._create_index_name(*args, **kwargs)
  1494. return self.quote_name(name)
  1495. return Statement(
  1496. sql_create_index,
  1497. table=Table(table, self.quote_name),
  1498. name=IndexName(table, columns, suffix, create_index_name),
  1499. using=using,
  1500. columns=(
  1501. self._index_columns(table, columns, col_suffixes, opclasses)
  1502. if columns
  1503. else Expressions(table, expressions, compiler, self.quote_value)
  1504. ),
  1505. extra=tablespace_sql,
  1506. condition=self._index_condition_sql(condition),
  1507. include=self._index_include_sql(model, include),
  1508. )
  1509. def _delete_index_sql(self, model, name, sql=None):
  1510. statement = Statement(
  1511. sql or self.sql_delete_index,
  1512. table=Table(model._meta.db_table, self.quote_name),
  1513. name=self.quote_name(name),
  1514. )
  1515. # Remove all deferred statements referencing the deleted index.
  1516. table_name = statement.parts["table"].table
  1517. index_name = statement.parts["name"]
  1518. for sql in list(self.deferred_sql):
  1519. if isinstance(sql, Statement) and sql.references_index(
  1520. table_name, index_name
  1521. ):
  1522. self.deferred_sql.remove(sql)
  1523. return statement
  1524. def _rename_index_sql(self, model, old_name, new_name):
  1525. return Statement(
  1526. self.sql_rename_index,
  1527. table=Table(model._meta.db_table, self.quote_name),
  1528. old_name=self.quote_name(old_name),
  1529. new_name=self.quote_name(new_name),
  1530. )
  1531. def _index_columns(self, table, columns, col_suffixes, opclasses):
  1532. return Columns(table, columns, self.quote_name, col_suffixes=col_suffixes)
  1533. def _model_indexes_sql(self, model):
  1534. """
  1535. Return a list of all index SQL statements (field indexes, Meta.indexes)
  1536. for the specified model.
  1537. """
  1538. if not model._meta.managed or model._meta.proxy or model._meta.swapped:
  1539. return []
  1540. output = []
  1541. for field in model._meta.local_fields:
  1542. output.extend(self._field_indexes_sql(model, field))
  1543. for index in model._meta.indexes:
  1544. if (
  1545. not index.contains_expressions
  1546. or self.connection.features.supports_expression_indexes
  1547. ):
  1548. output.append(index.create_sql(model, self))
  1549. return output
  1550. def _field_indexes_sql(self, model, field):
  1551. """
  1552. Return a list of all index SQL statements for the specified field.
  1553. """
  1554. output = []
  1555. if self._field_should_be_indexed(model, field):
  1556. output.append(self._create_index_sql(model, fields=[field]))
  1557. return output
  1558. def _field_should_be_altered(self, old_field, new_field, ignore=None):
  1559. if not old_field.concrete and not new_field.concrete:
  1560. return False
  1561. ignore = ignore or set()
  1562. _, old_path, old_args, old_kwargs = old_field.deconstruct()
  1563. _, new_path, new_args, new_kwargs = new_field.deconstruct()
  1564. # Don't alter when:
  1565. # - changing only a field name
  1566. # - changing an attribute that doesn't affect the schema
  1567. # - changing an attribute in the provided set of ignored attributes
  1568. # - adding only a db_column and the column name is not changed
  1569. # - db_table does not change for model referenced by foreign keys
  1570. for attr in ignore.union(old_field.non_db_attrs):
  1571. old_kwargs.pop(attr, None)
  1572. for attr in ignore.union(new_field.non_db_attrs):
  1573. new_kwargs.pop(attr, None)
  1574. if (
  1575. not new_field.many_to_many
  1576. and old_field.remote_field
  1577. and new_field.remote_field
  1578. and old_field.remote_field.model._meta.db_table
  1579. == new_field.remote_field.model._meta.db_table
  1580. ):
  1581. old_kwargs.pop("to", None)
  1582. new_kwargs.pop("to", None)
  1583. # db_default can take many form but result in the same SQL.
  1584. if (
  1585. old_kwargs.get("db_default")
  1586. and new_kwargs.get("db_default")
  1587. and self.db_default_sql(old_field) == self.db_default_sql(new_field)
  1588. ):
  1589. old_kwargs.pop("db_default")
  1590. new_kwargs.pop("db_default")
  1591. return self.quote_name(old_field.column) != self.quote_name(
  1592. new_field.column
  1593. ) or (old_path, old_args, old_kwargs) != (new_path, new_args, new_kwargs)
  1594. def _field_should_be_indexed(self, model, field):
  1595. return field.db_index and not field.unique
  1596. def _field_became_primary_key(self, old_field, new_field):
  1597. return not old_field.primary_key and new_field.primary_key
  1598. def _unique_should_be_added(self, old_field, new_field):
  1599. return (
  1600. not new_field.primary_key
  1601. and new_field.unique
  1602. and (not old_field.unique or old_field.primary_key)
  1603. )
  1604. def _rename_field_sql(self, table, old_field, new_field, new_type):
  1605. return self.sql_rename_column % {
  1606. "table": self.quote_name(table),
  1607. "old_column": self.quote_name(old_field.column),
  1608. "new_column": self.quote_name(new_field.column),
  1609. "type": new_type,
  1610. }
  1611. def _create_fk_sql(self, model, field, suffix):
  1612. table = Table(model._meta.db_table, self.quote_name)
  1613. name = self._fk_constraint_name(model, field, suffix)
  1614. column = Columns(model._meta.db_table, [field.column], self.quote_name)
  1615. to_table = Table(field.target_field.model._meta.db_table, self.quote_name)
  1616. to_column = Columns(
  1617. field.target_field.model._meta.db_table,
  1618. [field.target_field.column],
  1619. self.quote_name,
  1620. )
  1621. deferrable = self.connection.ops.deferrable_sql()
  1622. return Statement(
  1623. self.sql_create_fk,
  1624. table=table,
  1625. name=name,
  1626. column=column,
  1627. to_table=to_table,
  1628. to_column=to_column,
  1629. deferrable=deferrable,
  1630. )
  1631. def _fk_constraint_name(self, model, field, suffix):
  1632. def create_fk_name(*args, **kwargs):
  1633. return self.quote_name(self._create_index_name(*args, **kwargs))
  1634. return ForeignKeyName(
  1635. model._meta.db_table,
  1636. [field.column],
  1637. split_identifier(field.target_field.model._meta.db_table)[1],
  1638. [field.target_field.column],
  1639. suffix,
  1640. create_fk_name,
  1641. )
  1642. def _delete_fk_sql(self, model, name):
  1643. return self._delete_constraint_sql(self.sql_delete_fk, model, name)
  1644. def _deferrable_constraint_sql(self, deferrable):
  1645. if deferrable is None:
  1646. return ""
  1647. if deferrable == Deferrable.DEFERRED:
  1648. return " DEFERRABLE INITIALLY DEFERRED"
  1649. if deferrable == Deferrable.IMMEDIATE:
  1650. return " DEFERRABLE INITIALLY IMMEDIATE"
  1651. def _unique_index_nulls_distinct_sql(self, nulls_distinct):
  1652. if nulls_distinct is False:
  1653. return " NULLS NOT DISTINCT"
  1654. elif nulls_distinct is True:
  1655. return " NULLS DISTINCT"
  1656. return ""
  1657. def _unique_supported(
  1658. self,
  1659. condition=None,
  1660. deferrable=None,
  1661. include=None,
  1662. expressions=None,
  1663. nulls_distinct=None,
  1664. ):
  1665. return (
  1666. (not condition or self.connection.features.supports_partial_indexes)
  1667. and (
  1668. not deferrable
  1669. or self.connection.features.supports_deferrable_unique_constraints
  1670. )
  1671. and (not include or self.connection.features.supports_covering_indexes)
  1672. and (
  1673. not expressions or self.connection.features.supports_expression_indexes
  1674. )
  1675. and (
  1676. nulls_distinct is None
  1677. or self.connection.features.supports_nulls_distinct_unique_constraints
  1678. )
  1679. )
  1680. def _unique_sql(
  1681. self,
  1682. model,
  1683. fields,
  1684. name,
  1685. condition=None,
  1686. deferrable=None,
  1687. include=None,
  1688. opclasses=None,
  1689. expressions=None,
  1690. nulls_distinct=None,
  1691. ):
  1692. if not self._unique_supported(
  1693. condition=condition,
  1694. deferrable=deferrable,
  1695. include=include,
  1696. expressions=expressions,
  1697. nulls_distinct=nulls_distinct,
  1698. ):
  1699. return None
  1700. if (
  1701. condition
  1702. or include
  1703. or opclasses
  1704. or expressions
  1705. or nulls_distinct is not None
  1706. ):
  1707. # Databases support conditional, covering, functional unique,
  1708. # and nulls distinct constraints via a unique index.
  1709. sql = self._create_unique_sql(
  1710. model,
  1711. fields,
  1712. name=name,
  1713. condition=condition,
  1714. include=include,
  1715. opclasses=opclasses,
  1716. expressions=expressions,
  1717. nulls_distinct=nulls_distinct,
  1718. )
  1719. if sql:
  1720. self.deferred_sql.append(sql)
  1721. return None
  1722. constraint = self.sql_unique_constraint % {
  1723. "columns": ", ".join([self.quote_name(field.column) for field in fields]),
  1724. "deferrable": self._deferrable_constraint_sql(deferrable),
  1725. }
  1726. return self.sql_constraint % {
  1727. "name": self.quote_name(name),
  1728. "constraint": constraint,
  1729. }
  1730. def _create_unique_sql(
  1731. self,
  1732. model,
  1733. fields,
  1734. name=None,
  1735. condition=None,
  1736. deferrable=None,
  1737. include=None,
  1738. opclasses=None,
  1739. expressions=None,
  1740. nulls_distinct=None,
  1741. ):
  1742. if not self._unique_supported(
  1743. condition=condition,
  1744. deferrable=deferrable,
  1745. include=include,
  1746. expressions=expressions,
  1747. nulls_distinct=nulls_distinct,
  1748. ):
  1749. return None
  1750. compiler = Query(model, alias_cols=False).get_compiler(
  1751. connection=self.connection
  1752. )
  1753. table = model._meta.db_table
  1754. columns = [field.column for field in fields]
  1755. if name is None:
  1756. name = self._unique_constraint_name(table, columns, quote=True)
  1757. else:
  1758. name = self.quote_name(name)
  1759. if condition or include or opclasses or expressions:
  1760. sql = self.sql_create_unique_index
  1761. else:
  1762. sql = self.sql_create_unique
  1763. if columns:
  1764. columns = self._index_columns(
  1765. table, columns, col_suffixes=(), opclasses=opclasses
  1766. )
  1767. else:
  1768. columns = Expressions(table, expressions, compiler, self.quote_value)
  1769. return Statement(
  1770. sql,
  1771. table=Table(table, self.quote_name),
  1772. name=name,
  1773. columns=columns,
  1774. condition=self._index_condition_sql(condition),
  1775. deferrable=self._deferrable_constraint_sql(deferrable),
  1776. include=self._index_include_sql(model, include),
  1777. nulls_distinct=self._unique_index_nulls_distinct_sql(nulls_distinct),
  1778. )
  1779. def _unique_constraint_name(self, table, columns, quote=True):
  1780. if quote:
  1781. def create_unique_name(*args, **kwargs):
  1782. return self.quote_name(self._create_index_name(*args, **kwargs))
  1783. else:
  1784. create_unique_name = self._create_index_name
  1785. return IndexName(table, columns, "_uniq", create_unique_name)
  1786. def _delete_unique_sql(
  1787. self,
  1788. model,
  1789. name,
  1790. condition=None,
  1791. deferrable=None,
  1792. include=None,
  1793. opclasses=None,
  1794. expressions=None,
  1795. nulls_distinct=None,
  1796. ):
  1797. if not self._unique_supported(
  1798. condition=condition,
  1799. deferrable=deferrable,
  1800. include=include,
  1801. expressions=expressions,
  1802. nulls_distinct=nulls_distinct,
  1803. ):
  1804. return None
  1805. if condition or include or opclasses or expressions:
  1806. sql = self.sql_delete_index
  1807. else:
  1808. sql = self.sql_delete_unique
  1809. return self._delete_constraint_sql(sql, model, name)
  1810. def _check_sql(self, name, check):
  1811. return self.sql_constraint % {
  1812. "name": self.quote_name(name),
  1813. "constraint": self.sql_check_constraint % {"check": check},
  1814. }
  1815. def _create_check_sql(self, model, name, check):
  1816. if not self.connection.features.supports_table_check_constraints:
  1817. return None
  1818. return Statement(
  1819. self.sql_create_check,
  1820. table=Table(model._meta.db_table, self.quote_name),
  1821. name=self.quote_name(name),
  1822. check=check,
  1823. )
  1824. def _delete_check_sql(self, model, name):
  1825. if not self.connection.features.supports_table_check_constraints:
  1826. return None
  1827. return self._delete_constraint_sql(self.sql_delete_check, model, name)
  1828. def _delete_constraint_sql(self, template, model, name):
  1829. return Statement(
  1830. template,
  1831. table=Table(model._meta.db_table, self.quote_name),
  1832. name=self.quote_name(name),
  1833. )
  1834. def _constraint_names(
  1835. self,
  1836. model,
  1837. column_names=None,
  1838. unique=None,
  1839. primary_key=None,
  1840. index=None,
  1841. foreign_key=None,
  1842. check=None,
  1843. type_=None,
  1844. exclude=None,
  1845. ):
  1846. """Return all constraint names matching the columns and conditions."""
  1847. if column_names is not None:
  1848. column_names = [
  1849. (
  1850. self.connection.introspection.identifier_converter(
  1851. truncate_name(name, self.connection.ops.max_name_length())
  1852. )
  1853. if self.connection.features.truncates_names
  1854. else self.connection.introspection.identifier_converter(name)
  1855. )
  1856. for name in column_names
  1857. ]
  1858. with self.connection.cursor() as cursor:
  1859. constraints = self.connection.introspection.get_constraints(
  1860. cursor, model._meta.db_table
  1861. )
  1862. result = []
  1863. for name, infodict in constraints.items():
  1864. if column_names is None or column_names == infodict["columns"]:
  1865. if unique is not None and infodict["unique"] != unique:
  1866. continue
  1867. if primary_key is not None and infodict["primary_key"] != primary_key:
  1868. continue
  1869. if index is not None and infodict["index"] != index:
  1870. continue
  1871. if check is not None and infodict["check"] != check:
  1872. continue
  1873. if foreign_key is not None and not infodict["foreign_key"]:
  1874. continue
  1875. if type_ is not None and infodict["type"] != type_:
  1876. continue
  1877. if not exclude or name not in exclude:
  1878. result.append(name)
  1879. return result
  1880. def _delete_primary_key(self, model, strict=False):
  1881. constraint_names = self._constraint_names(model, primary_key=True)
  1882. if strict and len(constraint_names) != 1:
  1883. raise ValueError(
  1884. "Found wrong number (%s) of PK constraints for %s"
  1885. % (
  1886. len(constraint_names),
  1887. model._meta.db_table,
  1888. )
  1889. )
  1890. for constraint_name in constraint_names:
  1891. self.execute(self._delete_primary_key_sql(model, constraint_name))
  1892. def _create_primary_key_sql(self, model, field):
  1893. return Statement(
  1894. self.sql_create_pk,
  1895. table=Table(model._meta.db_table, self.quote_name),
  1896. name=self.quote_name(
  1897. self._create_index_name(
  1898. model._meta.db_table, [field.column], suffix="_pk"
  1899. )
  1900. ),
  1901. columns=Columns(model._meta.db_table, [field.column], self.quote_name),
  1902. )
  1903. def _delete_primary_key_sql(self, model, name):
  1904. return self._delete_constraint_sql(self.sql_delete_pk, model, name)
  1905. def _collate_sql(self, collation, old_collation=None, table_name=None):
  1906. return "COLLATE " + self.quote_name(collation) if collation else ""
  1907. def remove_procedure(self, procedure_name, param_types=()):
  1908. sql = self.sql_delete_procedure % {
  1909. "procedure": self.quote_name(procedure_name),
  1910. "param_types": ",".join(param_types),
  1911. }
  1912. self.execute(sql)