【问题标题】:Using repoze.what with declarative syntax in pylons在 pylons 中使用带有声明性语法的 repoze.what
【发布时间】:2011-06-18 16:23:58
【问题描述】:

我正在 Pylons 中编写一个应用程序,我想添加一个授权方案。我选择了 repoze.what。我按照 Pylons 食谱中的教程进行操作:

http://wiki.pylonshq.com/display/pylonscookbook/Authorization+with+repoze.what

问题是在lib/auth.py 中我需要包含用户、组和权限的模型。在模型中使用声明性基础,当我想部署时它给了我一个错误:

sqlalchemy.exc.UnboundExecutionError: No engine is bound to this Table's MetaData.
Pass an engine to the Table via autoload_with=<someengine>, or associate the MetaData
with an engine via metadata.bind=<someengine>

我在这里发现了类似的问题:

SQLAlchemy declarative syntax with autoload (reflection) in Pylons

我在__init__.py 的单独文件中声明了所有授权模型。我也遵循了上述问题的所有迹象,但仍然有问题。 有人找到解决方案了吗?

【问题讨论】:

    标签: python sqlalchemy pylons


    【解决方案1】:

    我想我找到了解决办法。我只是在包含授权模型的模块中创建一个引擎,并将它绑定到元数据。我不知道为什么 init_model 不自己这样做。所以这不是声明性语法的问题。给您带来不便,敬请见谅。

    【讨论】:

      【解决方案2】:

      在模型__init__.py 中,您需要将引擎绑定到会话。

      def init_model(engine):
          """Call me before using any of the tables or classes in the model"""
          ## Reflected tables must be defined and mapped here
          #global reflected_table
          #reflected_table = sa.Table("Reflected", meta.metadata, autoload=True,
          #                           autoload_with=engine)
          #orm.mapper(Reflected, reflected_table)
      
          session = orm.sessionmaker(bind=engine, autoflush=True, autocommit=False)
          meta.metadata.bind = engine
          meta.engine = engine
          meta.Session = orm.scoped_session(session)
      

      资源: http://docs.pylonsproject.org/projects/pylons_framework/dev/advanced_models.html

      【讨论】:

      • 这不起作用。我的 init_model() 看起来像这样:def init_model(engine): """Call me before using any of the tables or classes in the model""" Session.configure(bind=engine) meta.engine = engine Base.metadata.bind = engine 我忘了指出 repoze 充当中间件。在 middleware.py 我包含一个方法,其中包括我的应用程序的一些模型(用于授权),然后在调用 init_model() 之前加载模型,据我所知。这会产生冲突。
      • 我认为您在加载反射表时缺少 autoload 和 autoload_with 参数,即 Table('some_table', meta, autoload=True,autoload_with=engine)
      猜你喜欢
      • 2011-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-12
      • 2019-05-28
      • 1970-01-01
      • 2013-05-03
      • 2020-06-12
      相关资源
      最近更新 更多