Flask-SqLAlchemy SELECT(取得)

Flask Flask-SQLAlchemy

概要

当ページはFlask-SQLAlchemyの【SELECT】に関して記載しております。

基本的な内容は以下にまとめてあります。

https://outpust.jp/blog/4ab4e69d-a47c-4be1-a33e-0bdb9c042de9

SELECT

構文
db.session.execute(db.select(model).where(model.item == X)).scalar()
db.session.execute(db.select(model).where(model.item == X)).scalar_one()

db.get_or_404(model, KEY_VALUE, description='XXX')
db.first_or_404(db.select(model).where(model.item== 'X'), description='XXX')
db.one_or_404(db.select(model).where(model.item== 'X'), description='XXX')

db.session.execute(db.select(model).where(model.item== 'X')).scalars()
db.session.execute(db.select(model).where(model.item== 'X')).scalars().all()

https://docs.sqlalchemy.org/en/20/tutorial/data_select.html

【Test】データベース状態
【Test】データベース状態
Testデータベース クラス
class Test(db.Model):
    __tablename__ = 'test'

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(60))

    def __str__(self):
        return f'id:{self.id}, name:{self.name}'

1件取得「.scalar()」

https://docs.sqlalchemy.org/en/20/core/connections.html#sqlalchemy.engine.Result.scalar

1件のみ該当あり

test = db.session.execute(db.select(Test).where(Test.id == 3)).scalar()
print(test)
print(type(test))
if test:
    print("A")
else:
    print("B")

-- 結果 --
id:3, name:test

A

複数件該当あり

test = db.session.execute(db.select(Test).where(Test.name == 'test')).scalar()
print(test)
print(type(test))
if test:
    print("A")
else:
    print("B")

-- 結果 --
id:3, name:test

A

該当なし

test = db.session.execute(db.select(Test).where(Test.name == 'tes')).scalar()
print(test)
print(type(test))
if test:
    print("A")
else:
    print("B")

-- 結果 --
None

B

1件取得「.scalar_one()」

https://docs.sqlalchemy.org/en/20/core/connections.html#sqlalchemy.engine.Result.scalar_one

1件のみ該当あり

test = db.session.execute(db.select(Test).where(Test.id == 3)).scalar_one()
print(test)
print(type(test))
if test:
    print("A")
else:
    print("B")

-- 結果 --
id:3, name:test

A

複数件該当あり

test = db.session.execute(db.select(Test).where(Test.name == 'test')).scalar_one()
print(test)
print(type(test))
if test:
    print("A")
else:
    print("B")

-- 結果 --
Multiple rows were found when exactly one was required
訳:1つの行が必要なのに、複数の行が検出された

該当なし

test = db.session.execute(db.select(Test).where(Test.name == 'tes')).scalar_one()
print(test)
print(type(test))
if test:
    print("A")
else:
    print("B")

-- 結果 --
No row was found when one was required
訳:行が必要なときに行が見つからなかった

1件取得「.get_or_404()」

https://flask-sqlalchemy.palletsprojects.com/en/3.0.x/api/#flask_sqlalchemy.SQLAlchemy.get_or_404

該当あり

test = db.get_or_404(Test, 3, description='test404です')
print(test)
print(type(test))
if test:
    print("A")
else:
    print("B")

-- 結果 --
id:3, name:test

A

該当なし

test = db.get_or_404(Test, 6, description='test404です')
print(test)
print(type(test))
if test:
    print("A")
else:
    print("B")

-- 結果 --
"GET /laboratory/db/select/get_or_404/6 HTTP/1.1" 404 -

1件取得「.first_or_404()」

https://flask-sqlalchemy.palletsprojects.com/en/3.0.x/api/#flask_sqlalchemy.SQLAlchemy.first_or_404

複数件該当あり

test = db.first_or_404(db.select(Test).where(Test.name == 'test'), description='test404です')
print(test)
print(type(test))
if test:
    print("A")
else:
    print("B")

-- 結果 --
id:3, name:test

A

該当なし

test = db.first_or_404(db.select(Test).where(Test.name == 'tes'), description='test404です')
print(test)
print(type(test))
if test:
    print("A")
else:
    print("B")

-- 結果 --
"GET /laboratory/db/select/first_or_404/tes HTTP/1.1" 404 -

1件取得「.one_or_404()」

https://flask-sqlalchemy.palletsprojects.com/en/3.0.x/api/#flask_sqlalchemy.SQLAlchemy.one_or_404

複数件該当あり

test = db.one_or_404(db.select(Test).where(Test.name == 'test'), description='test404です')
print(test)
print(type(test))
if test:
    print("A")
else:
    print("B")

-- 結果 --
"GET /laboratory/db/select/one_or_404/test HTTP/1.1" 404 -

該当なし

test = db.one_or_404(db.select(Test).where(Test.name == 'tes'), description='test404です')
print(test)
print(type(test))
if test:
    print("A")
else:
    print("B")

-- 結果 --
"GET /laboratory/db/select/one_or_404/tes HTTP/1.1" 404 -

複数件該当あり

tests = db.session.execute(db.select(Test).where(Test.name == 'test')).scalars()
print(tests)
print(type(tests))
if test:
    print("A")
else:
    print("B")

-- 結果 --
<sqlalchemy.engine.result.ScalarResult object at 0x00000201D708CF00>
<class 'sqlalchemy.engine.result.ScalarResult'>
A
-- 結果画面 --
3:test
4:test

該当なし

tests = db.session.execute(db.select(Test).where(Test.name == 'tes')).scalars()
print(tests)
print(type(tests))
if test:
    print("A")
else:
    print("B")

-- 結果 --
<sqlalchemy.engine.result.ScalarResult object at 0x000001AB1351C280>
<class 'sqlalchemy.engine.result.ScalarResult'>
A
-- 結果画面 --
何も表示されない

複数件取得「.scalars().all()」

https://docs.sqlalchemy.org/en/20/core/connections.html#sqlalchemy.engine.ScalarResult

複数件該当あり

tests = db.session.execute(db.select(Test).where(Test.name == 'test')).scalars().all()
print(tests)
print(type(tests))
if test:
    print("A")
else:
    print("B")

-- 結果 --
[<Test 3>, <Test 4>]
<class 'list'>
A
-- 結果画面 --
3:test
4:test

該当なし

tests = db.session.execute(db.select(Test).where(Test.name == 'tes')).scalars().all()
print(tests)
print(type(tests))
if test:
    print("A")
else:
    print("B")

-- 結果 --
[]
<class 'list'>
B
-- 結果画面 --
何も表示されない
プロフィール背景画像 プロフィール画像
名前:田中寛之
今まで約20年間、出版社、マスコミ、化粧品会社、システム会社・病院・クリニックにてシステムエンジニアとして勤務。現在、個人事業主「ONLINE-WORLD」として活動し、42歳からPythonを始めました!
プロフィール背景画像 プロフィール画像
名前:田中寛之
今まで約20年間、出版社、マスコミ、化粧品会社、システム会社・病院・クリニックにてシステムエンジニアとして勤務。現在、個人事業主「ONLINE-WORLD」として活動し、42歳からPythonを始めました!