---
title: "SchemaEditor"
version: 3.2
locale: ja
source: https://docs.djangoproject.com/ja/3.2/ref/schema-editor/
canonical: https://djangodocs.dev/ja/3.2/ref/schema-editor/
---
# `SchemaEditor`

#### `class BaseDatabaseSchemaEditor`

Django のマイグレーションシステムは2つの部分に分かれています。データベースに対してどのような操作を実行するべきかを計算してその結果を保管するロジックの部分　(`django.db.migrations`)と、「モデルを作成する」や「フィールドを削除する」といった操作を SQL に変換する、データベースの抽象レイヤーの部分です。後者の仕事を担当するのが、 `SchemaEditor` です。

Django を使っている普通の開発者が `SchemaEditor` を直接扱うことはほとんどありませんが、自前のマイグレーションシステムを実装したい場合や、より高度なことが必要になった場合には、SQL を書くのではなく、こちらを選びましょう。

Django のデータベースのバックエンドは、それぞれ対応するバージョンの `SchemaEditor` を提供しており、好きなときに `connection.schema_editor()` コンテキストマネージャを使ってアクセスできます。

```
with connection.schema_editor() as schema_editor:
    schema_editor.delete_model(MyModel)
```

It must be used via the context manager as this allows it to manage things
like transactions and deferred SQL (like creating `ForeignKey` constraints).

It exposes all possible operations as methods, that should be called in
the order you wish changes to be applied. Some possible operations or types
of change are not possible on all databases - for example, MyISAM does not
support foreign key constraints.

If you are writing or maintaining a third-party database backend for Django,
you will need to provide a `SchemaEditor` implementation in order to work with
Django's migration functionality - however, as long as your database is
relatively standard in its use of SQL and relational design, you should be able
to subclass one of the built-in Django `SchemaEditor` classes and tweak the
syntax a little.

## メソッド

### `execute()`

#### `BaseDatabaseSchemaEditor.execute(sql, params=())`

Executes the SQL statement passed in, with parameters if supplied. This
is a wrapper around the normal database cursors that allows capture of the SQL
to a `.sql` file if the user wishes.

### `create_model()`

#### `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()`

#### `BaseDatabaseSchemaEditor.delete_model(model)`

Drops the model's table in the database along with any unique constraints
or indexes it has.

### `add_index()`

#### `BaseDatabaseSchemaEditor.add_index(model, index)`

Adds `index` to `model`’s table.

### `remove_index()`

#### `BaseDatabaseSchemaEditor.remove_index(model, index)`

Removes `index` from `model`’s table.

### `add_constraint()`

#### `BaseDatabaseSchemaEditor.add_constraint(model, constraint)`

Adds `constraint` to `model`'s table.

### `remove_constraint()`

#### `BaseDatabaseSchemaEditor.remove_constraint(model, constraint)`

Removes `constraint` from `model`'s table.

### `alter_unique_together()`

#### `BaseDatabaseSchemaEditor.alter_unique_together(model, old_unique_together, new_unique_together)`

Changes a model's [`unique_together`](/ja/3.2/ref/models/options/#django.db.models.Options.unique_together) value; this
will add or remove unique constraints from the model's table until they match
the new value.

### `alter_index_together()`

#### `BaseDatabaseSchemaEditor.alter_index_together(model, old_index_together, new_index_together)`

Changes a model's [`index_together`](/ja/3.2/ref/models/options/#django.db.models.Options.index_together) value; this
will add or remove indexes from the model's table until they match the new
value.

### `alter_db_table()`

#### `BaseDatabaseSchemaEditor.alter_db_table(model, old_db_table, new_db_table)`

Renames the model's table from `old_db_table` to `new_db_table`.

### `alter_db_tablespace()`

#### `BaseDatabaseSchemaEditor.alter_db_tablespace(model, old_db_tablespace, new_db_tablespace)`

Moves the model's table from one tablespace to another.

### `add_field()`

#### `BaseDatabaseSchemaEditor.add_field(model, 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 `ManyToManyField` without a value for `through`, instead
of creating a column, it will make a table to represent the relationship. If
`through` is provided, it is a no-op.

If the field is a `ForeignKey`, this will also add the foreign key
constraint to the column.

### `remove_field()`

#### `BaseDatabaseSchemaEditor.remove_field(model, 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.

If the field is a ManyToManyField without a value for `through`, it will
remove the table created to track the relationship. If
`through` is provided, it is a no-op.

### `alter_field()`

#### `BaseDatabaseSchemaEditor.alter_field(model, old_field, new_field, strict=False)`

This transforms the field on the model from the old field to the new one. This
includes changing the name of the column (the
[`db_column`](/ja/3.2/ref/models/fields/#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.

The most common transformation this cannot do is transforming a
`ManyToManyField` into a normal Field or vice-versa; Django cannot do this
without losing data, and so it will refuse to do it. Instead,
[`remove_field()`](#django.db.backends.base.schema.BaseDatabaseSchemaEditor.remove_field) and [`add_field()`](#django.db.backends.base.schema.BaseDatabaseSchemaEditor.add_field) should be called separately.

If the database has the `supports_combined_alters`, Django will try and
do as many of these in a single database call as possible; otherwise, it will
issue a separate ALTER statement for each change, but will not issue ALTERs
where no change is required.

## 属性

特に言及されない限り、すべての属性は読み取り専用です。

### `connection`

#### `SchemaEditor.connection`

データベースへの  connection オブジェクト。connection の便利な属性に、現在アクセスしているデータベースの名前を特定するのに使える `alias` があります。

特に、この属性は、[複数のデータベースに対するマイグレーション](/ja/3.2/howto/writing-migrations/#data-migrations-and-multiple-databases) を行っているときに役に立ちます。
