Skip to content

app.run(debug=True),会导致flask_sqlalchemy错误! #5655

@gitDebianD

Description

@gitDebianD

开启debug模式后,debug=Ture的时候,flask_sqlalchemy的with self.app.app_context()会重复执行。
例如我写入一条数据 {user:"123",name:"youname"}, 在debug模式下 with self.app.app_context() 会执行完写入完毕后,会重新执行一次,导致程序退出,提示重复写入!
我以为是我代码问题,我检查了无数次,都无法找到我代码问题,最后关闭debug模式后,就可以正常使用。

复现办法:
app.py:
app.run(debug=True)

model.py:
pass

userdb.py:
with self.app.app_context():
new_user = userdbmodel(userdb)(user='11eee',
name=input("input")
)
userdb.session.add(new_user)
userdb.session.commit()

会出现两次input输入,之后会提示重复后程序停止。

目前只要把app.run(debug=True)改为app.run(debug=False),例如:
app.py:
app.run(debug=False)
就只会执行一次,不会报错。

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions