我的 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”。为什么此列没有映射到“食物”映射器?