Professional Documents
Culture Documents
``SchemaEditor``
================
.. module:: django.db.backends.base.schema
.. class:: BaseDatabaseSchemaEditor
Django's migration system is split into two parts; the logic for calculating
and storing what operations should be run (``django.db.migrations``), and the
database abstraction layer that turns things like "create a model" or
"delete a field" into SQL - which is the job of the ``SchemaEditor``.
It's unlikely that you will want to interact directly with ``SchemaEditor`` as
a normal developer using Django, but if you want to write your own migration
system, or have more advanced needs, it's a lot nicer than writing SQL.
It must be used via the context manager as this allows it to manage things
like transactions and deferred SQL (like creating ``ForeignKey`` constraints).
Methods
=======
``execute()``
-------------
Executes the SQL statement passed in, with parameters if supplied. This
is a simple wrapper around the normal database cursors that allows
capture of the SQL to a ``.sql`` file if the user wishes.
``create_model()``
------------------
.. method:: BaseDatabaseSchemaEditor.create_model(model)
Creates a new table in the database for the provided model, along with any
unique constraints or indexes it requires.
``delete_model()``
------------------
.. method:: BaseDatabaseSchemaEditor.delete_model(model)
Drops the model's table in the database along with any unique constraints
or indexes it has.
``add_index()``
---------------
.. versionadded:: 1.11
``remove_index()``
------------------
.. versionadded:: 1.11
``alter_unique_together()``
---------------------------
.. method:: BaseDatabaseSchemaEditor.alter_unique_together(model,
old_unique_together, new_unique_together)
``alter_index_together()``
--------------------------
.. method:: BaseDatabaseSchemaEditor.alter_index_together(model,
old_index_together, new_index_together)
``alter_db_table()``
--------------------
``alter_db_tablespace()``
-------------------------
.. method:: BaseDatabaseSchemaEditor.alter_db_tablespace(model, old_db_tablespace,
new_db_tablespace)
``add_field()``
---------------
Adds a column (or sometimes multiple) to the model's table to represent the
field. This will also add indexes or a unique constraint
if the field has ``db_index=True`` or ``unique=True``.
If the field is a ``ForeignKey``, this will also add the foreign key
constraint to the column.
``remove_field()``
------------------
Removes the column(s) representing the field from the model's table, along
with any unique constraints, foreign key constraints, or indexes caused by
that field.
``alter_field()``
-----------------
This transforms the field on the model from the old field to the new one. This
includes changing the name of the column (the
:attr:`~django.db.models.Field.db_column` attribute), changing the type of the
field (if the field class changes), changing the ``NULL`` status of the field,
adding or removing field-only unique constraints and indexes, changing primary
key, and changing the destination of ``ForeignKey`` constraints.
Attributes
==========
All attributes should be considered read-only unless stated otherwise.
``connection``
--------------
.. attribute:: SchemaEditor.connection
This is useful when doing data migrations for :ref:`migrations with multiple
databases <data-migrations-and-multiple-databases>`.
C:\Users\stive>cd proyectosdj
C:\Users\stive\proyectosdj>cd miprimerproyecto
C:\Users\stive>cd proyectosdj
C:\Users\stive\proyectosdj>cd miprimerproyecto