我想知道SQLAlchemy如何跟蹤在SQLAlchemy之外進行的更改(例如手動更改)?SQLAlchemy如何跟蹤數據庫更改?
到現在爲止,我曾經把db.session.commit()
放在每個可以在SQLAlchemy之外更改的值之前。這是一種不好的做法嗎?如果是的話,有沒有更好的方法來確保我有最新的價值?我實際上已經在下面創建了一個小腳本來檢查它,顯然,SQLAlchemy可以檢測到外部更改,而不會每次調用db.session.commit()
。
感謝,
P.S:我真的想了解所有的魔法是如何發生的背後SQLAlchemy的工作。有沒有人有指向一些文檔解釋SQLAlchemy的幕後工作?
import os
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
# Use SQLlite so this example can be run anywhere.
# On Mysql, the same behaviour is observed
basedir = os.path.abspath(os.path.dirname(__file__))
db_path = os.path.join(basedir, "app.db")
app.config["SQLALCHEMY_DATABASE_URI"] = 'sqlite:///' + db_path
db = SQLAlchemy(app)
# A small class to use in the test
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(100))
# Create all the tables and a fake data
db.create_all()
user = User(name="old name")
db.session.add(user)
db.session.commit()
@app.route('/')
def index():
"""The scenario: the first request returns "old name" as expected.
Then, I modify the name of User:1 to "new name" directly on the database.
On the next request, "new name" will be returned.
My question is: how SQLAlchemy knows that the value has been changed?
"""
# Before, I always use db.session.commit()
# to make sure that the latest value is fetched.
# Without db.session.commit(),
# SQLAlchemy still can track change made on User.name
# print "refresh db"
# db.session.commit()
u = User.query.filter_by(id=1).first()
return u.name
app.run(debug=True)
SQLAlchemy文檔非常非常詳盡,並提及外部資源。您是否考慮閱讀這些內容以瞭解其工作原理? – davidism
另外,還不清楚你發佈的代碼有什麼問題。請[編輯]在帖子本身中創建描述您的問題的[mcve] *。 – davidism
嘿感謝您的反饋。我試圖在SQLAlchemy doc中搜索,但沒有找到描述這個「魔術」背後的機制的部分。也許這是很多ORM使用的技術,所以SQLAlchemy不需要指出它? – Son