---
title: "FAQ: データベースとモデル"
version: 1.10
locale: ja
source: https://docs.djangoproject.com/ja/1.10/faq/models/
canonical: https://djangodocs.dev/ja/1.10/faq/models/
---
# FAQ: データベースとモデル

## Django が実行している生の SQL クエリを見られますか？

Djangoの設定ファイルの [`DEBUG`](/ja/1.10/ref/settings/#std-setting-DEBUG) が `True` になっていることを確認します。次に、以下のコードを実行します:

```
>>> from django.db import connection
>>> connection.queries
[{'sql': 'SELECT polls_polls.id, polls_polls.question, polls_polls.pub_date FROM polls_polls',
'time': '0.002'}]
```

`connection.queries` は [`DEBUG`](/ja/1.10/ref/settings/#std-setting-DEBUG) が `True` の場合のみに、利用することができます。これはクエリの実行順に辞書を並べたリストです。各辞書には以下の項目が入っています:

```
``sql`` -- The raw SQL statement
``time`` -- How long the statement took to execute, in seconds.
```

`connection.queries` にはINSERT, UPDATE, SELECTなどの全てのSQL文が記録されています。アプリケーションがデータベースを操作するたびに、クエリが記録されてゆきます。

もし [複数のデータベース](/ja/1.10/topics/db/multi-db/) を利用している場合、 `connections` の辞書の各要素に対して、同じコードを実行できます:

```
>>> from django.db import connections
>>> connections['my_db_alias'].queries
```

もし、何らかの機能の中で、クエリのリストをクリアする必要がある場合は、以下のように `reset_queries()` を実行します:

```
from django.db import reset_queries
reset_queries()
```

## 既存のデータベースで Django を使えますか？

使えます。[古いデータベースの組み込み方](/ja/1.10/howto/legacy-databases/) を参照してください。

## モデルを変更した時、どのようにデータベースを更新すればよいですか？

[`スキーママイグレーション`](/ja/1.10/topics/migrations/#module-django.db.migrations) に関するページを参照してください。

データが消えてもかまわないのなら、 `manage.py` ユーティリティの [`flush`](/ja/1.10/ref/django-admin/#django-admin-flush) オプションを利用することで、データベースを [`migrate`](/ja/1.10/ref/django-admin/#django-admin-migrate) コマンドが実行された直後と同じ状態にすることができます。

## Django のモデルは複合主キーをサポートしますか？

いいえ。単一の主キーのみをサポートします。

But this isn't an issue in practice, because there's nothing stopping you from
adding other constraints (using the `unique_together` model option or
creating the constraint directly in your database), and enforcing the
uniqueness at that level. Single-column primary keys are needed for things such
as the admin interface to work; e.g., you need a simple way of being able to
specify an object to edit or delete.

## Django で NoSQL データベースは利用できますか？

NoSQL データベースは Django で公式にはサポートされていません。しかし、[Django non-rel](http://django-nonrel.org/) など、多くのプロジェクトが NoSQL を Django で利用できるような機能を提供しています。

You can also take a look on [the wiki page](https://code.djangoproject.com/wiki/NoSqlSupport) which discusses some alternatives.

## How do I add database-specific options to my CREATE TABLE statements, such as specifying MyISAM as the table type?

We try to avoid adding special cases in the Django code to accommodate all the
database-specific options such as table type, etc. If you'd like to use any of
these options, create a migration with a
[`RunSQL`](/ja/1.10/ref/migration-operations/#django.db.migrations.operations.RunSQL) operation that contains
`ALTER TABLE` statements that do what you want to do.

For example, if you're using MySQL and want your tables to use the MyISAM table
type, use the following SQL:

```
ALTER TABLE myapp_mytable ENGINE=MyISAM;
```
