schema-editor.txt 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. Methods
  23. =======
  24. execute
  25. -------
  26. ::
  27. execute(sql, params=[])
  28. Executes the SQL statement passed in, with parameters if supplied. This
  29. is a simple wrapper around the normal database cursors that allows
  30. capture of the SQL to a ``.sql`` file if the user wishes.
  31. create_model
  32. ------------
  33. ::
  34. create_model(model)
  35. delete_model
  36. ------------
  37. ::
  38. delete_model(model)
  39. alter_unique_together
  40. ---------------------
  41. ::
  42. alter_unique_together(model, old_unique_together, new_unique_together)
  43. alter_index_together
  44. --------------------
  45. ::
  46. alter_index_together(model, old_index_together, new_index_together)
  47. alter_db_table
  48. --------------
  49. ::
  50. alter_db_table(model, old_db_table, new_db_table)
  51. alter_db_tablespace
  52. -------------------
  53. ::
  54. alter_db_tablespace(model, old_db_tablespace, new_db_tablespace)
  55. add_field
  56. ---------
  57. ::
  58. add_field(model, field)
  59. remove_field
  60. ------------
  61. ::
  62. remove_field(model, field)
  63. alter_field
  64. ------------
  65. ::
  66. alter_field(model, old_field, new_field, strict=False)