我有一个Pyramid应用程序,它通过pyramid_basemodel使用SQLAlchemy进行 CRUD 。一切似乎都很好。
然后我 pip 安装了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.
出于某种原因,它读取以相同方式添加的一个模型,但随后尝试更新它失败并出现相同的错误。
我的猜测是,我需要配置 Continuum 用于 SQLAlchemy 会话的任何内容,以指向 Pyramid 中配置的现有会话,但我不确定。我变热了吗?