views:

332

answers:

1

I'm not sure what I'm doing wrong here to warrant this message. Any help with my configuration would be appreciated.

"""The application's model objects"""
import sqlalchemy as sa
from sqlalchemy import orm

from project.model import meta

def now():
    return datetime.datetime.now()

def init_model(engine):
    """Call me before using any of the tables or classes in the model"""
    sm = orm.sessionmaker(autoflush=True, autocommit=True, bind=engine)
    meta.Session.configure(bind=engine)
    meta.engine = engine
    meta.Session = orm.scoped_session(sm)

class User(object):
    pass

t_user = sa.Table("User", meta.metadata,
sa.Column("id", sa.types.Integer, primary_key=True),
sa.Column("name", sa.types.String(100), nullable=False),
sa.Column("first_name", sa.types.String(100), nullable=False),
sa.Column("last_name", sa.types.String(100), nullable=False),
sa.Column("email", sa.types.String(100), nullable=False),
sa.Column("password", sa.types.String(32), nullable=False)
)

orm.mapper(User,t_user)

From the python console, I am executing:

from project.model import *

mr_jones = User()
meta.Session.add(mr_jones)
mr_jones.name = 'JR Jones'
meta.Session.commit()

And the error I receive is:

sqlalchemy.exc.UnboundExecutionError: Could not locate a bind configured on mapper Mapper|User|User or this Session

Thanks for your help.

A: 

This issue was resolved. I didn't know that when using pylons from the CLI, I have to include the entire environment:

from paste.deploy import appconfig
from pylons import config

from project.config.environment import load_environment

conf = appconfig('config:development.ini', relative_to='.')
load_environment(conf.global_conf, conf.local_conf)

from project.model import *

After this the database queries executed without a problem.

ensnare