schema-editor.txt 6.9 KB

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