我使用session.merge(obj1)来插入和插入行。我偶尔会遇到像这样的错误:
(pymysql.err.IntegrityError) (1062, u"Duplicate entry 'q/FtMv4syu5QXenCXvx6ZGhbLSFYSbo6Kgv8sjuZDZwW4j1Zh3ZFHtey/aqVOe5j' for key 'PRIMARY'")
traceback
Traceback (most recent call last):
File "/var/task/streaming_lambda.py", line 71, in lambda_handler
session.commit()
File "/var/task/sqlalchemy/orm/session.py", line 874, in commit
self.transaction.commit()
File "/var/task/sqlalchemy/orm/session.py", line 461, in commit
self._prepare_impl()
File "/var/task/sqlalchemy/orm/session.py", line 441, in _prepare_impl
self.session.flush()
File "/var/task/sqlalchemy/orm/session.py", line 2137, in flush
self._flush(objects)
File "/var/task/sqlalchemy/orm/session.py", line 2257, in _flush
transaction.rollback(_capture_exception=True)
File "/var/task/sqlalchemy/util/langhelpers.py", line 60, in __exit__
compat.reraise(exc_type, exc_value, exc_tb)
File "/var/task/sqlalchemy/orm/session.py", line 2221, in _flush
flush_context.execute()
File "/var/task/sqlalchemy/orm/unitofwork.py", line 389, in execute
rec.execute(self)
File "/var/task/sqlalchemy/orm/unitofwork.py", line 548, in execute
uow
File "/var/task/sqlalchemy/orm/persistence.py", line 181, in save_obj
mapper, table, insert)
File "/var/task/sqlalchemy/orm/persistence.py", line 799, in _emit_insert_statements
execute(statement, multiparams)
File "/var/task/sqlalchemy/engine/base.py", line 945, in execute
return meth(self, multiparams, params)
File "/var/task/sqlalchemy/sql/elements.py", line 263, in _execute_on_connection
return connection._execute_clauseelement(self, multiparams, params)
File "/var/task/sqlalchemy/engine/base.py", line 1053, in _execute_clauseelement
compiled_sql, distilled_params
File "/var/task/sqlalchemy/engine/base.py", line 1189, in _execute_context
context)
File "/var/task/sqlalchemy/engine/base.py", line 1393, in _handle_dbapi_exception
exc_info
File "/var/task/sqlalchemy/util/compat.py", line 202, in raise_from_cause
reraise(type(exception), exception, tb=exc_tb, cause=cause)
File "/var/task/sqlalchemy/engine/base.py", line 1182, in _execute_context
context)
File "/var/task/sqlalchemy/engine/default.py", line 469, in do_execute
cursor.execute(statement, parameters)
File "/var/task/pymysql/cursors.py", line 166, in execute
result = self._query(query)
File "/var/task/pymysql/cursors.py", line 322, in _query
conn.query(q)
File "/var/task/pymysql/connections.py", line 835, in query
self._affected_rows = self._read_query_result(unbuffered=unbuffered)
File "/var/task/pymysql/connections.py", line 1019, in _read_query_result
result.read()
File "/var/task/pymysql/connections.py", line 1302, in read
first_packet = self.connection._read_packet()
File "/var/task/pymysql/connections.py", line 981, in _read_packet
packet.check_error()
File "/var/task/pymysql/connections.py", line 393, in check_error
err.raise_mysql_exception(self._data)
File "/var/task/pymysql/err.py", line 107, in raise_mysql_exception
raise errorclass(errno, errval)
IntegrityError: (pymysql.err.IntegrityError) (1062, u"Duplicate entry 'q/FtMv4syu5QXenCXvx6ZGhbLSFYSbo6Kgv8sjuZDZwW4j1Zh3ZFHtey/aqVOe5j' for key 'PRIMARY'") [SQL: u'INSERT INTO user_table (_id, collection_name, platform, created_at, updated_at, deleted, account_complete, population_current, state, stars, language, age_range, private_account, user_credit, new_user, anonymous) VALUES (%(_id)s, %(collection_name)s, %(platform)s, %(created_at)s, %(updated_at)s, %(deleted)s, %(account_complete)s, %(population_current)s, %(state)s, %(stars)s, %(language)s, %(age_range)s, %(private_account)s, %(user_credit)s, %(new_user)s, %(anonymous)s)'] [parameters: {'user_credit': 0, 'age_range': None, 'anonymous': 0, 'language': None, 'new_user': 1, 'collection_name': None, 'created_at': datetime.datetime(2000, 1, 1, 0, 0), 'deleted': 0, 'account_complete': None, 'updated_at': None, 'platform': 'undefined', 'state': None, 'stars': None, '_id': 'q/FtMv4syu5QXenCXvx6ZGhbLSFYSbo6Kgv8sjuZDZwW4j1Zh3ZFHtey/aqVOe5jmS3lKMKns0LkrC+uZEifDHg32wqWG6Efm+h4ofptf1kZSKIp54yj243Av+lQFSrQOJd5SeOcn178SodEAmfEEpRhzy3S9NW6RO1e64zpxoY=', 'private_account': 0, 'population_current': 'undefined'}]
我使用sqlalchemy 0.9.1和PyMysql 0.7.9在mysql中与RDS mysql数据库进行通信。
session.merge()
如何生成重复的输入错误?这不是它的目的吗?