我有下表:
class Feedback(Base):
__tablename__ = 'feedbacks'
__table_args__ = (UniqueConstraint('user_id', 'look_id'),)
id = Column(Integer, primary_key=True)
user_id = Column(Integer, ForeignKey('users.id'), nullable=False)
look_id = Column(Integer, ForeignKey('looks.id'), nullable=False)
我目前在此表中插入了许多违反该 UniqueConstraint 的条目。
我正在使用以下代码:
for comment in session.query(Comment).filter(Comment.type == Comment.TYPE_LOOK).yield_per(100):
feedback = Feedback()
feedback.user_id = User.get_or_create(comment.model_id).id
feedback.look_id = comment.commentable_id
session.add(feedback)
try: # Refer to T20
session.flush()
except IntegrityError,e:
print "IntegrityError", e
session.rollback()
session.commit()
我收到以下错误:
IntegrityError (IntegrityError) duplicate key value violates unique constraint "feedbacks_user_id_look_id_key"
DETAIL: Key (user_id, look_id)=(140, 263008) already exists.
'INSERT INTO feedbacks (user_id, look_id, score) VALUES (%(user_id)s, %(look_id)s, %(score)s) RETURNING feedbacks.id' {'user_id': 140, 'score': 1, 'look_id': 263008}
IntegrityError (IntegrityError) duplicate key value violates unique constraint "feedbacks_user_id_look_id_key"
...
(there's about 24 of these integrity errors here)
...
DETAIL: Key (user_id, look_id)=(173, 263008) already exists.
'INSERT INTO feedbacks (user_id, look_id, score) VALUES (%(user_id)s, %(look_id)s, %(score)s) RETURNING feedbacks.id' {'user_id': 173, 'score': 1, 'look_id': 263008}
No handlers could be found for logger "sqlalchemy.pool.QueuePool"
Traceback (most recent call last):
File "load.py", line 40, in <module>
load_crawl_data_into_feedback()
File "load.py", line 21, in load_crawl_data_into_feedback
for comment in session.query(Comment).filter(Comment.type == Comment.TYPE_LOOK).yield_per(100):
File "/Volumes/Data2/Dropbox/projects/Giordano/venv/lib/python2.7/site-packages/sqlalchemy/orm/query.py", line 2337, in instances
fetch = cursor.fetchmany(self._yield_per)
File "/Volumes/Data2/Dropbox/projects/Giordano/venv/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 3230, in fetchmany
self.cursor, self.context)
File "/Volumes/Data2/Dropbox/projects/Giordano/venv/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 3223, in fetchmany
l = self.process_rows(self._fetchmany_impl(size))
File "/Volumes/Data2/Dropbox/projects/Giordano/venv/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 3343, in _fetchmany_impl
row = self._fetchone_impl()
File "/Volumes/Data2/Dropbox/projects/Giordano/venv/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 3333, in _fetchone_impl
self.__buffer_rows()
File "/Volumes/Data2/Dropbox/projects/Giordano/venv/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 3326, in __buffer_rows
self.__rowbuffer = collections.deque(self.cursor.fetchmany(size))
sqlalchemy.exc.ProgrammingError: (ProgrammingError) named cursor isn't valid anymore None None
在你得出关于这个错误是由 yield_per 引起的结论之前,我可以向你保证,yield_per 不是这里的罪魁祸首。
我尝试了没有唯一约束的相同代码,但根本没有遇到任何错误。
我相信完整性错误导致No handlers could be found for logger "sqlalchemy.pool.QueuePool"。
我假设每个完整性错误都会杀死队列池中的每个“线程”。
有人可以告诉我发生了什么吗?
如果此时我无法对数据做太多事情,您会建议我做什么?