schema-editor.txt 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. ============
  2. SchemaEditor
  3. ============
  4. Django's migration system is split into two parts; the logic for calculating
  5. and storing what operations should be run (``django.db.migrations``), and the
  6. database abstraction layer that turns things like "create a model" or
  7. "delete a field" into SQL - which is the job of the ``SchemaEditor``.
  8. It's unlikely that you will want to interact directly with ``SchemaEditor`` as
  9. a normal developer using Django, but if you want to write your own migration
  10. system, or have more advanced needs, it's a lot nicer than writing SQL.
  11. Each database backend in Django supplies its own version of ``SchemaEditor``,
  12. and it's always accessible via the ``connection.schema_editor()`` context
  13. manager::
  14. with connection.schema_editor() as schema_editor:
  15. schema_editor.delete_model(MyModel)
  16. It must be used via the context manager as this allows it to manage things
  17. like transactions and deferred SQL (like creating ``ForeignKey`` constraints).
  18. It exposes all possible operations as methods, that should be called in
  19. the order you wish changes to be applied. Some possible operations or types
  20. of change are not possible on all databases - for example, MyISAM does not
  21. support foreign key constraints.
  22. If you are writing or maintaining a third-party database backend for Django,
  23. you will need to provide a SchemaEditor implementation in order to work with
  24. 1.7's migration functionality - however, as long as your database is relatively
  25. standard in its use of SQL and relational design, you should be able to
  26. subclass one of the built-in Django SchemaEditor classes and just tweak the
  27. syntax a little. Also note that there are a few new database features that
  28. migrations will look for: ``can_rollback_ddl`` and
  29. ``supports_combined_alters`` are the most important.
  30. Methods
  31. =======
  32. execute
  33. -------
  34. ::
  35. execute(sql, params=[])
  36. Executes the SQL statement passed in, with parameters if supplied. This
  37. is a simple wrapper around the normal database cursors that allows
  38. capture of the SQL to a ``.sql`` file if the user wishes.
  39. create_model
  40. ------------
  41. ::
  42. 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. ::
  48. delete_model(model)
  49. Drops the model's table in the database along with any unique constraints
  50. or indexes it has.
  51. alter_unique_together
  52. ---------------------
  53. ::
  54. alter_unique_together(model, old_unique_together, new_unique_together)
  55. Changes a model's unique_together value; this will add or remove unique
  56. constraints from the model's table until they match the new value.
  57. alter_index_together
  58. --------------------
  59. ::
  60. alter_index_together(model, old_index_together, new_index_together)
  61. Changes a model's index_together value; this will add or remove indexes
  62. from the model's table until they match the new value.
  63. alter_db_table
  64. --------------
  65. ::
  66. alter_db_table(model, old_db_table, new_db_table)
  67. Renames the model's table from ``old_db_table`` to ``new_db_table``.
  68. alter_db_tablespace
  69. -------------------
  70. ::
  71. alter_db_tablespace(model, old_db_tablespace, new_db_tablespace)
  72. Moves the model's table from one tablespace to another.
  73. add_field
  74. ---------
  75. ::
  76. add_field(model, field)
  77. Adds a column (or sometimes multiple) to the model's table to represent the
  78. field. This will also add indexes or a unique constraint
  79. if the field has ``db_index=True`` or ``unique=True``.
  80. If the field is a ManyToManyField without a value for ``through``, instead of
  81. creating a column, it will make a table to represent the relationship. If
  82. ``through`` is provided, it is a no-op.
  83. If the field is a ``ForeignKey``, this will also add the foreign key
  84. constraint to the column.
  85. remove_field
  86. ------------
  87. ::
  88. remove_field(model, field)
  89. Removes the column(s) representing the field from the model's table, along
  90. with any unique constraints, foreign key constraints, or indexes caused by
  91. that field.
  92. If the field is a ManyToManyField without a value for ``through``, it will
  93. remove the table created to track the relationship. If
  94. ``through`` is provided, it is a no-op.
  95. alter_field
  96. ------------
  97. ::
  98. alter_field(model, old_field, new_field, strict=False)
  99. This transforms the field on the model from the old field to the new one. This
  100. includes changing the name of the column (the ``db_column`` attribute),
  101. changing the type of the field (if the field class changes), changing
  102. the ``NULL`` status of the field, adding or removing field-only unique
  103. constraints and indexes, changing primary key, and changing the destination
  104. of ForeignKey constraints.
  105. The most common transformation this cannot do is transforming a
  106. ManyToManyField into a normal Field or vice-versa; Django cannot do this
  107. without losing data, and so it will refuse to do it. Instead, ``remove_field``
  108. and ``add_field`` should be called separately.
  109. If the database has the ``supports_combined_alters``, Django will try and
  110. do as many of these in a single database call as possible; otherwise, it will
  111. issue a separate ALTER statement for each change, but will not issue ALTERs
  112. where no change is required (as South often did).