代码:
newUser = User( openid = userOpenid, province = userInfoDict[‘province’], avatar_url = avatarUrl, avatar_static_path = avatarStaticPath, language = userInfoDict[‘language’], city = userInfoDict[‘city’], country = userInfoDict[‘country’], sex = userInfoDict[‘sex’], nickname = userInfoDict[‘nickname’]) app.logger.debug(‘newUser=%s’, newUser) db.session.add(newUser) db.session.commit() |
运行出错:
<div–<—————————————————————————— [2016-08-31 10:23:32 +0000] [28597] [ERROR] Error handling request /?code=011U8Wvf0FhjFE1YZUuf0juUvf0U8Wvj&state= Traceback (most recent call last): File “/root/Envs/SIPEvents/lib/python2.7/site-packages/gunicorn/workers/sync.py”, line 135, in handle self.handle_request(listener, req, client, addr) File “/root/Envs/SIPEvents/lib/python2.7/site-packages/gunicorn/workers/sync.py”, line 176, in handle_request respiter = self.wsgi(environ, resp.start_response) File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask/app.py”, line 2000, in __call__ return self.wsgi_app(environ, start_response) File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask/app.py”, line 1991, in wsgi_app response = self.make_response(self.handle_exception(e)) File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask/app.py”, line 1567, in handle_exception reraise(exc_type, exc_value, tb) File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask/app.py”, line 1988, in wsgi_app response = self.full_dispatch_request() File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask/app.py”, line 1641, in full_dispatch_request rv = self.handle_user_exception(e) File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask/app.py”, line 1544, in handle_user_exception reraise(exc_type, exc_value, tb) File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask/app.py”, line 1639, in full_dispatch_request rv = self.dispatch_request() File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask/app.py”, line 1625, in dispatch_request return self.view_functions[rule.endpoint](**req.view_args) File “/root/html/SIPEvents/sipevents/views.py”, line 408, in index savedNewUser = saveUserInfoToDb(respUserInfoDict) File “/root/html/SIPEvents/sipevents/views.py”, line 138, in saveUserInfoToDb db.session.commit() File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/orm/scoping.py”, line 157, in do return getattr(self.registry(), name)(*args, **kwargs) File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/orm/session.py”, line 811, in commit self.transaction.commit() File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/orm/session.py”, line 398, in commit self._prepare_impl() File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/orm/session.py”, line 378, in _prepare_impl self.session.flush() File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/orm/session.py”, line 2065, in flush self._flush(objects) File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/orm/session.py”, line 2183, in _flush transaction.rollback(_capture_exception=True) File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/util/langhelpers.py”, line 60, in __exit__ compat.reraise(exc_type, exc_value, exc_tb) File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/orm/session.py”, line 2147, in _flush flush_context.execute() File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/orm/unitofwork.py”, line 386, in execute rec.execute(self) File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/orm/unitofwork.py”, line 545, in execute uow File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/orm/persistence.py”, line 176, in save_obj mapper, table, insert) File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/orm/persistence.py”, line 776, in _emit_insert_statements execute(statement, multiparams) File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/engine/base.py”, line 947, in execute return meth(self, multiparams, params) File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/sql/elements.py”, line 262, in _execute_on_connection return connection._execute_clauseelement(self, multiparams, params) File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/engine/base.py”, line 1055, in _execute_clauseelement compiled_sql, distilled_params File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/engine/base.py”, line 1191, in _execute_context context) File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/engine/base.py”, line 1386, in _handle_dbapi_exception exc_info File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/util/compat.py”, line 202, in raise_from_cause reraise(type(exception), exception, tb=exc_tb, cause=cause) File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/engine/base.py”, line 1184, in _execute_context context) File “/root/Envs/SIPEvents/lib/python2.7/site-packages/sqlalchemy/engine/default.py”, line 462, in do_execute cursor.execute(statement, parameters) IntegrityError: (sqlite3.IntegrityError) column openid is not unique [SQL: u’INSERT INTO wechat_users (openid, province, avatar_url, avatar_static_path, language, city, country, sex, nickname) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)’] [parameters: (u’oswjmv4X0cCXcfkIwjoDfCkeTVVY’, u’\u6c5f\u82cf’, u’http://wx.qlogo.cn/mmopen/ajNVdqHZLLDYtIJicNl7MjwZK5c1lxAJZ253c9v3JzDib2HDcw8UNKt6gkO19C3qPBaxQZVmPZohkzMhFVWplH2Q/0′, u’img/avatar/oswjmv4X0cCXcfkIwjoDfCkeTVVY.png’, u’zh_CN’, u’\u82cf\u5dde’, u’\u4e2d\u56fd’, 1, u’\u793c\u8c8c’)] |
看起来是:
IntegrityError是完整性错误
column openid is not unique
是openid不是唯一的
-》说明是此处已有openid,再次插入了这个openid的User
-》
看来是:
自己的代码的逻辑,不严谨:
在插入新的User之前,没有去判断是否已存在该用户
-》所以此处改为:
在插入新用户之前,如果已有该用户,就去更新对应的信息:
[已解决]SQLAlchemy中update已有的某列的信息
转载请注明:在路上 » [已解决]SQLAlchemy中commit出错:IntegrityError sqlite3.IntegrityError column openid is not unique SQL