.. FAQ: Databases and models ========================= FAQ: データベースとモデル ========================== :revision-up-to: 17812 (1.4) .. _faq-see-raw-sql-queries: .. How can I see the raw SQL queries Django is running? ---------------------------------------------------- Django が実行している生の SQL クエリを見られますか? ---------------------------------------------------- .. Make sure your Django :setting:`DEBUG` setting is set to ``True``. Then, just do this:: まず、 :setting:`DEBUG` 設定を ``True`` にして Django を動かしているか確認してく ださい。次に、以下のコードを実行します:: >>> 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`` is only available if :setting:`DEBUG` is ``True``. It's a list of dictionaries in order of query execution. Each dictionary has the following:: ``connection.queries`` を使えるのは :setting:`DEBUG` が ``True`` の時だけです。 この値は、クエリの実行順に辞書を並べたものです。各辞書には以下の値が入ってい ます:: .. ``sql`` -- The raw SQL statement ``time`` -- How long the statement took to execute, in seconds. ``sql`` -- 生の SQL 文 ``time`` -- SQL 文の実行にかかった時間を秒で表したもの .. ``connection.queries`` includes all SQL statements -- INSERTs, UPDATES, SELECTs, etc. Each time your app hits the database, the query will be recorded. Note that the SQL recorded here may be :ref:`incorrectly quoted under SQLite `. ``connection.queries`` には、 INSERT, UPDATE, SELECT といった全ての SQL 文 が記録されています。アプリケーションがデータベースを操作する度に、クエリが 記録されてゆきます。ここで記録されている SQL は :ref:`SQLite 上では正しく ないクオート処理` がされているかもしれないことに 注意してください。 .. versionadded:: 1.2 .. If you are using :doc:`multiple databases`, you can use the same interface on each member of the ``connections`` dictionary:: もし :doc:`複数のデータベース` を使用している場合は、 ``connections`` 辞書内の各エイリアスに対し、同じコードを実行できます:: >>> from django.db import connections >>> connections['my_db_alias'].queries .. Can I use Django with a pre-existing database? ---------------------------------------------- 既存のデータベースで Django を使えますか? ------------------------------------------ .. Yes. See :doc:`Integrating with a legacy database `. 使えます。 :doc:`古いデータベースの組み込み方 ` を 参照してください。 .. If I make changes to a model, how do I update the database? ----------------------------------------------------------- モデルを変更したとき、どうやってデータベースを更新すればよいですか? --------------------------------------------------------------------- .. If you don't mind clearing data, your project's ``manage.py`` utility has an option to reset the SQL for a particular application:: データが消えてもかまわないのなら、 ``manage.py`` ユーティリティを使って、特 定のアプリケーションをリセットする SQL を発行してください:: manage.py reset appname .. This drops any tables associated with ``appname`` and recreates them. この操作で、 ``appname`` に関係したテーブルが削除され、再度作成されます。 .. If you do care about deleting data, you'll have to execute the ``ALTER TABLE`` statements manually in your database. もしデータを削除したくないのなら、手作業で ``ALTER TABLE`` 文を実行せねば なりません。 .. There are `external projects which handle schema updates `_, of which the current defacto standard is `south `_. `スキーマのアップデートを扱う外部プロジェクト `_ のデファクトスタンダードは `south `_ です。 .. Do Django models support multiple-column primary keys? ------------------------------------------------------ Django のモデルは複数カラムにわたる主キーをサポートしていますか? ----------------------------------------------------------------- .. No. Only single-column primary keys are supported. いいえ。サポートしているのは単カラムの主キーだけです。 .. 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. しかし、実際には問題はありません。というのも、(``unique_together`` モデルオ プションを指定したり、直接データベースに制約をかけたりして) 複数カラムにわ たる一意性が保たれるようモデルレベルで制約をかけられるからです。単カラムの 主キーは admin インタフェースをうまく稼働させるため、例えば編集や削除対象の オブジェクトを指定する簡潔な手段として必要であるにすぎません。 .. How do I add database-specific options to my CREATE TABLE statements, such as specifying MyISAM as the table type? ------------------------------------------------------------------------------------------------------------------ テーブル形式を MyISAM に指定するなど、データベース固有のオプションを CREATE TABLE 文に追加したいのですが、どうすればよいですか? -------------------------------------------------------------------------------------------------------------------------------- .. 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 an :ref:`SQL initial data file ` that contains ``ALTER TABLE`` statements that do what you want to do. The initial data files are executed in your database after the ``CREATE TABLE`` statements. 私達は、テーブルの形式のようなデータベース固有のオプションに対応するために Django のコードに特殊なケースを追加しないよう心がけています。データベース固 有のオプションを使いたければ、 :ref:`SQL の初期データファイル ` を作成して、その中で ``ALTER TABLE`` 文を使うなどして自分の目的を実現してください。初期データ ファイルはデータベースの中で ``CREATE TABLE`` 文の後に実行されます。 .. For example, if you're using MySQL and want your tables to use the MyISAM table type, create an initial data file and put something like this in it:: 例えば、 MySQL を使っていて、 MyISAM テーブルタイプを使いたい場合には、初期 データファイルを作成して、以下のような行を挿入します:: ALTER TABLE myapp_mytable ENGINE=MyISAM; .. As explained in the :ref:`SQL initial data file ` documentation, this SQL file can contain arbitrary SQL, so you can make any sorts of changes you need to make. `SQL の初期データファイル ` でも説明していますが、 SQL ファイ ルには任意の SQL コードを入れられるので、SQL で行なえる変更なら何でも実現で きます。 .. Why is Django leaking memory? ----------------------------- Django がメモリリークを起こしているのですが、なぜですか? --------------------------------------------------------- .. Django isn't known to leak memory. If you find your Django processes are allocating more and more memory, with no sign of releasing it, check to make sure your :setting:`DEBUG` setting is set to ``False``. If :setting:`DEBUG` is ``True``, then Django saves a copy of every SQL statement it has executed. Django に既知のメモリリークはありません。 Django プロセスがメモリをどんどん 消費して、一向に開放する気配がない場合、 :setting:`DEBUG` が ``True`` にな っていないか調べてみてください。 :setting:`DEBUG` を ``True`` にすると、 Django は実行した SQL 文の全てのコピーを保存するからです。 .. (The queries are saved in ``django.db.connection.queries``. See `How can I see the raw SQL queries Django is running?`_.) (クエリは ``django.db.connection.queries`` で保存されます。 `Django が実行している生の SQL クエリを見られますか?`_ を参照してください。) .. To fix the problem, set :setting:`DEBUG` to ``False``. 問題を解決するには、 :setting:`DEBUG` を ``False`` にしてください。 .. If you need to clear the query list manually at any point in your functions, just call ``reset_queries()``, like this:: クエリリストを手動で消去するには、以下のように ``reset_queries()`` を呼び出 してください:: from django import db db.reset_queries()