SQLAlchemy - "命令不同步;你现在不能运行这个命令"

时间:2017-09-06 21:50:44

标签: python mysql sqlalchemy

我试图通过SQLAlchemy将数据提交到MySQL数据库。我被困在日志之间,建议它被提交,但不是,或者得到以下错误。

"Commands out of sync; you can't run this command now"

这个错误是由准系统配置产生的(为简洁起见,并非粘贴所有表格)

db = create_engine('mysql://xxxxx:xxxx@127.0.0.1:3306/ytDb?charset=utf8', convert_unicode=True)
db.echo = True

DBSession = sessionmaker(bind=db)
Base = declarative_base()

def init_db():

  Base.metadata.create_all(bind=db)

class UnprocessedVideos(Base):
  __tablename__ = 'unprocessedvideos'

  videoId = Column(VARCHAR(3072), primary_key=True)
  playlistId = Column(VARCHAR(3072))
  processed = Column(Integer)
  processedAt = Column(DateTime)
  searchTerm = Column(VARCHAR(500))

我的理解是,因为我没有调用下面的任何一个,我的对象应该自动提交。

  

自动提交=假

     

自动冲洗=假

Session = DBSession()
    s = Session.query(UnprocessedVideos).filter(UnprocessedVideos.processed == None).yield_per(10000)
    if not s:
        print("No results")
    else:
        for video in s:
                    # Lots of code stripped out. Left to show the other to ORM commits if certain conditions are matched
                    for url in urls:
                        try:
                            session1 = DBSession()
                            session1.merge(ScrapedVideos(
                                videoId = video.videoId,
                                title =  title,
                                description = description,
                                published = published,
                                viewcount = viewcount,
                                tags = str(tags)
                                ))

                        except sqlalchemy.exc.IntegrityError as exc:
                            reason = exc.message
                            if reason.endswith('is not unique'):
                                db.rollback()

            print("Setting processed here")
            video.processed = 1
            video.processedAt = time.strftime('%Y-%m-%d %H:%M:%S')
            video.searchTerm="amiworking"
            Session.merge(video)

在此配置中,我在日志中收到以下错误

  

sqlalchemy.exc.ProgrammingError :(由于Query-invoked而引发   自动冲洗;如果这次刷新,请考虑使用session.no_autoflush块   过早发生)(_ mysql_exceptions.ProgrammingError)(2014,   "命令不同步;你现在不能运行这个命令")[SQL:'更新   unprocessedvideos SET processed =%s,processedAt =%s,searchTerm =%s   哪里有未经处理的视频。videoId =%s'] [参数:(1,' 2017-09-06   22:37:18',' amiworking',' -816LEY-F9U')]

我已尝试将 sessionmaker 中的自动提交 autoflush 修改为False。当这些禁用并在 Session.merge 之后手动运行以下内容时,db.echo = True的日志表明它已成功提交视频对象内容 - 但这是事实并非如此。

Session.flush()
Session.commit()
Session.close()

有人可以告诉我这次会议出错的地方吗?

0 个答案:

没有答案