schema-editor.txt 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. 1.7's migration functionality - however, as long as your database is relatively
  27. standard in its use of SQL and relational design, you should be able to
  28. subclass one of the built-in Django ``SchemaEditor`` classes and tweak the
  29. syntax a little. Also note that there are a few new database features that
  30. migrations will look for: ``can_rollback_ddl``
  31. and ``supports_combined_alters`` are the most important.
  32. Methods
  33. =======
  34. ``execute()``
  35. -------------
  36. .. method:: BaseDatabaseSchemaEditor.execute(sql, params=[])
  37. Executes the SQL statement passed in, with parameters if supplied. This
  38. is a wrapper around the normal database cursors that allows capture of the SQL
  39. to a ``.sql`` file if the user wishes.
  40. ``create_model()``
  41. ------------------
  42. .. method:: BaseDatabaseSchemaEditor.create_model(model)
  43. Creates a new table in the database for the provided model, along with any
  44. unique constraints or indexes it requires.
  45. ``delete_model()``
  46. ------------------
  47. .. method:: BaseDatabaseSchemaEditor.delete_model(model)
  48. Drops the model's table in the database along with any unique constraints
  49. or indexes it has.
  50. ``add_index()``
  51. ---------------
  52. .. method:: BaseDatabaseSchemaEditor.add_index(model, index)
  53. Adds ``index`` to ``model``’s table.
  54. ``remove_index()``
  55. ------------------
  56. .. method:: BaseDatabaseSchemaEditor.remove_index(model, index)
  57. Removes ``index`` from ``model``’s table.
  58. ``add_constraint()``
  59. --------------------
  60. .. method:: BaseDatabaseSchemaEditor.add_constraint(model, constraint)
  61. Adds ``constraint`` to ``model``'s table.
  62. ``remove_constraint()``
  63. -----------------------
  64. .. method:: BaseDatabaseSchemaEditor.remove_constraint(model, constraint)
  65. Removes ``constraint`` from ``model``'s table.
  66. ``alter_unique_together()``
  67. ---------------------------
  68. .. method:: BaseDatabaseSchemaEditor.alter_unique_together(model, old_unique_together, new_unique_together)
  69. Changes a model's :attr:`~django.db.models.Options.unique_together` value; this
  70. will add or remove unique constraints from the model's table until they match
  71. the new value.
  72. ``alter_index_together()``
  73. --------------------------
  74. .. method:: BaseDatabaseSchemaEditor.alter_index_together(model, old_index_together, new_index_together)
  75. Changes a model's :attr:`~django.db.models.Options.index_together` value; this
  76. will add or remove indexes from the model's table until they match the new
  77. value.
  78. ``alter_db_table()``
  79. --------------------
  80. .. method:: BaseDatabaseSchemaEditor.alter_db_table(model, old_db_table, new_db_table)
  81. Renames the model's table from ``old_db_table`` to ``new_db_table``.
  82. ``alter_db_tablespace()``
  83. -------------------------
  84. .. method:: BaseDatabaseSchemaEditor.alter_db_tablespace(model, old_db_tablespace, new_db_tablespace)
  85. Moves the model's table from one tablespace to another.
  86. ``add_field()``
  87. ---------------
  88. .. method:: BaseDatabaseSchemaEditor.add_field(model, field)
  89. Adds a column (or sometimes multiple) to the model's table to represent the
  90. field. This will also add indexes or a unique constraint
  91. if the field has ``db_index=True`` or ``unique=True``.
  92. If the field is a ``ManyToManyField`` without a value for ``through``, instead
  93. of creating a column, it will make a table to represent the relationship. If
  94. ``through`` is provided, it is a no-op.
  95. If the field is a ``ForeignKey``, this will also add the foreign key
  96. constraint to the column.
  97. ``remove_field()``
  98. ------------------
  99. .. method:: BaseDatabaseSchemaEditor.remove_field(model, field)
  100. Removes the column(s) representing the field from the model's table, along
  101. with any unique constraints, foreign key constraints, or indexes caused by
  102. that field.
  103. If the field is a ManyToManyField without a value for ``through``, it will
  104. remove the table created to track the relationship. If
  105. ``through`` is provided, it is a no-op.
  106. ``alter_field()``
  107. -----------------
  108. .. method:: BaseDatabaseSchemaEditor.alter_field(model, old_field, new_field, strict=False)
  109. This transforms the field on the model from the old field to the new one. This
  110. includes changing the name of the column (the
  111. :attr:`~django.db.models.Field.db_column` attribute), changing the type of the
  112. field (if the field class changes), changing the ``NULL`` status of the field,
  113. adding or removing field-only unique constraints and indexes, changing primary
  114. key, and changing the destination of ``ForeignKey`` constraints.
  115. The most common transformation this cannot do is transforming a
  116. ``ManyToManyField`` into a normal Field or vice-versa; Django cannot do this
  117. without losing data, and so it will refuse to do it. Instead,
  118. :meth:`.remove_field` and :meth:`.add_field` should be called separately.
  119. If the database has the ``supports_combined_alters``, Django will try and
  120. do as many of these in a single database call as possible; otherwise, it will
  121. issue a separate ALTER statement for each change, but will not issue ALTERs
  122. where no change is required.
  123. Attributes
  124. ==========
  125. All attributes should be considered read-only unless stated otherwise.
  126. ``connection``
  127. --------------
  128. .. attribute:: SchemaEditor.connection
  129. A connection object to the database. A useful attribute of the connection is
  130. ``alias`` which can be used to determine the name of the database being
  131. accessed.
  132. This is useful when doing data migrations for :ref:`migrations with multiple
  133. databases <data-migrations-and-multiple-databases>`.