SQLAlchemy和pylons错误“找不到映射器上配置的绑定”

时间:2010-02-04 21:50:27

标签: python sqlalchemy pylons

我不确定我在这里做错了什么来保证这个消息。任何有关我的配置的帮助将不胜感激。

"""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)

从python控制台,我正在执行:

from project.model import *

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

我收到的错误是:

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

感谢您的帮助。

1 个答案:

答案 0 :(得分:3)

此问题已得到解决。我不知道在使用CLI中的pylons时,我必须包含整个环境:

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 *

在此之后执行数据库查询没有问题。