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

第1章 スキーマと型 スキーマと型

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

最初にしなければならないことは、テーブルが保持するデータ、そのデータの相互関係、そのデータに対する制約を定義することである。

基礎となるデータベースへのアクセスを提供するために、SQLAlchemy はデータベー スに存在すべきテーブルの表現を必要とする。 これには3つの方法がある:

  • ユーザ定義のTable オブジェクトを使用する。

  • テーブルを表す宣言的なクラスを使う

  • データベースから推測する

この 章では、SQLAlchemy Core で使われているアプローチである、これらのうち最初の ものを取り上げる。他の2つのオプションは、基本を理解した後の章で取り上げる。 Table オブジェクトは、型付けされたカラムとその属性のリストを持ち、共通のメタデータコン テナに関連づけられる。 SQLAlchemy でテーブルを構築するために利用可能な型を見て、スキーマ定義の探索を始めま しょう。

種類

SQLAlchemy の内部で使える型には4つのカテゴリがある:

  • ジェネリック

  • 標準SQL

  • ベンダ特有

  • ユーザ定義

SQLAlchemyは、それぞれのバックエンドデータベースがサポートする実際の SQL の型から抽象化された、多数の汎用型を定義しています。 これらの型はすべてsqlalchemy.types モジュールで利用でき、便宜上sqlalchemy モジュールでも利用できる。では、これらの汎用型がどのように便利なのか考えてみよう。

ブール汎用型は通常BOOLEAN SQL 型を使い、Python 側では真か偽を扱う。しかし、BOOLEAN 型をサポートしていないバックエンドデータベースではSMALLINT も使う。SQLAlchemy のおかげで、この些細なことは隠蔽され、あなたが作るクエリや演算子は、使用され ているデータベースの型に関係なく、その型のフィールドに対して正しく動作することを信頼でき ます。Python のコードでは true か false かだけを扱えばよい。 このような振る舞いはジェネリック型を非常に強力なものにし、データベースの移行時や、データウェアハウスがあるデータベース型でトランザクションが別のデータベース型であるスプリットバックエンドシステムで役に立つ。PythonとSQLの両方におけるジェネリック型とそれに関連する型表現は、 表1-1で見ることができる。

表1-1. 一般的な型表現
SQLAlchemy Python SQL

BigInteger

int

BIGINT

Boolean

bool

BOOLEAN またはSMALLINT

Date

datetime.date

DATE (SQLite: )STRING

DateTime

datetime.datetime

DATETIME (SQLite: )STRING

Enum

str

ENUM またはVARCHAR

Float

float またはDecimal

FLOAT またはREAL

Integer

int

INTEGER

Interval

datetime.timedelta

INTERVAL またはDATE from epoch

LargeBinary

byte

BLOB ...

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