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

第6章 SQLAlchemy ORMでスキーマを定義する SQLAlchemy ORM でスキーマを定義する

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

SQLAlchemy ORM を使うとき、スキーマの定義は少し異なります。なぜなら、 SQLAlchemy ORM は、基礎となるデータベースのスキーマの代わりに、ユーザ定義のデータオブ ジェクトにフォーカスしているからです。SQLAlchemy Core では、メタデータコンテナを作成し、そのメタデータに関連するTable オブジェクトを宣言した。SQLAlchemy ORM では、declarative_base と呼ばれる特殊な基底クラスを継承するクラスを定義します。declarative_base はメタデータコンテナと、クラスをデータベースのテーブルにマッピングするマッパを結合します。また、クラスのインスタンスが保存されていれば、そのテーブルのレコードにマッピングする。この方法でテーブルを定義してみよう。

ORMクラスでテーブルを定義する

ORMで使う適切なクラスは、4つのことをしなければならない:

  • declarative_base オブジェクトを継承する。

  • __tablename__,がコンテナで、これはデータベースで使用されるテーブル名である。

  • Column オブジェクトである1つ以上の属性をコンテナする。

  • 1つ以上の属性が主キーを構成していることを確認する。

属性に関連する最後の2つの要件をもう少し詳しく調べる必要がある。 まず、ORMクラスでカラムを定義することは、第2章で説明したTable オブジェクトでカラムを定義することとよく似ている。しかし、1つだけ非常に重要な違いがある。ORMクラスでカラムを定義する場合、Column コンストラクタの最初の引数にカラム名を指定する必要はない。その代わりに、カラム名は代入先のクラス属性の名前にセットされる。しかし、ORMは、クラスのインスタンスと基礎となるデータベーステーブルの特定のレコードを一意に識別し、関連付ける方法を持たなければならない。ORMクラスとして定義されたcookies テーブルを見てみよう(例6-1)。

例6-1. ORM クラスとして定義されたクッキーテーブル
from sqlalchemy import Table, Column, Integer, Numeric, String
from sqlalchemy.ext.declarative import declarative_base


Base = declarative_base() 1


class Cookie(Base): 2
    __tablename__ = 'cookies' 

    cookie_id = Column(Integer(), primary_key=True) 
    cookie_name = Column(String
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

Publisher Resources

ISBN: 9798341649866