INSERT事务在Heroku PostrgeSQL数据库中中止,如何回滚?

时间:2018-07-02 18:17:04

标签: postgresql heroku

我尝试在开发的网站上注册,您可以在PostgreSQL数据库的POST方法中找到代码here on GitHub,其中包含一些SQL请求。

psycopg2.InternalError: current transaction is aborted, 
                        commands ignored until end of transaction block

这是app.py中的代码:

# User Register
@app.route('/register', methods=['GET', 'POST'])
def register():
    form = RegisterForm(request.form)
    if request.method == 'POST' and form.validate():
        name = form.name.data
        email = form.email.data
        username = form.username.data
        password = sha256_crypt.encrypt(str(form.password.data))

        # Create cursor
        cur = mysql.connection.cursor()

        # Execute query
        cur.execute("INSERT INTO users(name, email, username, password) VALUES(%s, %s, %s, %s)", (name, email, username, password))

        # Commit to DB
        mysql.connection.commit()

        # Close connection
        cur.close()

        flash('You are now registered and can log in', 'success')

        return redirect(url_for('login'))
return render_template('register.html', form=form)

这是完整的错误:

2018-07-02T17:14:35.231415+00:00 heroku[router]: at=info method=GET path="/register" host=rosenberg-moodbot.herokuapp.com request_id=1b86212b-13a9-45be-b6f6-99dd379af98e fwd="193.60.231.83" dyno=web.1 connect=0ms service=4ms status=200 bytes=2769 protocol=https
2018-07-02T17:14:55.966856+00:00 heroku[router]: at=info method=POST path="/register" host=rosenberg-moodbot.herokuapp.com request_id=c9894f61-1d09-4c45-ad08-1460af61b8dc fwd="193.60.231.83" dyno=web.1 connect=1ms service=661ms status=500 bytes=450 protocol=https
2018-07-02T17:14:55.967005+00:00 app[web.1]: [2018-07-02 17:14:55,966] ERROR in app: Exception on /register [POST]
2018-07-02T17:14:55.967013+00:00 app[web.1]: Traceback (most recent call last):
2018-07-02T17:14:55.967016+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 2292, in wsgi_app
2018-07-02T17:14:55.967018+00:00 app[web.1]: response = self.full_dispatch_request()
2018-07-02T17:14:55.967021+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1815, in full_dispatch_request
2018-07-02T17:14:55.967023+00:00 app[web.1]: rv = self.handle_user_exception(e)
2018-07-02T17:14:55.967025+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1718, in handle_user_exception
2018-07-02T17:14:55.967026+00:00 app[web.1]: reraise(exc_type, exc_value, tb)
2018-07-02T17:14:55.967031+00:00 app[web.1]: raise value
2018-07-02T17:14:55.967028+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/flask/_compat.py", line 35, in reraise
2018-07-02T17:14:55.967032+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1813, in full_dispatch_request
2018-07-02T17:14:55.967034+00:00 app[web.1]: rv = self.dispatch_request()
2018-07-02T17:14:55.967036+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/flask/app.py", line 1799, in dispatch_request
2018-07-02T17:14:55.967038+00:00 app[web.1]: return self.view_functions[rule.endpoint](**req.view_args)
2018-07-02T17:14:55.967040+00:00 app[web.1]: File "app.py", line 111, in register
2018-07-02T17:14:55.967042+00:00 app[web.1]: cur.execute("INSERT INTO users(name, email, username, password) VALUES(%s, %s, %s, %s)", (name, email, username, password))
2018-07-02T17:14:55.967044+00:00 app[web.1]: psycopg2.InternalError: current transaction is aborted, commands ignored until end of transaction block
2018-07-02T17:14:55.967053+00:00 app[web.1]: 
2018-07-02T17:14:55.967477+00:00 app[web.1]: 10.123.227.73 - - [02/Jul/2018 17:14:55] "POST /register HTTP/1.1" 500 -

您知道我的方法做错了吗?

根据their faq

  

在向会话发送新命令之前必须运行rollback()

但是我该怎么做呢?我应该将其添加到代码中吗?在哪里?

在声明我的连接后,我试图添加一行:

conn = psycopg2.connect(DATABASE_URL)
conn.setAutoCommit(true)

但是我遇到了以下错误:

AttributeError: 'psycopg2.extensions.connection' object has no attribute 'setAutoCommit'

0 个答案:

没有答案