这是我偶然发现的一个奇怪的错误,我不确定它为什么会发生,无论是SQLAlchemy中的错误,还是Flask-SQLAlchemy中的错误,还是我还没有意识到的Python的任何功能.
我们使用Flask 0.11.1,Flask-SQLAlchemy 2.1使用PostgreSQL作为DBMS.
示例使用以下代码更新数据库中的数据:
entry = Entry.query.get(1)
entry.name = 'New name'
db.session.commit()
Run Code Online (Sandbox Code Playgroud)
从Flask shell执行时,这完全正常,因此数据库已正确配置.现在,我们的控制器用于更新条目,略微简化(没有验证和其他样板),如下所示:
def details(id):
entry = Entry.query.get(id)
if entry:
if request.method == 'POST':
form = request.form
entry.name = form['name']
db.session.commit()
flash('Updated successfully.')
return render_template('/entry/details.html', entry=entry)
else:
flash('Entry not found.')
return redirect(url_for('entry_list'))
# In the application the URLs are built dynamically, hence why this instead of @app.route
app.add_url_rule('/entry/details/<int:id>', 'entry_details', details, methods=['GET', 'POST'])
Run Code Online (Sandbox Code Playgroud)
当我在details.html中提交表单时,我可以完全看到更改,这意味着表单已正确提交,有效并且模型对象已更新.但是,当我重新加载页面时,更改已经消失,就好像它已被DBMS回滚一样.
我启用了app.config['SQLALCHEMY_ECHO'] = True
,我可以在自己的手动提交之前看到"ROLLBACK".
如果我换行:
entry = Entry.query.get(id)
Run Code Online (Sandbox Code Playgroud)
至:
entry = db.session.query(Entry).get(id)
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用 Typescript 配置 eslint 以支持 Java 的多行if
缩进标准。
作为记录,Java 标准指定if
用于复杂if
s的多行条件应该缩进两次:
if (
a >= MIN && a <= MAX ||
b >= MIN && b <= MAX
) {
alert("Either value is out of range");
}
Run Code Online (Sandbox Code Playgroud)
似乎没有if
特定条件,所以我尝试使用MemberExpression
,CallExpression
和FunctionExpression
类型,但似乎都不起作用:
我的缩小版.eslintrc.json
如下:
{
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended"
],
"env": {
"browser": true,
"mocha": true
},
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 2015,
"sourceType": "module",
"project": "tsconfig.json"
},
"plugins": [
"@typescript-eslint/eslint-plugin" …
Run Code Online (Sandbox Code Playgroud)