Skip to content

Instantly share code, notes, and snippets.

@tell-k
Created September 14, 2012 15:11
Show Gist options
  • Save tell-k/3722527 to your computer and use it in GitHub Desktop.
Save tell-k/3722527 to your computer and use it in GitHub Desktop.
# filter
# * メソッドをつなげて利用できる。
# => SELECT * From entries WHERE id = 1 AND text = "entry"
Entry.query.filter(Entry.id == 1).filter(Entry.text == "entry").all()
# filter_by
# * filterのシンタックスシュガー
Entry.query.filter_by(id=1, text="entry").all()
# get
# * プライマリーキーで取得
# => SELECT * From entries WHERE id = 1
Entry.query.get(1)
# fisrt
# * 最初のレコードのみ取得
# => SELECT * From entries WHERE id = 1 AND text = "entry" LIMIT 1
Entry.query.filter(Entry.id == 1).filter(Entry.text == "entry").first()
# one
# * レコードが一つしかない事を保証
# => 複数結果 => sqlalchemy.orm.exc.MultipleResultsFound が raise
# => 結果無し => sqlalchemy.orm.exc.NoResultFound が raise
Entry.query.filter(Entry.text == "entry").one()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment