Python 在SQLAlchemy中使用声明性_base时,如何在需要时绑定引擎?
这是我的密码:Python 在SQLAlchemy中使用声明性_base时,如何在需要时绑定引擎?,python,database,orm,sqlalchemy,Python,Database,Orm,Sqlalchemy,这是我的密码: from sqlalchemy import create_engine, Column, Integer from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import sessionmaker database_url = 'mysql://some_path' engine = create_engine(database_url) Base = declarative_
from sqlalchemy import create_engine, Column, Integer
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
database_url = 'mysql://some_path'
engine = create_engine(database_url)
Base = declarative_base(engine)
class Users(Base):
__tablename__ = 'Users'
__table_args__ = {'autoload':True}
metadata = Base.metadata
Session = sessionmaker(bind=engine)
session = Session()
它是有效的,但是…是否可以在需要时绑定引擎,而不仅仅是在导入时?这样我就可以将此实现封装到类中 现在,我明白了
class Users(Base):
File "/usr/lib/python2.5/site-packages/SQLAlchemy-0.6.5-py2.5.egg/sqlalchemy/ext/declarative.py", line 1231, in __init__
_as_declarative(cls, classname, cls.__dict__)
File "/usr/lib/python2.5/site-packages/SQLAlchemy-0.6.5-py2.5.egg/sqlalchemy/ext/declarative.py", line 1122, in _as_declarative
**table_kw)
File "/usr/lib/python2.5/site-packages/SQLAlchemy-0.6.5-py2.5.egg/sqlalchemy/schema.py", line 209, in __new__
table._init(name, metadata, *args, **kw)
File "/usr/lib/python2.5/site-packages/SQLAlchemy-0.6.5-py2.5.egg/sqlalchemy/schema.py", line 260, in _init
msg="No engine is bound to this Table's MetaData. "
File "/usr/lib/python2.5/site-packages/SQLAlchemy-0.6.5-py2.5.egg/sqlalchemy/schema.py", line 2598, in _bind_or_error
raise exc.UnboundExecutionError(msg)
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>
类用户(基本):
文件“/usr/lib/python2.5/site packages/SQLAlchemy-0.6.5-py2.5.egg/SQLAlchemy/ext/declarative.py”,第1231行,在__
_as_声明性(cls,classname,cls.\uu dict\uuuuu)
文件“/usr/lib/python2.5/site packages/SQLAlchemy-0.6.5-py2.5.egg/SQLAlchemy/ext/declarative.py”,第1122行,在声明性
**表1(千瓦)
文件“/usr/lib/python2.5/site packages/SQLAlchemy-0.6.5-py2.5.egg/SQLAlchemy/schema.py”,第209行,新__
表._init(名称、元数据、*args、**kw)
文件“/usr/lib/python2.5/site packages/SQLAlchemy-0.6.5-py2.5.egg/SQLAlchemy/schema.py”,第260行,在_init中
msg=“没有引擎绑定到此表的元数据。”
文件“/usr/lib/python2.5/site packages/SQLAlchemy-0.6.5-py2.5.egg/SQLAlchemy/schema.py”,第2598行,在绑定或错误中
提升exc.UnboundExecutionError(消息)
sqlalchemy.exc.UnboundExecutionError:没有引擎绑定到此表的元数据。通过autoload_with=将引擎传递给表,或通过MetaData.bind将元数据与引擎关联=
未指定引擎时:
Base=declarative_Base()
否,您不能在autoload
选项设置为True
的情况下使用后期绑定,因为SQLAlchemy无法在不了解数据库架构的情况下编译映射程序。至少在SQLAlchemy 0.9中,您可以使用延迟反射来延迟绑定。请参见中的示例
在这里,您可以找到以下示例(简化):
e是发动机。链路断开
from sqlalchemy.ext.declarative import declarative_base, DeferredReflection
Base = declarative_base(cls=DeferredReflection)
class Foo(Base):
__tablename__ = 'foo'
bars = relationship("Bar")
class Bar(Base):
__tablename__ = 'bar'
Base.prepare(e)