【发布时间】:2015-10-27 20:39:55
【问题描述】:
我有一个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 中配置的现有会话,但我不确定。我暖和了吗?
【问题讨论】:
标签: python python-3.x sqlalchemy pyramid sqlalchemy-continuum