阻止SQLAlchemy回滚session.execute查询

时间:2017-02-23 17:00:45

标签: python mysql sqlalchemy

我在“事件”表中有一些数据,看起来像......

+-------------+----------------+
| device_time | player_number  |
+-------------+----------------+
| 12345678    |      1         |
+-------------+----------------+

“播放器”表中的数据看起来像......

+----------------+-------------+
| player_number  | name        |
+----------------+-------------+
| 1              | bob         |
+----------------+-------------+

我正在使用SQLalchemy的session.execute功能来传递一个类似于下面的创建视图语句。

CREATE VIEW test_view AS 
SELECT MIN(device_time) as device_time, player_number FROM event_table_1
WHERE device_time >= 123 
GROUP BY player_number

我有多个事件类型表。我必须级联这些视图以确定按顺序在彼此之后发生的事件。我采用这种观点,并利用这一个作为父母来生成另一个观点...

这看起来像......

CREAT VIEW test_view_2 AS 
SELECT this.player_number as player_number 
FROM event_table_2 as this, test_view as parent             
WHERE this.player_number = parent.player_number
AND this.device_time > parent.device_time
GROUP BY this.player_number

查询在...中运行

def _start_session(self):
        Session = sessionmaker(bind=self.connection_engine)
        session = Session()
        session._model_changes = {}
        MyModels.metadata.create_all(bind=self.connection_engine)
        return session

def _end_session(self, session):
        session.close()

def query_db(self, query):
    session = self._start_session()
    try:
        results = session.execute(query)
    except Exception as e:
        self._end_session(session)
        return

    try:
        session.commit()
    except Exception as e:
        session.rollback()
        self._end_session(session)
        return

    self._end_session(session)
    return results

每当我到达第一个循环结束时,第一个视图已经生成,SQLAlchemy会这样做......

ROLLBACK

此命令删除新创建的VIEW,因此MySQL会抱怨并且整个系统崩溃。

我错过了某个地方阻止SQLAlchemy自动回滚我的更改吗?

0 个答案:

没有答案