レガシー・データベースとキー付きテーブル

□未翻訳

□翻訳中

■翻訳完了(細田謙二)

■レビュー(Omi Chiba)

レガシー・データベースとキー付きテーブル

web2pyは、いくつかの条件の下で、レガシー・データベースに接続することができます。

web2py can connect to legacy databases under some conditions.

最も簡単な方法は、以下の条件を満たしているときです:

The easiest way is when these conditions are met:

  • 各テーブルは、必ず"id"と呼ばれる一意で自動インクリメントした整数フィールドを持つ

  • Each table must have a unique auto-increment integer field called "id"

  • レコードは、必ず"id"フィールドを用いてのみ参照される

  • Records must be referenced exclusively using the "id" field.

既存のテーブルにアクセスするとき、つまり、テーブルが現在のアプリケーションのweb2pyによって作成されていない場合、常にmigrate=Falseとしてください。

When accessing an existing table, i.e., a table not created by web2py in the current application, always set migrate=False.

レ ガシー・テーブルが自動インクリメントした整数フィールドを持つが、それが"id"と呼ばれていない場合、web2pyはまだそれにアクセス可能です。し かしこの場合、テーブル定義にて、Field('....','id')として明示的に含めなければなりません。ここで...は、自動インクリメントした 整数フィールドの名前です。

If the legacy table has an auto-increment integer field but it is not called "id", web2py can still access it but the table definition must contain explicitly as Field('....','id') where ... is the name of the auto-increment integer field.

最後に、レガシー・テーブルが自動インクリメントidでないプライマリキーを使用していた場合、次の例のように、キー付きテーブルを用いてそれにアクセスすることが可能です:

Finally if the legacy table uses a primary key that is not an auto-increment id field it is possible to use a "keyed table", for example:

1.

2.

3.

4.

5.

6.

db.define_table('account',

Field('accnum','integer'),

Field('acctype'),

Field('accdesc'),

primarykey=['accnum','acctype'],

migrate=False)

ただし、現在、これはDB2とMS-SQL、Ingres、Informixに対してのみ利用可能です。しかし、他のものも簡単に加えることができます。

Note that currently this is only available for DB2, MS-SQL, Ingres and Informix, but others can be easily added.

この例では、primarykey属性はプライマリキーからなるフィールドのリストです。書き込み時に、primarykey属性が、すべてのレガシー・テーブルと、サポートされたデータベース・バックエンドに対して機能することは保障されていません。簡単にするために、可能なら、自動インクリメントしたidフィールドを持つデータベースのビューを作成することをお勧めします。

In this example the primarykey attribute is a list of field that constitute the primary key. At the time of writing, we cannot guarantee that the primarykey attribute works with every existing legacy table and every supported database backend. For simplicity, we recommend, if possible, creating a database view that has an auto-increment id field.