SQLAlchemy-Continuum和Pyramid:UnboundExecutionError

时间:2015-08-05 07:40:36

标签: python python-3.x sqlalchemy pyramid sqlalchemy-continuum

我有一个Pyramid应用程序通过SQLAlchemypyramid_basemodel进行CRUD。一切似乎都很好。

然后我点了安装SQLAlchemy-Continuum,为某些对象提供历史记录。我所做的就是对我的models.py文件进行以下更改:

import sqlalchemy as sa
from sqlalchemy import (event, Column, Index, Integer, Text, String, Date, DateTime, \
    Float, ForeignKey, Table, Boolean,)
from sqlalchemy.orm import (relationship, backref, mapper, scoped_session, sessionmaker,)

from pyramid_basemodel import Base, BaseMixin, Session, save
from pyramid_fullauth.models import User
from sqlalchemy_continuum import make_versioned

from colanderalchemy import setup_schema
from zope.sqlalchemy import ZopeTransactionExtension

DBSession = scoped_session(sessionmaker(extension=ZopeTransactionExtension()))
event.listen(mapper, 'mapper_configured', setup_schema)

# Continuum setup
make_versioned()

# FOR EACH VERSIONED MODEL I ADD __versioned__ = {} at the start of each model def. Eg:
class Thing(Base):
    __versioned__ = {}
    __tablename__ = 'thing'
    id = sa.Column(Integer, primary_key=True)
    related_id = sa.Column(Integer, ForeignKey('OtherThing.id'))
    other_thing = sa.orm.relationship("OtherThing", backref="thing")
    description = sa.Column(String(length=100))
    a_date = sa.Column(Date)
    some_hours = sa.Column(Integer)
    b_date = sa.Column(Date)
    more_hours = sa.Column(Integer)


sa.orm.configure_mappers()

(对不起有点多余的导入;我决定完全遵循Continuum示例和import sqlalchemy as sa,并转而在我版本化的模型中使用该表示法。我也可能做傻,猴子 - 基于对不同教程的半理解,看看猴子做的事情。)

这个设置允许我运行alembic revision --autogenerate并在数据库中生成ModelHistory表,但是当我去一些读取现在版本模型的页面时,它们会给出错误

sqlalchemy.exc.UnboundExecutionError: This session is not bound to a single Engine or Connection, and no context was provided to locate a binding.

由于某种原因,它读取了以相同方式添加的一个模型,但随后尝试更新它失败并出现相同的错误。

我的猜测是我需要为SQLAlchemy会话配置Continuum用于指向金字塔中配置的现有会话,但我不确定。我变暖了吗?

1 个答案:

答案 0 :(得分:1)

您在致电时正在生成会话:

DBSession = scoped_session(sessionmaker(extension=ZopeTransactionExtension()))

但不将其绑定到引擎。您的金字塔模板pyramid_basemodel已经为您生成会话并将其绑定到引擎。

尝试删除DBSession并使用从pyramid_basemodel导入的会话。