{"title":"SchemaEditor","version":"1.9","locale":"pt-br","docname":"ref/schema-editor","url":"/pt-br/1.9/ref/schema-editor/","canonical":"https://djangodocs.dev/pt-br/1.9/ref/schema-editor/","summary":"class BaseDatabaseSchemaEditor Link para esta definição # 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\">SchemaEditor</code><a class=\"heading-anchor\" href=\"#module-django.db.backends.base.schema\"><span class=\"visually-hidden\">Link para este cabeçalho</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\">class</span> </em><span class=\"sig-name descname\">BaseDatabaseSchemaEditor</span><a class=\"heading-anchor\" href=\"#django.db.backends.base.schema.BaseDatabaseSchemaEditor\"><span class=\"visually-hidden\">Link para esta definição</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\">django.db.migrations</code>), and the\ndatabase abstraction layer that turns things like “create a model” or\n“delete a field” into SQL - which is the job of the <code class=\"docutils literal notranslate\">SchemaEditor</code>.</p>\n<p>It’s unlikely that you will want to interact directly with <code class=\"docutils literal notranslate\">SchemaEditor</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\">SchemaEditor</code>,\nand it’s always accessible via the <code class=\"docutils literal notranslate\">connection.schema_editor()</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> connection<span class=\"o\">.</span>schema_editor<span class=\"p\">()</span> <span class=\"k\">as</span> schema_editor<span class=\"p\">:</span>\n    schema_editor<span class=\"o\">.</span>delete_model<span class=\"p\">(</span>MyModel<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\">ForeignKey</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\">SchemaEditor</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\">SchemaEditor</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\">can_rollback_ddl</code> and\n<code class=\"docutils literal notranslate\">supports_combined_alters</code> are the most important.</p>\n<section id=\"methods\">\n<h2>Métodos<a class=\"heading-anchor\" href=\"#methods\"><span class=\"visually-hidden\">Link para este cabeçalho</span><span aria-hidden=\"true\">#</span></a></h2>\n<section id=\"execute\">\n<h3><code class=\"docutils literal notranslate\">execute()</code><a class=\"heading-anchor\" href=\"#execute\"><span class=\"visually-hidden\">Link para este cabeçalho</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\">BaseDatabaseSchemaEditor.</span><span class=\"sig-name descname\">execute</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">sql</em>, <em class=\"sig-param\">params<span class=\"o\">=</span><span class=\"default_value\">[]</span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.db.backends.base.schema.BaseDatabaseSchemaEditor.execute\"><span class=\"visually-hidden\">Link para esta definição</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\">.sql</code> file if the user wishes.</p>\n</section>\n<section id=\"create-model\">\n<h3><code class=\"docutils literal notranslate\">create_model()</code><a class=\"heading-anchor\" href=\"#create-model\"><span class=\"visually-hidden\">Link para este cabeçalho</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\">BaseDatabaseSchemaEditor.</span><span class=\"sig-name descname\">create_model</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">model</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.db.backends.base.schema.BaseDatabaseSchemaEditor.create_model\"><span class=\"visually-hidden\">Link para esta definição</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\">delete_model()</code><a class=\"heading-anchor\" href=\"#delete-model\"><span class=\"visually-hidden\">Link para este cabeçalho</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\">BaseDatabaseSchemaEditor.</span><span class=\"sig-name descname\">delete_model</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">model</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.db.backends.base.schema.BaseDatabaseSchemaEditor.delete_model\"><span class=\"visually-hidden\">Link para esta definição</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=\"alter-unique-together\">\n<h3><code class=\"docutils literal notranslate\">alter_unique_together()</code><a class=\"heading-anchor\" href=\"#alter-unique-together\"><span class=\"visually-hidden\">Link para este cabeçalho</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\">BaseDatabaseSchemaEditor.</span><span class=\"sig-name descname\">alter_unique_together</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">model</em>, <em class=\"sig-param\">old_unique_together</em>, <em class=\"sig-param\">new_unique_together</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 para esta definição</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>Changes a model’s <a class=\"reference internal\" href=\"/pt-br/1.9/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\">unique_together</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\">alter_index_together()</code><a class=\"heading-anchor\" href=\"#alter-index-together\"><span class=\"visually-hidden\">Link para este cabeçalho</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\">BaseDatabaseSchemaEditor.</span><span class=\"sig-name descname\">alter_index_together</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">model</em>, <em class=\"sig-param\">old_index_together</em>, <em class=\"sig-param\">new_index_together</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 para esta definição</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>Changes a model’s <a class=\"reference internal\" href=\"/pt-br/1.9/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\">index_together</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\">alter_db_table()</code><a class=\"heading-anchor\" href=\"#alter-db-table\"><span class=\"visually-hidden\">Link para este cabeçalho</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\">BaseDatabaseSchemaEditor.</span><span class=\"sig-name descname\">alter_db_table</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">model</em>, <em class=\"sig-param\">old_db_table</em>, <em class=\"sig-param\">new_db_table</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 para esta definição</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\">old_db_table</code> to <code class=\"docutils literal notranslate\">new_db_table</code>.</p>\n</section>\n<section id=\"alter-db-tablespace\">\n<h3><code class=\"docutils literal notranslate\">alter_db_tablespace()</code><a class=\"heading-anchor\" href=\"#alter-db-tablespace\"><span class=\"visually-hidden\">Link para este cabeçalho</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\">BaseDatabaseSchemaEditor.</span><span class=\"sig-name descname\">alter_db_tablespace</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">model</em>, <em class=\"sig-param\">old_db_tablespace</em>, <em class=\"sig-param\">new_db_tablespace</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 para esta definição</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\">add_field()</code><a class=\"heading-anchor\" href=\"#add-field\"><span class=\"visually-hidden\">Link para este cabeçalho</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\">BaseDatabaseSchemaEditor.</span><span class=\"sig-name descname\">add_field</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">model</em>, <em class=\"sig-param\">field</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.db.backends.base.schema.BaseDatabaseSchemaEditor.add_field\"><span class=\"visually-hidden\">Link para esta definição</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\">db_index=True</code> or <code class=\"docutils literal notranslate\">unique=True</code>.</p>\n<p>If the field is a <code class=\"docutils literal notranslate\">ManyToManyField</code> without a value for <code class=\"docutils literal notranslate\">through</code>, instead\nof creating a column, it will make a table to represent the relationship. If\n<code class=\"docutils literal notranslate\">through</code> is provided, it is a no-op.</p>\n<p>If the field is a <code class=\"docutils literal notranslate\">ForeignKey</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\">remove_field()</code><a class=\"heading-anchor\" href=\"#remove-field\"><span class=\"visually-hidden\">Link para este cabeçalho</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\">BaseDatabaseSchemaEditor.</span><span class=\"sig-name descname\">remove_field</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">model</em>, <em class=\"sig-param\">field</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.db.backends.base.schema.BaseDatabaseSchemaEditor.remove_field\"><span class=\"visually-hidden\">Link para esta definição</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\">through</code>, it will\nremove the table created to track the relationship. If\n<code class=\"docutils literal notranslate\">through</code> is provided, it is a no-op.</p>\n</section>\n<section id=\"alter-field\">\n<h3><code class=\"docutils literal notranslate\">alter_field()</code><a class=\"heading-anchor\" href=\"#alter-field\"><span class=\"visually-hidden\">Link para este cabeçalho</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\">BaseDatabaseSchemaEditor.</span><span class=\"sig-name descname\">alter_field</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">model</em>, <em class=\"sig-param\">old_field</em>, <em class=\"sig-param\">new_field</em>, <em class=\"sig-param\">strict<span class=\"o\">=</span><span class=\"default_value\">False</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 para esta definição</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=\"/pt-br/1.9/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\">db_column</code></a> attribute), changing the type of the\nfield (if the field class changes), changing the <code class=\"docutils literal notranslate\">NULL</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\">ForeignKey</code> constraints.</p>\n<p>The most common transformation this cannot do is transforming a\n<code class=\"docutils literal notranslate\">ManyToManyField</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\">remove_field()</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\">add_field()</code></a> should be called separately.</p>\n<p>If the database has the <code class=\"docutils literal notranslate\">supports_combined_alters</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>Atributos<a class=\"heading-anchor\" href=\"#attributes\"><span class=\"visually-hidden\">Link para este cabeçalho</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\">connection</code><a class=\"heading-anchor\" href=\"#connection\"><span class=\"visually-hidden\">Link para este cabeçalho</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\">SchemaEditor.</span><span class=\"sig-name descname\">connection</span><a class=\"heading-anchor\" href=\"#django.db.backends.base.schema.SchemaEditor.connection\"><span class=\"visually-hidden\">Link para esta definição</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\">alias</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=\"/pt-br/1.9/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":"Métodos","anchor":"methods","children":[{"title":"execute()","anchor":"execute","children":[]},{"title":"create_model()","anchor":"create-model","children":[]},{"title":"delete_model()","anchor":"delete-model","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":"Atributos","anchor":"attributes","children":[{"title":"connection","anchor":"connection","children":[]}]}],"breadcrumbs":[{"docname":"ref/index","title":"Referência da API","url":"/pt-br/1.9/ref/"}],"prev":{"docname":"ref/request-response","title":"Request and response objects","url":"/pt-br/1.9/ref/request-response/"},"next":{"docname":"ref/settings","title":"Settings","url":"/pt-br/1.9/ref/settings/"},"formats":{"html":"/pt-br/1.9/ref/schema-editor/","markdown":"/pt-br/1.9/ref/schema-editor.md","json":"/pt-br/1.9/ref/schema-editor.json"},"source":"https://github.com/django/django/blob/stable/1.9.x/docs/ref/schema-editor.txt","official":"https://docs.djangoproject.com/pt-br/1.9/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","1.11","1.10","1.9"],"inLocales":["en","fr","ja","id","pt-br","es"]}