考虑以下代码
import peewee
SQL_CONN = peewee.MySQLDatabase(database=SQL_DATA,
host=SQL_HOST,
port=SQL_PORT,
user=SQL_USER,
passwd=SQL_PASS)
class User(peewee.Model):
name = peewee.CharField(max_length=100, primary_key=True)
born = peewee.DateTimeField()
print(SQL_CONN.is_closed()) # True
print(User.select().where(User.name == "Jack").execute()) # Silently opens a connection w/o letting me know
print(SQL_CONN.is_closed()) # False
这将在后台自动执行SQL_CONN.connect()
。
如何禁用此功能 -peewee
如果数据库未连接,则强制抛出异常 - 而不是在不通知我的情况下自动连接到它。