Release: 2.0.31 | Release Date: June 18, 2024

SQLAlchemy 2.0 Documentation - 私家版日本語訳ドキュメント

SQLAlchemy 2.0 Documentation - 私家版日本語訳ドキュメント

Home

Frequently Asked Questions

  • Installation
  • Connections / Engines
  • MetaData / Schema¶
    • My program is hanging when I say table.drop() / metadata.drop_all()
    • Does SQLAlchemy support ALTER TABLE, CREATE VIEW, CREATE TRIGGER, Schema Upgrade Functionality?
    • How can I sort Table objects in order of their dependency?
    • How can I get the CREATE TABLE/ DROP TABLE output as a string?
    • How can I subclass Table/Column to provide certain behaviors/configurations?
  • SQL式
  • ORM Configuration
  • Performance
  • Sessions / Queries
  • Third Party Integration Issues

Home

  • Previous: Connections / Engines
  • Next: SQL式
  • Up: Home
    • Frequently Asked Questions
  • On this page:
    • MetaData / Schema
      • My program is hanging when I say table.drop() / metadata.drop_all()
      • Does SQLAlchemy support ALTER TABLE, CREATE VIEW, CREATE TRIGGER, Schema Upgrade Functionality?
      • How can I sort Table objects in order of their dependency?
      • How can I get the CREATE TABLE/ DROP TABLE output as a string?
      • How can I subclass Table/Column to provide certain behaviors/configurations?

MetaData / Schema¶

  • My program is hanging when I say table.drop() / metadata.drop_all()

  • Does SQLAlchemy support ALTER TABLE, CREATE VIEW, CREATE TRIGGER, Schema Upgrade Functionality?

  • How can I sort Table objects in order of their dependency?

  • How can I get the CREATE TABLE/ DROP TABLE output as a string?

  • How can I subclass Table/Column to provide certain behaviors/configurations?

My program is hanging when I say table.drop() / metadata.drop_all()¶

これは通常、次の2つの条件に対応します。1. PostgreSQLを使用しており、テーブルのロックに関して厳密である。2. テーブルのロックを含み、DROP文で使用されている接続とは異なる接続がまだ開いている。パターンの最も小さなバージョンを以下に示します。:

connection = engine.connect()
result = connection.execute(mytable.select())

mytable.drop(engine)

上記では、接続プール接続がチェックアウトされています。さらに、上記の結果オブジェクトもこの接続へのリンクを保持しています。”暗黙的な実行”が使用されている場合、結果オブジェクトが閉じられるか、すべての行が使い果たされるまで、結果はこの接続を開いたままにします。

mytable.drop(engine) の呼び出しは、 Engine から取得した2番目の接続でDROP TABLEを発行しようとします。この接続はロックされます。

これを解決するには、DROP TABLEを発行する前にすべての接続を閉じます。:

connection = engine.connect()
result = connection.execute(mytable.select())

# fully read result sets
result.fetchall()

# close connections
connection.close()

# now locks are removed
mytable.drop(engine)

Does SQLAlchemy support ALTER TABLE, CREATE VIEW, CREATE TRIGGER, Schema Upgrade Functionality?¶

一般的なALTERサポートはSQLAlchemyには直接存在しません。アドホックな特別なDDLでは、 DDL および関連する構文を使用することができます。この件に関する議論については Customizing DDL を参照してください。

より包括的なオプションは、AlembicやSQLAlchemy-Migrateなどのスキーママイグレーションツールを使用することです。これについては Altering Database Objects through Migrations を参照してください。

How can I sort Table objects in order of their dependency?¶

これは MetaData.sorted_tables 関数から利用できます:

metadata_obj = MetaData()
# ... add Table objects to metadata
ti = metadata_obj.sorted_tables
for t in ti:
    print(t)

How can I get the CREATE TABLE/ DROP TABLE output as a string?¶

最近のSQLAlchemyには、DDL操作を表す節構造があります。これらは、他のSQL式と同様に文字列に変換できます。:

from sqlalchemy.schema import CreateTable

print(CreateTable(mytable))

特定のエンジンに固有の文字列を取得するには、次のようにします。:

print(CreateTable(mytable).compile(engine))

create_mock_engine() で利用できる Engine の特別な形式もあります。これを使うと、メタデータ作成シーケンス全体を文字列としてダンプすることができます。:

from sqlalchemy import create_mock_engine

def dump(sql, *multiparams, **params):
    print(sql.compile(dialect=engine.dialect))

engine = create_mock_engine("postgresql+psycopg2://", dump)
metadata_obj.create_all(engine, checkfirst=False)

Alembic ツールは、データベースの移行をSQLスクリプトとしてレンダリングする”オフライン”SQL生成モードもサポートしています。

How can I subclass Table/Column to provide certain behaviors/configurations?¶

Table と Column は、直接サブクラスを作成するのに適した対象ではありません。しかし、作成関数を使って構築時の振る舞いを取得する簡単な方法や、制約規則や添付イベントを使った命名規則など、スキーマオブジェクト間のリンクに関連する振る舞いがあります。これらのテクニックの多くの例は、 Naming Conventions で見ることができます。

Previous: Connections / Engines Next: SQL式
© Copyright 2007-2024, the SQLAlchemy authors and contributors.

flambé! the dragon and The Alchemist image designs created and generously donated by Rotem Yaari.

Created using Sphinx 7.2.6. Documentation last generated: Sat Aug 17 14:19:19 2024 JST