1. ================
    
  2. ``SchemaEditor``
    
  3. ================
    
  4. 
    
  5. .. module:: django.db.backends.base.schema
    
  6. 
    
  7. .. class:: BaseDatabaseSchemaEditor
    
  8. 
    
  9. Django's migration system is split into two parts; the logic for calculating
    
  10. and storing what operations should be run (``django.db.migrations``), and the
    
  11. database abstraction layer that turns things like "create a model" or
    
  12. "delete a field" into SQL - which is the job of the ``SchemaEditor``.
    
  13. 
    
  14. It's unlikely that you will want to interact directly with ``SchemaEditor`` as
    
  15. a normal developer using Django, but if you want to write your own migration
    
  16. system, or have more advanced needs, it's a lot nicer than writing SQL.
    
  17. 
    
  18. Each database backend in Django supplies its own version of ``SchemaEditor``,
    
  19. and it's always accessible via the ``connection.schema_editor()`` context
    
  20. manager::
    
  21. 
    
  22.     with connection.schema_editor() as schema_editor:
    
  23.         schema_editor.delete_model(MyModel)
    
  24. 
    
  25. It must be used via the context manager as this allows it to manage things
    
  26. like transactions and deferred SQL (like creating ``ForeignKey`` constraints).
    
  27. 
    
  28. It exposes all possible operations as methods, that should be called in
    
  29. the order you wish changes to be applied. Some possible operations or types
    
  30. of change are not possible on all databases - for example, MyISAM does not
    
  31. support foreign key constraints.
    
  32. 
    
  33. If you are writing or maintaining a third-party database backend for Django,
    
  34. you will need to provide a ``SchemaEditor`` implementation in order to work with
    
  35. Django's migration functionality - however, as long as your database is
    
  36. relatively standard in its use of SQL and relational design, you should be able
    
  37. to subclass one of the built-in Django ``SchemaEditor`` classes and tweak the
    
  38. syntax a little.
    
  39. 
    
  40. Methods
    
  41. =======
    
  42. 
    
  43. ``execute()``
    
  44. -------------
    
  45. 
    
  46. .. method:: BaseDatabaseSchemaEditor.execute(sql, params=())
    
  47. 
    
  48. Executes the SQL statement passed in, with parameters if supplied. This
    
  49. is a wrapper around the normal database cursors that allows capture of the SQL
    
  50. to a ``.sql`` file if the user wishes.
    
  51. 
    
  52. ``create_model()``
    
  53. ------------------
    
  54. 
    
  55. .. method:: BaseDatabaseSchemaEditor.create_model(model)
    
  56. 
    
  57. Creates a new table in the database for the provided model, along with any
    
  58. unique constraints or indexes it requires.
    
  59. 
    
  60. ``delete_model()``
    
  61. ------------------
    
  62. 
    
  63. .. method:: BaseDatabaseSchemaEditor.delete_model(model)
    
  64. 
    
  65. Drops the model's table in the database along with any unique constraints
    
  66. or indexes it has.
    
  67. 
    
  68. ``add_index()``
    
  69. ---------------
    
  70. 
    
  71. .. method:: BaseDatabaseSchemaEditor.add_index(model, index)
    
  72. 
    
  73. Adds ``index`` to ``model``’s table.
    
  74. 
    
  75. ``remove_index()``
    
  76. ------------------
    
  77. 
    
  78. .. method:: BaseDatabaseSchemaEditor.remove_index(model, index)
    
  79. 
    
  80. Removes ``index`` from ``model``’s table.
    
  81. 
    
  82. ``rename_index()``
    
  83. ------------------
    
  84. 
    
  85. .. versionadded:: 4.1
    
  86. 
    
  87. .. method:: BaseDatabaseSchemaEditor.rename_index(model, old_index, new_index)
    
  88. 
    
  89. Renames ``old_index`` from ``model``’s table to ``new_index``.
    
  90. 
    
  91. ``add_constraint()``
    
  92. --------------------
    
  93. 
    
  94. .. method:: BaseDatabaseSchemaEditor.add_constraint(model, constraint)
    
  95. 
    
  96. Adds ``constraint`` to ``model``'s table.
    
  97. 
    
  98. ``remove_constraint()``
    
  99. -----------------------
    
  100. 
    
  101. .. method:: BaseDatabaseSchemaEditor.remove_constraint(model, constraint)
    
  102. 
    
  103. Removes ``constraint`` from ``model``'s table.
    
  104. 
    
  105. ``alter_unique_together()``
    
  106. ---------------------------
    
  107. 
    
  108. .. method:: BaseDatabaseSchemaEditor.alter_unique_together(model, old_unique_together, new_unique_together)
    
  109. 
    
  110. Changes a model's :attr:`~django.db.models.Options.unique_together` value; this
    
  111. will add or remove unique constraints from the model's table until they match
    
  112. the new value.
    
  113. 
    
  114. ``alter_index_together()``
    
  115. --------------------------
    
  116. 
    
  117. .. method:: BaseDatabaseSchemaEditor.alter_index_together(model, old_index_together, new_index_together)
    
  118. 
    
  119. Changes a model's :attr:`~django.db.models.Options.index_together` value; this
    
  120. will add or remove indexes from the model's table until they match the new
    
  121. value.
    
  122. 
    
  123. ``alter_db_table()``
    
  124. --------------------
    
  125. 
    
  126. .. method:: BaseDatabaseSchemaEditor.alter_db_table(model, old_db_table, new_db_table)
    
  127. 
    
  128. Renames the model's table from ``old_db_table`` to ``new_db_table``.
    
  129. 
    
  130. ``alter_db_tablespace()``
    
  131. -------------------------
    
  132. 
    
  133. .. method:: BaseDatabaseSchemaEditor.alter_db_tablespace(model, old_db_tablespace, new_db_tablespace)
    
  134. 
    
  135. Moves the model's table from one tablespace to another.
    
  136. 
    
  137. ``add_field()``
    
  138. ---------------
    
  139. 
    
  140. .. method:: BaseDatabaseSchemaEditor.add_field(model, field)
    
  141. 
    
  142. Adds a column (or sometimes multiple) to the model's table to represent the
    
  143. field. This will also add indexes or a unique constraint
    
  144. if the field has ``db_index=True`` or ``unique=True``.
    
  145. 
    
  146. If the field is a ``ManyToManyField`` without a value for ``through``, instead
    
  147. of creating a column, it will make a table to represent the relationship. If
    
  148. ``through`` is provided, it is a no-op.
    
  149. 
    
  150. If the field is a ``ForeignKey``, this will also add the foreign key
    
  151. constraint to the column.
    
  152. 
    
  153. ``remove_field()``
    
  154. ------------------
    
  155. 
    
  156. .. method:: BaseDatabaseSchemaEditor.remove_field(model, field)
    
  157. 
    
  158. Removes the column(s) representing the field from the model's table, along
    
  159. with any unique constraints, foreign key constraints, or indexes caused by
    
  160. that field.
    
  161. 
    
  162. If the field is a ManyToManyField without a value for ``through``, it will
    
  163. remove the table created to track the relationship. If
    
  164. ``through`` is provided, it is a no-op.
    
  165. 
    
  166. ``alter_field()``
    
  167. -----------------
    
  168. 
    
  169. .. method:: BaseDatabaseSchemaEditor.alter_field(model, old_field, new_field, strict=False)
    
  170. 
    
  171. This transforms the field on the model from the old field to the new one. This
    
  172. includes changing the name of the column (the
    
  173. :attr:`~django.db.models.Field.db_column` attribute), changing the type of the
    
  174. field (if the field class changes), changing the ``NULL`` status of the field,
    
  175. adding or removing field-only unique constraints and indexes, changing primary
    
  176. key, and changing the destination of ``ForeignKey`` constraints.
    
  177. 
    
  178. The most common transformation this cannot do is transforming a
    
  179. ``ManyToManyField`` into a normal Field or vice-versa; Django cannot do this
    
  180. without losing data, and so it will refuse to do it. Instead,
    
  181. :meth:`.remove_field` and :meth:`.add_field` should be called separately.
    
  182. 
    
  183. If the database has the ``supports_combined_alters``, Django will try and
    
  184. do as many of these in a single database call as possible; otherwise, it will
    
  185. issue a separate ALTER statement for each change, but will not issue ALTERs
    
  186. where no change is required.
    
  187. 
    
  188. Attributes
    
  189. ==========
    
  190. 
    
  191. All attributes should be considered read-only unless stated otherwise.
    
  192. 
    
  193. ``connection``
    
  194. --------------
    
  195. 
    
  196. .. attribute:: SchemaEditor.connection
    
  197. 
    
  198. A connection object to the database. A useful attribute of the connection is
    
  199. ``alias`` which can be used to determine the name of the database being
    
  200. accessed.
    
  201. 
    
  202. This is useful when doing data migrations for :ref:`migrations with multiple
    
  203. databases <data-migrations-and-multiple-databases>`.