1

我的 SQLAlchemy 0.8 代码如下所示:

模型.py:

from sqlalchemy import Table
from sqlalchemy.orm import mapper

class Food(object):
    pass

def initialize_model(metadata):
    FoodTable = Table('food', metadata, schema='food', autoload=True)
    globals()['Food'] = mapper(Food, FoodTable)

应用程序.py

engine = create_engine(dsn, convert_unicode=True)$
metadata = MetaData(engine)$

import xxx.core.model$
xxx.core.model.initialize_model(metadata)$

session = scoped_session(sessionmaker(bind=engine, twophase=True))

from xxx.core.model import Food
results = session.query(Food).filter(Food.nbo_no==query)

这给了我 Food.ndb_no 的 AttributeError,尽管 Food Table 包含此列“ndb_no”。为什么此列没有映射到“食物”映射器?

4

1 回答 1

1

通过使用声明层并将 Base 直接传递给 initialize_model() 方法来解决:

  9 def initialize_model(Base):
 10     
 11     class Food(Base, Mixin):
 12         __tablename__ = 'food'
 13         __table_args__ = {'schema': 'food', 'autoload': True}
 14     globals()['Food'] = Food
于 2013-01-06T11:44:18.617 回答