0

I'm writing an application in Pylons and I want to add an authorization scheme. I've chosen repoze.what. I followed the tutorial from Pylons cookbook:

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

The problem is that in lib/auth.py I need to include models for User, Group and Permission. Using declarative base in model, it gives me an error when I want to deploy:

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>

I found similar problem here:

SQLAlchemy declarative syntax with autoload (reflection) in Pylons

I have all the models for authorization declared in a separate file from __init__.py. I also followed all indications from above question but there is still something wrong. Anyone found a solution?

Community
  • 1
  • 1
hesler
  • 1

2 Answers2

0

In the model __init__.py you need to bind the engine to the session.

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)

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

Jasmijn
  • 9,370
  • 2
  • 29
  • 43
Roberto Alarcon
  • 1,430
  • 2
  • 18
  • 32
  • This doesn't work. My init_model() looks like this: `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` I forgot to point out that repoze acts as a middleware. In middleware.py I include a method which includes some models of my application (for authorization) and then models are loaded before init_model() is invoked, as I understand. This creates the conflict. – hesler Jun 20 '11 at 18:30
  • I think what you are missing are the autoload and autoload_with args when loading your reflected tables i.e Table('some_table', meta, autoload=True,autoload_with=engine) – webjunkie Jun 21 '11 at 23:24
0

I think I found a solution. I just create an engine in the module that contains models for authorization and I bind it to the metadata. I have no idea why init_model does not do this on its own. So this is not a problem of declarative syntax. Sorry for inconvenience.

hesler
  • 1