{"title":"SchemaEditor","version":"2.0","locale":"zh-hans","docname":"ref/schema-editor","url":"/zh-hans/2.0/ref/schema-editor/","canonical":"https://djangodocs.dev/zh-hans/2.0/ref/schema-editor/","summary":"class BaseDatabaseSchemaEditor Link to this definition # Django's migration system is split into two parts; the logic for calculating and storing what operations…","html":"<span id=\"schemaeditor\"></span><h1><code class=\"docutils literal notranslate\"><span class=\"pre\">SchemaEditor</span></code><a class=\"heading-anchor\" href=\"#module-django.db.backends.base.schema\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.db.backends.base.schema.BaseDatabaseSchemaEditor\">\n<em class=\"property\"><span class=\"k\"><span class=\"pre\">class</span></span><span class=\"w\"> </span></em><span class=\"sig-name descname\"><span class=\"pre\">BaseDatabaseSchemaEditor</span></span><a class=\"heading-anchor\" href=\"#django.db.backends.base.schema.BaseDatabaseSchemaEditor\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>Django's migration system is split into two parts; the logic for calculating\nand storing what operations should be run (<code class=\"docutils literal notranslate\"><span class=\"pre\">django.db.migrations</span></code>), and the\ndatabase abstraction layer that turns things like &quot;create a model&quot; or\n&quot;delete a field&quot; into SQL - which is the job of the <code class=\"docutils literal notranslate\"><span class=\"pre\">SchemaEditor</span></code>.</p>\n<p>It's unlikely that you will want to interact directly with <code class=\"docutils literal notranslate\"><span class=\"pre\">SchemaEditor</span></code> as\na normal developer using Django, but if you want to write your own migration\nsystem, or have more advanced needs, it's a lot nicer than writing SQL.</p>\n<p>Each database backend in Django supplies its own version of <code class=\"docutils literal notranslate\"><span class=\"pre\">SchemaEditor</span></code>,\nand it's always accessible via the <code class=\"docutils literal notranslate\"><span class=\"pre\">connection.schema_editor()</span></code> context\nmanager:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"k\">with</span> <span class=\"n\">connection</span><span class=\"o\">.</span><span class=\"n\">schema_editor</span><span class=\"p\">()</span> <span class=\"k\">as</span> <span class=\"n\">schema_editor</span><span class=\"p\">:</span>\n    <span class=\"n\">schema_editor</span><span class=\"o\">.</span><span class=\"n\">delete_model</span><span class=\"p\">(</span><span class=\"n\">MyModel</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>It must be used via the context manager as this allows it to manage things\nlike transactions and deferred SQL (like creating <code class=\"docutils literal notranslate\"><span class=\"pre\">ForeignKey</span></code> constraints).</p>\n<p>It exposes all possible operations as methods, that should be called in\nthe order you wish changes to be applied. Some possible operations or types\nof change are not possible on all databases - for example, MyISAM does not\nsupport foreign key constraints.</p>\n<p>If you are writing or maintaining a third-party database backend for Django,\nyou will need to provide a <code class=\"docutils literal notranslate\"><span class=\"pre\">SchemaEditor</span></code> implementation in order to work with\n1.7's migration functionality - however, as long as your database is relatively\nstandard in its use of SQL and relational design, you should be able to\nsubclass one of the built-in Django <code class=\"docutils literal notranslate\"><span class=\"pre\">SchemaEditor</span></code> classes and just tweak the\nsyntax a little. Also note that there are a few new database features that\nmigrations will look for: <code class=\"docutils literal notranslate\"><span class=\"pre\">can_rollback_ddl</span></code> and\n<code class=\"docutils literal notranslate\"><span class=\"pre\">supports_combined_alters</span></code> are the most important.</p>\n<section id=\"methods\">\n<h2>Methods<a class=\"heading-anchor\" href=\"#methods\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<section id=\"execute\">\n<h3><code class=\"docutils literal notranslate\"><span class=\"pre\">execute()</span></code><a class=\"heading-anchor\" href=\"#execute\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.db.backends.base.schema.BaseDatabaseSchemaEditor.execute\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">BaseDatabaseSchemaEditor.</span></span><span class=\"sig-name descname\"><span class=\"pre\">execute</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">sql</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">params</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">[]</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.db.backends.base.schema.BaseDatabaseSchemaEditor.execute\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>Executes the SQL statement passed in, with parameters if supplied. This\nis a simple wrapper around the normal database cursors that allows\ncapture of the SQL to a <code class=\"docutils literal notranslate\"><span class=\"pre\">.sql</span></code> file if the user wishes.</p>\n</section>\n<section id=\"create-model\">\n<h3><code class=\"docutils literal notranslate\"><span class=\"pre\">create_model()</span></code><a class=\"heading-anchor\" href=\"#create-model\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.db.backends.base.schema.BaseDatabaseSchemaEditor.create_model\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">BaseDatabaseSchemaEditor.</span></span><span class=\"sig-name descname\"><span class=\"pre\">create_model</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">model</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.db.backends.base.schema.BaseDatabaseSchemaEditor.create_model\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>Creates a new table in the database for the provided model, along with any\nunique constraints or indexes it requires.</p>\n</section>\n<section id=\"delete-model\">\n<h3><code class=\"docutils literal notranslate\"><span class=\"pre\">delete_model()</span></code><a class=\"heading-anchor\" href=\"#delete-model\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.db.backends.base.schema.BaseDatabaseSchemaEditor.delete_model\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">BaseDatabaseSchemaEditor.</span></span><span class=\"sig-name descname\"><span class=\"pre\">delete_model</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">model</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.db.backends.base.schema.BaseDatabaseSchemaEditor.delete_model\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>Drops the model's table in the database along with any unique constraints\nor indexes it has.</p>\n</section>\n<section id=\"add-index\">\n<h3><code class=\"docutils literal notranslate\"><span class=\"pre\">add_index()</span></code><a class=\"heading-anchor\" href=\"#add-index\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.db.backends.base.schema.BaseDatabaseSchemaEditor.add_index\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">BaseDatabaseSchemaEditor.</span></span><span class=\"sig-name descname\"><span class=\"pre\">add_index</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">model</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">index</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.db.backends.base.schema.BaseDatabaseSchemaEditor.add_index\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<aside class=\"version-note version-added\" data-version=\"1.11\">\n<p class=\"version-note-title\">New in Django 1.11</p></aside>\n<p>Adds <code class=\"docutils literal notranslate\"><span class=\"pre\">index</span></code> to <code class=\"docutils literal notranslate\"><span class=\"pre\">model</span></code>’s table.</p>\n</section>\n<section id=\"remove-index\">\n<h3><code class=\"docutils literal notranslate\"><span class=\"pre\">remove_index()</span></code><a class=\"heading-anchor\" href=\"#remove-index\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.db.backends.base.schema.BaseDatabaseSchemaEditor.remove_index\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">BaseDatabaseSchemaEditor.</span></span><span class=\"sig-name descname\"><span class=\"pre\">remove_index</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">model</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">index</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.db.backends.base.schema.BaseDatabaseSchemaEditor.remove_index\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<aside class=\"version-note version-added\" data-version=\"1.11\">\n<p class=\"version-note-title\">New in Django 1.11</p></aside>\n<p>Removes <code class=\"docutils literal notranslate\"><span class=\"pre\">index</span></code> from <code class=\"docutils literal notranslate\"><span class=\"pre\">model</span></code>’s table.</p>\n</section>\n<section id=\"alter-unique-together\">\n<h3><code class=\"docutils literal notranslate\"><span class=\"pre\">alter_unique_together()</span></code><a class=\"heading-anchor\" href=\"#alter-unique-together\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.db.backends.base.schema.BaseDatabaseSchemaEditor.alter_unique_together\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">BaseDatabaseSchemaEditor.</span></span><span class=\"sig-name descname\"><span class=\"pre\">alter_unique_together</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">model</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">old_unique_together</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">new_unique_together</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.db.backends.base.schema.BaseDatabaseSchemaEditor.alter_unique_together\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>Changes a model's <a class=\"reference internal\" href=\"/zh-hans/2.0/ref/models/options/#django.db.models.Options.unique_together\" title=\"django.db.models.Options.unique_together\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">unique_together</span></code></a> value; this\nwill add or remove unique constraints from the model's table until they match\nthe new value.</p>\n</section>\n<section id=\"alter-index-together\">\n<h3><code class=\"docutils literal notranslate\"><span class=\"pre\">alter_index_together()</span></code><a class=\"heading-anchor\" href=\"#alter-index-together\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.db.backends.base.schema.BaseDatabaseSchemaEditor.alter_index_together\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">BaseDatabaseSchemaEditor.</span></span><span class=\"sig-name descname\"><span class=\"pre\">alter_index_together</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">model</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">old_index_together</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">new_index_together</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.db.backends.base.schema.BaseDatabaseSchemaEditor.alter_index_together\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>Changes a model's <a class=\"reference internal\" href=\"/zh-hans/2.0/ref/models/options/#django.db.models.Options.index_together\" title=\"django.db.models.Options.index_together\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">index_together</span></code></a> value; this\nwill add or remove indexes from the model's table until they match the new\nvalue.</p>\n</section>\n<section id=\"alter-db-table\">\n<h3><code class=\"docutils literal notranslate\"><span class=\"pre\">alter_db_table()</span></code><a class=\"heading-anchor\" href=\"#alter-db-table\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.db.backends.base.schema.BaseDatabaseSchemaEditor.alter_db_table\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">BaseDatabaseSchemaEditor.</span></span><span class=\"sig-name descname\"><span class=\"pre\">alter_db_table</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">model</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">old_db_table</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">new_db_table</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.db.backends.base.schema.BaseDatabaseSchemaEditor.alter_db_table\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>Renames the model's table from <code class=\"docutils literal notranslate\"><span class=\"pre\">old_db_table</span></code> to <code class=\"docutils literal notranslate\"><span class=\"pre\">new_db_table</span></code>.</p>\n</section>\n<section id=\"alter-db-tablespace\">\n<h3><code class=\"docutils literal notranslate\"><span class=\"pre\">alter_db_tablespace()</span></code><a class=\"heading-anchor\" href=\"#alter-db-tablespace\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.db.backends.base.schema.BaseDatabaseSchemaEditor.alter_db_tablespace\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">BaseDatabaseSchemaEditor.</span></span><span class=\"sig-name descname\"><span class=\"pre\">alter_db_tablespace</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">model</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">old_db_tablespace</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">new_db_tablespace</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.db.backends.base.schema.BaseDatabaseSchemaEditor.alter_db_tablespace\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>Moves the model's table from one tablespace to another.</p>\n</section>\n<section id=\"add-field\">\n<h3><code class=\"docutils literal notranslate\"><span class=\"pre\">add_field()</span></code><a class=\"heading-anchor\" href=\"#add-field\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.db.backends.base.schema.BaseDatabaseSchemaEditor.add_field\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">BaseDatabaseSchemaEditor.</span></span><span class=\"sig-name descname\"><span class=\"pre\">add_field</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">model</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">field</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.db.backends.base.schema.BaseDatabaseSchemaEditor.add_field\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>Adds a column (or sometimes multiple) to the model's table to represent the\nfield. This will also add indexes or a unique constraint\nif the field has <code class=\"docutils literal notranslate\"><span class=\"pre\">db_index=True</span></code> or <code class=\"docutils literal notranslate\"><span class=\"pre\">unique=True</span></code>.</p>\n<p>If the field is a <code class=\"docutils literal notranslate\"><span class=\"pre\">ManyToManyField</span></code> without a value for <code class=\"docutils literal notranslate\"><span class=\"pre\">through</span></code>, instead\nof creating a column, it will make a table to represent the relationship. If\n<code class=\"docutils literal notranslate\"><span class=\"pre\">through</span></code> is provided, it is a no-op.</p>\n<p>If the field is a <code class=\"docutils literal notranslate\"><span class=\"pre\">ForeignKey</span></code>, this will also add the foreign key\nconstraint to the column.</p>\n</section>\n<section id=\"remove-field\">\n<h3><code class=\"docutils literal notranslate\"><span class=\"pre\">remove_field()</span></code><a class=\"heading-anchor\" href=\"#remove-field\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.db.backends.base.schema.BaseDatabaseSchemaEditor.remove_field\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">BaseDatabaseSchemaEditor.</span></span><span class=\"sig-name descname\"><span class=\"pre\">remove_field</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">model</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">field</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.db.backends.base.schema.BaseDatabaseSchemaEditor.remove_field\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>Removes the column(s) representing the field from the model's table, along\nwith any unique constraints, foreign key constraints, or indexes caused by\nthat field.</p>\n<p>If the field is a ManyToManyField without a value for <code class=\"docutils literal notranslate\"><span class=\"pre\">through</span></code>, it will\nremove the table created to track the relationship. If\n<code class=\"docutils literal notranslate\"><span class=\"pre\">through</span></code> is provided, it is a no-op.</p>\n</section>\n<section id=\"alter-field\">\n<h3><code class=\"docutils literal notranslate\"><span class=\"pre\">alter_field()</span></code><a class=\"heading-anchor\" href=\"#alter-field\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py method\">\n<dt class=\"sig sig-object py\" id=\"django.db.backends.base.schema.BaseDatabaseSchemaEditor.alter_field\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">BaseDatabaseSchemaEditor.</span></span><span class=\"sig-name descname\"><span class=\"pre\">alter_field</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">model</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">old_field</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">new_field</span></span></em>, <em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">strict</span></span><span class=\"o\"><span class=\"pre\">=</span></span><span class=\"default_value\"><span class=\"pre\">False</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.db.backends.base.schema.BaseDatabaseSchemaEditor.alter_field\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>This transforms the field on the model from the old field to the new one. This\nincludes changing the name of the column (the\n<a class=\"reference internal\" href=\"/zh-hans/2.0/ref/models/fields/#django.db.models.Field.db_column\" title=\"django.db.models.Field.db_column\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">db_column</span></code></a> attribute), changing the type of the\nfield (if the field class changes), changing the <code class=\"docutils literal notranslate\"><span class=\"pre\">NULL</span></code> status of the field,\nadding or removing field-only unique constraints and indexes, changing primary\nkey, and changing the destination of <code class=\"docutils literal notranslate\"><span class=\"pre\">ForeignKey</span></code> constraints.</p>\n<p>The most common transformation this cannot do is transforming a\n<code class=\"docutils literal notranslate\"><span class=\"pre\">ManyToManyField</span></code> into a normal Field or vice-versa; Django cannot do this\nwithout losing data, and so it will refuse to do it. Instead,\n<a class=\"reference internal\" href=\"#django.db.backends.base.schema.BaseDatabaseSchemaEditor.remove_field\" title=\"django.db.backends.base.schema.BaseDatabaseSchemaEditor.remove_field\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">remove_field()</span></code></a> and <a class=\"reference internal\" href=\"#django.db.backends.base.schema.BaseDatabaseSchemaEditor.add_field\" title=\"django.db.backends.base.schema.BaseDatabaseSchemaEditor.add_field\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">add_field()</span></code></a> should be called separately.</p>\n<p>If the database has the <code class=\"docutils literal notranslate\"><span class=\"pre\">supports_combined_alters</span></code>, Django will try and\ndo as many of these in a single database call as possible; otherwise, it will\nissue a separate ALTER statement for each change, but will not issue ALTERs\nwhere no change is required (as South often did).</p>\n</section>\n</section>\n<section id=\"attributes\">\n<h2>Attributes<a class=\"heading-anchor\" href=\"#attributes\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>All attributes should be considered read-only unless stated otherwise.</p>\n<section id=\"connection\">\n<h3><code class=\"docutils literal notranslate\"><span class=\"pre\">connection</span></code><a class=\"heading-anchor\" href=\"#connection\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.db.backends.base.schema.SchemaEditor.connection\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">SchemaEditor.</span></span><span class=\"sig-name descname\"><span class=\"pre\">connection</span></span><a class=\"heading-anchor\" href=\"#django.db.backends.base.schema.SchemaEditor.connection\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>A connection object to the database. A useful attribute of the connection is\n<code class=\"docutils literal notranslate\"><span class=\"pre\">alias</span></code> which can be used to determine the name of the database being\naccessed.</p>\n<p>This is useful when doing data migrations for <a class=\"reference internal\" href=\"/zh-hans/2.0/howto/writing-migrations/#data-migrations-and-multiple-databases\"><span class=\"std std-ref\">migrations with multiple\ndatabases</span></a>.</p>\n</section>\n</section>","rootId":"module-django.db.backends.base.schema","toc":[{"title":"Methods","anchor":"methods","children":[{"title":"execute()","anchor":"execute","children":[]},{"title":"create_model()","anchor":"create-model","children":[]},{"title":"delete_model()","anchor":"delete-model","children":[]},{"title":"add_index()","anchor":"add-index","children":[]},{"title":"remove_index()","anchor":"remove-index","children":[]},{"title":"alter_unique_together()","anchor":"alter-unique-together","children":[]},{"title":"alter_index_together()","anchor":"alter-index-together","children":[]},{"title":"alter_db_table()","anchor":"alter-db-table","children":[]},{"title":"alter_db_tablespace()","anchor":"alter-db-tablespace","children":[]},{"title":"add_field()","anchor":"add-field","children":[]},{"title":"remove_field()","anchor":"remove-field","children":[]},{"title":"alter_field()","anchor":"alter-field","children":[]}]},{"title":"Attributes","anchor":"attributes","children":[{"title":"connection","anchor":"connection","children":[]}]}],"breadcrumbs":[{"docname":"ref/index","title":"API Reference","url":"/zh-hans/2.0/ref/"}],"prev":{"docname":"ref/request-response","title":"Request and response objects","url":"/zh-hans/2.0/ref/request-response/"},"next":{"docname":"ref/settings","title":"Settings","url":"/zh-hans/2.0/ref/settings/"},"formats":{"html":"/zh-hans/2.0/ref/schema-editor/","markdown":"/zh-hans/2.0/ref/schema-editor.md","json":"/zh-hans/2.0/ref/schema-editor.json"},"source":"https://github.com/django/django/blob/stable/2.0.x/docs/ref/schema-editor.txt","official":"https://docs.djangoproject.com/zh-hans/2.0/ref/schema-editor/","inVersions":["6.1","6.0","5.2","5.1","5.0","4.2","4.1","4.0","3.2","3.1","3.0","2.2","2.1","2.0"],"inLocales":["en","zh-hans","fr","ja","id","pt-br","ko","es","el","pl"]}