Skip to Content
Essential SQLAlchemy, 2nd Edition
book

Essential SQLAlchemy, 2nd Edition

by Jason Myers, Rick Copeland
May 2025
Intermediate to advanced
208 pages
2h 55m
Japanese
O'Reilly Media, Inc.
Content preview from Essential SQLAlchemy, 2nd Edition

第11章. Alembicを使い始める

この作品はAIを使って翻訳されている。ご意見、ご感想をお待ちしている:translation-feedback@oreilly.com

Alembic は、アプリケーションの進化に伴って必要になるデータベースの変更に対応するマイグレーションを、プログラマが作成・実行する方法を提供する。たとえば、テーブルにカラムを追加したり、モデルから属性を削除したりする。また、まったく新しいモデルを追加したり、既存のモデルを複数のモデルに分割したりすることもある。Alembic は、SQLAlchemy のパワーを活用することで、このような種類の変更を事前に行う方法を提供する。

、Alembicをインストールする必要がある:

pip install alembic

Alembicをインストールしたら、移行環境を作成する必要がある。

移行環境の作成

マイグレーション環境を作成するために、CH12というラベルのついたフォルダを作成し、そのディレクトリに移動する。 次に、alembic init alembic コマンドを実行して、alembic/ディレクトリにマイグレーション環境を作成する。一般的にはmigrations/ディレクトリにマイグレーション環境を作成するが、これはalembic init migrations 。好きなディレクトリ名をつけてもいいが、コード中のモジュール名として使われないような、何か特徴的な名前をつけることをお勧めする。 この初期化プロセスは、マイグレーション環境を作成し、構成オプションを含むalembic.iniファイルも作成する。ディレクトリを見ると、次のような構造になっている:

.
├── alembic
│   ├── README
│   ├── env.py
│   ├── script.py.mako
│   └── versions
└── alembic.ini

新しく作成したマイグレーション環境の中に、env.pyと script.py.makoテンプレートファイル、versions/ディレクトリを発見する。 version/ディレクトリにはマイグレーションスクリプトを格納する。 env.pyファイルは、Alembic が SQLAlchemy エンジンを定義してインスタンス化し、そのエンジンに接続してトランザクションを開始し、Alembic コマンドを実行したときにマイグレーションエンジンを正しく呼び出すために使われる。script.py.makoテンプレートはマイグレーションを作成するときに使われ、マイグレーションの基本構造を定義する。

環境が作成できたら、いよいよアプリケーションで動作するように設定する。

移行環境を設定する

alembic.iniと env.pyファイルにあるの設定を調整して、Alembicがデータベースとアプリケーションで動作するようにする必要がある。まずはalembic.iniファイルで、sqlalchemy.url オプションをデータベース接続文字列に合わせて変更する必要がある。カレントディレクトリにあるalembictest.dbというSQLiteファイルに接続するようにセットしたい。そのためには、sqlalchemy.url 行を以下のように編集する:

sqlalchemy.url = sqlite:///alembictest.db

すべてのAlembicの例では、ORMの宣言的なスタイルを使用して、app/db.pyファイルにすべてのコードを作成する。まず、 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Essential SQLAlchemy, 2nd Edition

Essential SQLAlchemy, 2nd Edition

Jason Myers, Rick Copeland
High Performance Python, 3rd Edition

High Performance Python, 3rd Edition

Micha Gorelick, Ian Ozsvald
Python in a Nutshell, 4th Edition

Python in a Nutshell, 4th Edition

Alex Martelli, Anna Martelli Ravenscroft, Steve Holden, Paul McGuire

Publisher Resources

ISBN: 9798341649866