schema-editor.txt 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. ================
  2. ``SchemaEditor``
  3. ================
  4. .. module:: django.db.backends.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 just 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`` and
  31. ``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 simple wrapper around the normal database cursors that allows
  39. capture of the SQL 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. alter_unique_together
  51. ---------------------
  52. .. method:: BaseDatabaseSchemaEditor.alter_unique_together(model, old_unique_together, new_unique_together)
  53. Changes a model's :attr:`~django.db.models.Options.unique_together` value; this
  54. will add or remove unique constraints from the model's table until they match
  55. the new value.
  56. alter_index_together
  57. --------------------
  58. .. method:: BaseDatabaseSchemaEditor.alter_index_together(model, old_index_together, new_index_together)
  59. Changes a model's :attr:`~django.db.models.Options.index_together` value; this
  60. will add or remove indexes from the model's table until they match the new
  61. value.
  62. alter_db_table
  63. --------------
  64. .. method:: BaseDatabaseSchemaEditor.alter_db_table(model, old_db_table, new_db_table)
  65. Renames the model's table from ``old_db_table`` to ``new_db_table``.
  66. alter_db_tablespace
  67. -------------------
  68. .. method:: BaseDatabaseSchemaEditor.alter_db_tablespace(model, old_db_tablespace, new_db_tablespace)
  69. Moves the model's table from one tablespace to another.
  70. add_field
  71. ---------
  72. .. method:: BaseDatabaseSchemaEditor.add_field(model, field)
  73. Adds a column (or sometimes multiple) to the model's table to represent the
  74. field. This will also add indexes or a unique constraint
  75. if the field has ``db_index=True`` or ``unique=True``.
  76. If the field is a ``ManyToManyField`` without a value for ``through``, instead
  77. of creating a column, it will make a table to represent the relationship. If
  78. ``through`` is provided, it is a no-op.
  79. If the field is a ``ForeignKey``, this will also add the foreign key
  80. constraint to the column.
  81. remove_field
  82. ------------
  83. .. method:: BaseDatabaseSchemaEditor.remove_field(model, field)
  84. Removes the column(s) representing the field from the model's table, along
  85. with any unique constraints, foreign key constraints, or indexes caused by
  86. that field.
  87. If the field is a ManyToManyField without a value for ``through``, it will
  88. remove the table created to track the relationship. If
  89. ``through`` is provided, it is a no-op.
  90. alter_field
  91. ------------
  92. .. method:: BaseDatabaseSchemaEditor.alter_field(model, old_field, new_field, strict=False)
  93. This transforms the field on the model from the old field to the new one. This
  94. includes changing the name of the column (the
  95. :attr:`~django.db.models.Field.db_column` attribute), changing the type of the
  96. field (if the field class changes), changing the ``NULL`` status of the field,
  97. adding or removing field-only unique constraints and indexes, changing primary
  98. key, and changing the destination of ``ForeignKey`` constraints.
  99. The most common transformation this cannot do is transforming a
  100. ``ManyToManyField`` into a normal Field or vice-versa; Django cannot do this
  101. without losing data, and so it will refuse to do it. Instead,
  102. :meth:`.remove_field` and :meth:`.add_field` should be called separately.
  103. If the database has the ``supports_combined_alters``, Django will try and
  104. do as many of these in a single database call as possible; otherwise, it will
  105. issue a separate ALTER statement for each change, but will not issue ALTERs
  106. where no change is required (as South often did).
  107. Attributes
  108. ==========
  109. All attributes should be considered read-only unless stated otherwise.
  110. connection
  111. ----------
  112. .. attribute:: SchemaEditor.connection
  113. A connection object to the database. A useful attribute of the connection is
  114. ``alias`` which can be used to determine the name of the database being
  115. accessed.
  116. This is useful when doing data migrations for :ref:`migrations with multiple
  117. databases <data-migrations-and-multiple-databases>`.