Python 我一直收到一封“信”;元数据未绑定到引擎或连接。”;尝试使用挂架创建我的SQL表时
这是我目前的代码:Python 我一直收到一封“信”;元数据未绑定到引擎或连接。”;尝试使用挂架创建我的SQL表时,python,pylons,Python,Pylons,这是我目前的代码: def init_model(engine): global t_user 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.typ
def init_model(engine):
global t_user
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, nullable=False),
autoload=True,
autoload_with=engine
)
orm.mapper(User, t_user)
meta.Session.configure(bind=engine)
meta.Session = orm.scoped_session(sm)
meta.engine = engine
然后我尝试执行:
>>> meta.metadata.create_all(bind=meta.engine)
并接收错误:
raise exc.UnboundExecutionError(msg)
sqlalchemy.exc.UnboundExecutionError: The MetaData is not bound to an Engine or Connection. Execution can not proceed without a database to execute against. Either execute with an explicit connection or assign the MetaData's .bind to enable implicit execution.
在我的development.ini中,我有:
# SQLAlchemy database URL
sqlalchemy.url = sqlite:///%(here)s/development.db
我不熟悉Python的挂架,不知道如何解析此消息。对于训练有素的眼睛来说,这可能是一个简单的修复方法。谢谢。此问题已解决。我不知道从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 *
在此之后,数据库查询将顺利执行