---
title: "データベースのマイグレーション・オペレーション"
version: 6.0
locale: ja
source: https://docs.djangoproject.com/ja/6.0/ref/contrib/postgres/operations/
canonical: https://djangodocs.dev/ja/6.0/ref/contrib/postgres/operations/
---
# データベースのマイグレーション・オペレーション

これらの [マイグレーションオペレーション](/ja/6.0/ref/migration-operations/) は全て `django.contrib.postgres.operations` モジュールから利用できます。

## マイグレーションを使って拡張機能を作成する

マイグレーションファイルを使用して、データベースにPostgreSQL拡張を作成できます。この例ではhstore拡張を作成しますが、他の拡張にも同じ原則が適用されます。

[`HStoreField`](/ja/6.0/ref/contrib/postgres/fields/#django.contrib.postgres.fields.HStoreField) を使用する最初の `CreateModel` または `AddField` オペレーションの前に、PostgreSQL で hstore 拡張機能を設定するために、 [`HStoreExtension`](#django.contrib.postgres.operations.HStoreExtension) オペレーションを含むマイグレーションを追加してください。たとえば:

```
from django.contrib.postgres.operations import HStoreExtension

class Migration(migrations.Migration):
    ...

    operations = [HStoreExtension(), ...]
```

オペレーションは、拡張機能がすでに存在する場合は追加しないようにスキップします。

ほとんどの拡張機能には、スーパーユーザー権限を持つデータベースユーザーが必要です。Djangoのデータベースユーザーに適切な権限がない場合は、それらの権限を持つユーザーでDjangoのマイグレーション外で拡張機能を作成する必要があります。その場合、Djangoデータベースに接続してクエリ `CREATE EXTENSION IF NOT EXISTS hstore;` を実行してください。

## `CreateExtension`

#### `class CreateExtension(name, hints=None)`

PostgreSQL拡張モジュールをインストールする `Operation` サブクラスです。一般的な拡張モジュールについては、以下のサブクラスのいずれかを使用してください。

#### `name`

これは必須の引数です。インストールする拡張機能の名前です。

#### `hints`

> **New in Django 6.0**

The optional `hints` argument will be passed as `**hints` to the
[`allow_migrate()`](/ja/6.0/topics/db/multi-db/#allow_migrate) method of database routers to assist them in
[making routing decisions](/ja/6.0/topics/db/multi-db/#topics-db-multi-db-hints).

## `BloomExtension`

#### `class BloomExtension(hints=None)`

`bloom` 拡張機能をインストールします。

#### `hints`

> **New in Django 6.0**

The optional `hints` argument will be passed as `**hints` to the
[`allow_migrate()`](/ja/6.0/topics/db/multi-db/#allow_migrate) method of database routers to assist them in
[making routing decisions](/ja/6.0/topics/db/multi-db/#topics-db-multi-db-hints).

## `BtreeGinExtension`

#### `class BtreeGinExtension(hints=None)`

`btree_gin` 拡張機能をインストールします。

#### `hints`

> **New in Django 6.0**

The optional `hints` argument will be passed as `**hints` to the
[`allow_migrate()`](/ja/6.0/topics/db/multi-db/#allow_migrate) method of database routers to assist them in
[making routing decisions](/ja/6.0/topics/db/multi-db/#topics-db-multi-db-hints).

## `BtreeGistExtension`

#### `class BtreeGistExtension(hints=None)`

`btree_gist` 拡張機能をインストールします。

#### `hints`

> **New in Django 6.0**

The optional `hints` argument will be passed as `**hints` to the
[`allow_migrate()`](/ja/6.0/topics/db/multi-db/#allow_migrate) method of database routers to assist them in
[making routing decisions](/ja/6.0/topics/db/multi-db/#topics-db-multi-db-hints).

## `CITextExtension`

#### `class CITextExtension(hints=None)`

`citext` 拡張機能をインストールします。

#### `hints`

> **New in Django 6.0**

The optional `hints` argument will be passed as `**hints` to the
[`allow_migrate()`](/ja/6.0/topics/db/multi-db/#allow_migrate) method of database routers to assist them in
[making routing decisions](/ja/6.0/topics/db/multi-db/#topics-db-multi-db-hints).

## `CryptoExtension`

#### `class CryptoExtension(hints=None)`

`pgcrypto` 拡張機能をインストールします。

#### `hints`

> **New in Django 6.0**

The optional `hints` argument will be passed as `**hints` to the
[`allow_migrate()`](/ja/6.0/topics/db/multi-db/#allow_migrate) method of database routers to assist them in
[making routing decisions](/ja/6.0/topics/db/multi-db/#topics-db-multi-db-hints).

## `HStoreExtension`

#### `class HStoreExtension(hints=None)`

`hstore` 拡張機能をインストールし、後続のマイグレーションで hstore データを解釈するための接続を設定します。

#### `hints`

> **New in Django 6.0**

The optional `hints` argument will be passed as `**hints` to the
[`allow_migrate()`](/ja/6.0/topics/db/multi-db/#allow_migrate) method of database routers to assist them in
[making routing decisions](/ja/6.0/topics/db/multi-db/#topics-db-multi-db-hints).

## `TrigramExtension`

#### `class TrigramExtension(hints=None)`

`pg_trgm` 拡張機能をインストールします。

#### `hints`

> **New in Django 6.0**

The optional `hints` argument will be passed as `**hints` to the
[`allow_migrate()`](/ja/6.0/topics/db/multi-db/#allow_migrate) method of database routers to assist them in
[making routing decisions](/ja/6.0/topics/db/multi-db/#topics-db-multi-db-hints).

## `UnaccentExtension`

#### `class UnaccentExtension(hints=None)`

`unaccent` 拡張機能をインストールします。

#### `hints`

> **New in Django 6.0**

The optional `hints` argument will be passed as `**hints` to the
[`allow_migrate()`](/ja/6.0/topics/db/multi-db/#allow_migrate) method of database routers to assist them in
[making routing decisions](/ja/6.0/topics/db/multi-db/#topics-db-multi-db-hints).

## マイグレーションを使って照合順序 (collation) を管理する

OSでは提供されているが PostgreSQL では提供されていない特定の照合順序を使用してカラムをフィルタリングしたり並べ替えたりする必要がある場合、マイグレーションファイルを使ってデータベース内の照合順序を管理できます。これらの照合順序は、 [`CharField`](/ja/6.0/ref/models/fields/#django.db.models.CharField), [`TextField`](/ja/6.0/ref/models/fields/#django.db.models.TextField) やそのサブクラスの `db_collation` パラメータで使用できます。

例えば、ドイツ語の電話帳順に並べ替えるための照合順序を作成するには、次のようにします:

```
from django.contrib.postgres.operations import CreateCollation

class Migration(migrations.Migration):
    ...

    operations = [
        CreateCollation(
            "case_insensitive",
            provider="icu",
            locale="und-u-ks-level2",
            deterministic=False,
        ),
        ...,
    ]
```

#### `class CreateCollation(name, locale, * (Keyword-only parameters separator (PEP 3102)), provider='libc', deterministic=True)`

与えられた `name`、`locale`、および `provider` で照合順序を作成します。

大文字小文字を区別しないフィルタリングなど、非決定的な照合順序を作成するには、 `deterministic` パラメータを `False` に設定します。

#### `class RemoveCollation(name, locale, * (Keyword-only parameters separator (PEP 3102)), provider='libc', deterministic=True)`

`name` という照合順序を削除します。

逆順にすると、指定された `locale`、`provider`、そして `deterministic` 引数を使用して照合順序を作成します。したがって、この操作を逆にするには `locale` が必要です。

## インデックスの並列操作

PostgreSQLでは、`CREATE INDEX` と `DROP INDEX` 文の `CONCURRENTLY` オプションをサポートしており、書き込みをロックせずにインデックスを追加したり削除したりすることができます。このオプションは、稼働中のデータベースでインデックスを追加したり削除したりする場合に便利です。

#### `class AddIndexConcurrently(model_name, index)`

[`AddIndex`](/ja/6.0/ref/migration-operations/#django.db.migrations.operations.AddIndex) と似ていますが、`CONCURRENTLY` オプションを使用してインデックスを作成します。このオプションを使用する際に注意すべきいくつかの注意事項があります。このオプションの使用に関する詳細は、PostgreSQL の [indexes concurrently をビルドするためのドキュメント](https://www.postgresql.org/docs/current/sql-createindex.html#SQL-CREATEINDEX-CONCURRENTLY) を参照してください。

#### `class RemoveIndexConcurrently(model_name, name)`

[`RemoveIndex`](/ja/6.0/ref/migration-operations/#django.db.migrations.operations.RemoveIndex) と似ていますが、`CONCURRENTLY` オプションを使用してインデックスを削除します。このオプションを使用する際に留意すべきいくつかの注意点があります。詳細は、[PostgreSQLのドキュメント](https://www.postgresql.org/docs/current/sql-dropindex.html) を参照してください。

> **Note**
>
> `CONCURRENTLY` オプションはトランザクション内でサポートされていません (詳細は [non-atomic migration](/ja/6.0/howto/writing-migrations/#non-atomic-migrations) を参照してください)。

## 検証を強制しない制約を追加する

PostgreSQL は `NOT VALID` オプションを `ADD CONSTRAINT` 文でサポートしており、既存の行の検証を行わずに検査制約を追加できます。このオプションは、既存のすべての行が制約を満たすかどうかを確認するためにテーブルをスキャンする手間を省きたい場合に便利です。

`NOT VALID` オプションで作成されたチェック制約を後から検証するには、 [`ValidateConstraint`](#django.contrib.postgres.operations.ValidateConstraint) 操作を使用してください。

詳細については、[PostgreSQL のドキュメント](https://www.postgresql.org/docs/current/sql-altertable.html#SQL-ALTERTABLE-NOTES) を参照してください。

#### `class AddConstraintNotValid(model_name, constraint)`

[`AddConstraint`](/ja/6.0/ref/migration-operations/#django.db.migrations.operations.AddConstraint) に似ていますが、既存の行に対して制約を検証しないものです。

#### `class ValidateConstraint(model_name, name)`

テーブルをスキャンし、既存の行にある指定のチェック制約を検証します。

> **Note**
>
> `AddConstraintNotValid` と `ValidateConstraint` のオペレーションは、2 つの別々のマイグレーションで実行する必要があります。同じアトミック マイグレーション内で両方の操作を実行すると [`AddConstraint`](/ja/6.0/ref/migration-operations/#django.db.migrations.operations.AddConstraint) クラスと同じ効果があります。一方、非アトミック マイグレーション内で操作を行うと、`ValidateConstraint` 操作が失敗した場合にデータベースが不整合の状態になる可能性があります。
