@@ -119,64 +119,61 @@ Django から利用できる MySQL の全ての機能を使うには、 バー
119
119
.. _create your database: http://dev.mysql.com/doc/refman/5.0/en/create-database.html
120
120
.. _データベースを作成: `create your database`_
121
121
122
- .. TBD
123
-
124
122
.. _mysql-collation:
125
123
126
- Collation settings
127
- ~~~~~~~~~~~~~~~~~~
124
+ コレーションに関する設定
125
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
128
126
129
- The collation setting for a column controls the order in which data is sorted
130
- as well as what strings compare as equal. It can be set on a database-wide
131
- level and also per-table and per-column. This is `documented thoroughly`_ in
132
- the MySQL documentation. In all cases, you set the collation by directly
133
- manipulating the database tables; Django doesn't provide a way to set this on
134
- the model definition.
127
+ カラムのコレーション設定は、データの保存方法や、文字列の等価性の定義を制御
128
+ しています。コレーションはデータベース全体でも、テーブル単位でも、カラム単
129
+ 位でも設定できます。コレーションの詳細は MySQL のドキュメントで
130
+ `詳しく解説されています <documented thoroughly>`_ 。いずれの場合でも、コレー
131
+ ションの設定は直接データベーステーブルに対して行ってください。 Django はモ
132
+ デル定義でコレーションを設定する方法を提供していません。
135
133
136
134
.. _documented thoroughly: http://dev.mysql.com/doc/refman/5.0/en/charset.html
137
135
138
- By default, with a UTF-8 database, MySQL will use the
139
- ``utf8_general_ci_swedish`` collation. This results in all string equality
140
- comparisons being done in a *case-insensitive* manner. That is, ``"Fred"`` and
141
- ``"freD"`` are considered equal at the database level. If you have a unique
142
- constraint on a field, it would be illegal to try to insert both ``"aa"`` and
143
- ``"AA"`` into the same column, since they compare as equal (and, hence,
144
- non-unique) with the default collation.
145
-
146
- In many cases, this default will not be a problem. However, if you really want
147
- case-sensitive comparisons on a particular column or table, you would change
148
- the column or table to use the ``utf8_bin`` collation. The main thing to be
149
- aware of in this case is that if you are using MySQLdb 1.2.2, the database backend in Django will then return
150
- bytestrings (instead of unicode strings) for any character fields it returns
151
- receive from the database. This is a strong variation from Django's normal
152
- practice of *always* returning unicode strings. It is up to you, the
153
- developer, to handle the fact that you will receive bytestrings if you
154
- configure your table(s) to use ``utf8_bin`` collation. Django itself should work
155
- smoothly with such columns, but if your code must be prepared to call
156
- ``django.utils.encoding.smart_unicode()`` at times if it really wants to work
157
- with consistent data -- Django will not do this for you (the database backend
158
- layer and the model population layer are separated internally so the database
159
- layer doesn't know it needs to make this conversion in this one particular
160
- case).
161
-
162
- If you're using MySQLdb 1.2.1p2, Django's standard
163
- :class:`~django.db.models.CharField` class will return unicode strings even
164
- with ``utf8_bin`` collation. However, :class:`~django.db.models.TextField`
165
- fields will be returned as an ``array.array`` instance (from Python's standard
166
- ``array`` module). There isn't a lot Django can do about that, since, again,
167
- the information needed to make the necessary conversions isn't available when
168
- the data is read in from the database. This problem was `fixed in MySQLdb
169
- 1.2.2`_, so if you want to use :class:`~django.db.models.TextField` with
170
- ``utf8_bin`` collation, upgrading to version 1.2.2 and then dealing with the
171
- bytestrings (which shouldn't be too difficult) is the recommended solution.
172
-
173
- Should you decide to use ``utf8_bin`` collation for some of your tables with
174
- MySQLdb 1.2.1p2, you should still use ``utf8_collation_ci_swedish`` (the
175
- default) collation for the :class:`django.contrib.sessions.models.Session`
176
- table (usually called ``django_session`` and the table
177
- :class:`django.contrib.admin.models.LogEntry` table (usually called
178
- ``django_admin_log``). Those are the two standard tables that use
179
- :class:`~django.db.model.TextField` internally.
136
+ デフォルトの構成では、 MySQL は UTF-8 のデータベースに対して
137
+ ``utf8_general_ci_swedish`` コレーションを使います。この設定では、全ての文
138
+ 字列の等値比較が *大小文字を区別* せず行われます。つまり、 ``"Fred"`` と
139
+ ``"freD"`` はデータベースレベルでは同じ値だとみなされるのです。そのため、デ
140
+ フォルトのコレーションを使っていると、フィールドに ``unique`` 制約をかけた
141
+ ときに、 ``"aa"`` と ``"AA"`` は等しいとみなされ (一意性が破れるので) 同じ
142
+ カラムに入れられなくなります。
143
+
144
+ 大抵のケースでは、デフォルトの設定はさして問題を起こしません。しかし、特定
145
+ のカラムやテーブルで大小文字を区別させたいなら、そのカラムやテーブルに
146
+ ``utf8_bin`` コレーションを指定せねばなりません。その場合、注意すべきなのは、
147
+ MySQLdb 1.2.2 を使っていると、 Django のデータベースバックエンドが、データ
148
+ ベースから取り出した文字列フィールドの値として (unicode 文字列ではなく)
149
+ bytestring を返すということです。このふるまいは、 *常に* unicode を返す、と
150
+ いう Django の通常のやりかたから大きくかけ離れています。コレーションを
151
+ ``utf8_bin`` にして、 bytestring を受け取ったときの扱いは、開発者に委ねられ
152
+ ています。 Django 自体はこのカラムを問題なく扱えますが、一貫性をもってデー
153
+ タを処理したければ、 ``django.utils.encoding.smart_unicode()`` を何度も
154
+ 呼び出すことになるでしょう。(データベースバックエンドレイヤとモデルの操作レ
155
+ イヤは内部的に分離しているため、変換が必要かどうかをデータベースレイヤでは
156
+ 判断できないので) Django はこの変換に関知しないのです。
157
+
158
+ MySQLdb 1.2.1p2 を使っているなら、コレーションを ``utf8_bin`` にしても、
159
+ :class:`~django.db.models.CharField` は unicode 文字列を返します。しかし、
160
+ 今度は :class:`~django.db.models.TextField` が (Python 標準モジュール
161
+ ``array`` の) ``array.array`` を返します。データをデータベースから読み出す
162
+ ときに、変換に必要な情報が手にはいらないので、 Django 側ではどうしようもあ
163
+ りません。
164
+ この問題は `MySQLdb 1.2.2 で解決済み <fixed in MySQLdb 1.2.2>`_ なので、
165
+ ``itf8_bin`` コレーションで :class:`~django.db.models.TextField` を使いたけ
166
+ れば、バージョンを 1.2.2 に上げて、バイト文字列として扱うよう勧めます (それ
167
+ ほど難しくはありません)。
168
+
169
+ MySQLdb 1.2.1p2 で ``utf8_bin`` コレーションの設定されたテーブルを使うのな
170
+ ら、 :class:`django.contrib.sessions.models.Session` のテーブル
171
+ (通常は ``django_session``) や、
172
+ :class:`django.contrib.admin.models.LogEntry` のテーブル
173
+ (通常は ``django_admin_log``) のコレーションに
174
+ ``utf8_collation_ci_swedish`` (デフォルトのコレーション) を使わねばなりませ
175
+ ん。これらは標準の Django のテーブルのうち、内部的に
176
+ :class:`~django.db.model.TextField` を使っているものだからです。
180
177
181
178
.. _fixed in MySQLdb 1.2.2: http://sourceforge.net/tracker/index.php?func=detail&aid=1495765&group_id=22307&atid=374932
182
179
@@ -263,51 +260,47 @@ Django はスキーマを作成する際にストレージエンジンを指定
263
260
264
261
.. _AlterModelOnSyncDB: http://code.djangoproject.com/wiki/AlterModelOnSyncDB
265
262
266
- .. TBD
267
-
268
- Boolean fields in Django
269
- -------------------------
263
+ ``BooleanField`` に関する注意
264
+ -------------------------------
270
265
271
- Since MySQL doesn't have a direct ``BOOLEAN`` column type, Django uses a
272
- ``TINYINT`` column with values of ``1`` and ``0`` to store values for the
273
- :class:`~django.db.models.BooleanField` model field. Refer to the documentation
274
- of that field for more details, but usually this won't be something that will
275
- matter unless you're printing out the field values and are expecting to see
276
- ``True`` and ``False.``.
266
+ MySQL には直接的な ``BOOLEAN`` カラム型がないので、 Django は
267
+ :class:`~django.db.models.BooleanField` の値を ``TINYINT`` カラムを使って 0
268
+ または 1 で保存します。詳しくはモデルフィールドのドキュメントを参照してくだ
269
+ さい。ただし、フィールドの値を出力したり、値が ``True`` や ``False`` でなけ
270
+ ればならないような場合を除いて、特に問題はありません。
277
271
278
272
.. _sqlite-notes:
279
273
280
- SQLite notes
281
- ============
274
+ SQLite に関する注意
275
+ =====================
282
276
283
- Versions of SQLite 3.3.5 and older `contain a bug`_ when handling ``ORDER BY``
284
- parameters. This can cause problems when you use the ``select`` parameter for
285
- the ``extra()`` QuerySet method. The bug can be identified by the error message
286
- ``OperationalError: ORDER BY terms must not be non-integer constants``. The
287
- problem can be solved updating SQLite to version 3.3.6 or newer, possibly also
288
- updating the ``pysqlite2`` Python module in the process.
277
+ バージョン 3.3.5 以前の SQLite には、 ``ORDER_BY`` パラメタの扱いかたに
278
+ `バグがあります <contain a bug>`_ 。そのため、クエリセットメソッド
279
+ ``extra()`` を使って ``select`` のパラメタを指定しようとすると問題を引き起
280
+ こします。このバグは、
281
+ ``OperationalError: ORDER BY terms must not be non-integer constants`` とい
282
+ うメッセージを出力します。 SQLite のバージョンを 3.3.6 に上げ、
283
+ ``pysqlite2`` モジュールを更新すれば、問題を解決できます。
289
284
290
285
.. _contain a bug: http://www.sqlite.org/cvstrac/tktview?tn=1768
286
+
287
+ バージョン 3.3.6 は 2006 年の 4 月にリリースされており、現在手に入る各プラッ
288
+ トフォームのほとんどのバイナリ配布物で、Python から ``pysqlite2`` や
289
+ ``sqlite3`` を介して使える SQLite は新しいバージョンなので、あまり大きなイ
290
+ ンパクトはありません。
291
+
292
+ ただし、 Windows の場合、 Python 2.5 の公式の安定版リリースのバイナリ配布物
293
+ には (今のところ 2.5.2 も)、 SQLite 3.3.4 が組み込まれているため、バグが確
294
+ 認されています。このため (Django 1.0 で) 3 つのテストスイートが失敗します。
295
+ この問題は、上で述べたように、 新しいバージョン SQLite が組み込まれた
296
+ ``pysqlite2`` (``pysqlite-2.x.x.win32-py2.5.exe``) をダウンロードしてインス
297
+ トールすれば解決できます。 Python 2.6 には新たなバージョンの SQLite が付属
298
+ する予定なので、この問題の影響を受けないはずです。
291
299
292
- This has a very low impact because 3.3.6 was released in April 2006, so most
293
- current binary distributions for different platforms include newer version of
294
- SQLite usable from Python through either the ``pysqlite2`` or the ``sqlite3``
295
- modules.
296
-
297
- However, in the case of Windows, the official binary distribution of the stable
298
- release of Python 2.5 (2.5.2 as of now) includes SQLite 3.3.4 so the bug can
299
- make itself evident in that platform. There are (as of Django 1.0) even three
300
- tests in the Django test suite that will fail when run under this setup. As
301
- described above, this can be solved by downloading and installing a newer
302
- version of ``pysqlite2`` (``pysqlite-2.x.x.win32-py2.5.exe``) that includes and
303
- uses a newer version of SQLite. Python 2.6 will ship with a newer version of
304
- SQLite and so will no be affected by this issue.
305
-
306
- If you are in such platform and find yourself in the need to update
307
- ``pysqlite``/SQLite, you will also need to manually modify the
308
- ``django/db/backends/sqlite3/base.py`` file in the Django source tree so it
309
- attempts to import ``pysqlite2`` before that ``sqlite3`` and so it can take
310
- advantage of the new ``pysqlite2``/SQLite versions.
300
+ 上記のプラットフォームを使っていて、 ``pysqlite`` や SQLite を更新する必要
301
+ があるなら、Django のソースツリーの ``django/db/backends/sqlite3/base.py``
302
+ ファイルを手で編集して、 ``sqlite3`` より ``pysqlite2`` が先に import され
303
+ るように変更してください。
311
304
312
305
.. _oracle-notes:
313
306
.. _Oracle notes:
0 commit comments