0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

DB

0
Posted at

DB操作について

1. 1つのDBに対し複数のファイル・処理(APIなど)から操作しても、DB自体は一つで、全員が同じデータを見ている

2. ただし、各処理(ファイル)で使っているSQLAlchemyのセッションは別々で、それぞれが「自分のメモリ上のキャッシュ(オブジェクトの状態)」を持っている状態

3. そのため、 あるセッションでDBに変更を加えcommit()しても、 他のセッションのメモリ上のオブジェクトは古いままの状態

4. 他のセッションで最新のDB状態を反映させたい時は、db.session.refresh(対象のオブジェクト)を使うか、もう一度クエリで最新の情報を取り直す必要がある

# こんな感じでクエリ再取得
user = User.query.filter_by(id=some_id).first()
# イメージこれに似てる
git add .
git commit -m
git push 
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?