数据库
首页 > 数据库> > python-SQLAlchemy和塔的错误“无法找到在映射器上配置的绑定”

python-SQLAlchemy和塔的错误“无法找到在映射器上配置的绑定”

作者:互联网

我不确定我在做什么错,以保证此消息.我的配置的任何帮助将不胜感激.

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

谢谢你的帮助.

解决方法:

该问题已解决.我不知道从CLI使用挂架时,是否必须包括整个环境:

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 *

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

标签:pylons,sqlalchemy,python
来源: https://codeday.me/bug/20191210/2100646.html