我有一个金字塔应用程序,它通过SQLAlchemy通过基模执行CRUD操作。一切似乎都很顺利。
然后我安装了SQLAlchemy-连续体,以便为某些对象提供历史记录。我所做的配置就是对我的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()(对于稍微冗余的导入,我很抱歉;我决定完全遵循连续体示例和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会话所使用的任何内容,以指向在金字塔中配置的现有会话,但我不确定。我变暖了吗?
发布于 2015-09-23 21:13:54
您正在生成一个会话,当您调用:
DBSession = scoped_session(sessionmaker(extension=ZopeTransactionExtension()))但不把它绑在引擎上。您的金字塔模板pyramid_basemodel已经为您生成了一个会话并将其绑定到引擎。
尝试删除DBSession并使用从pyramid_basemodel导入的会话。
https://stackoverflow.com/questions/31826272
复制相似问题