Sqlalchemy模型名称与automap

时间:2016-05-25 10:05:47

标签: python sqlalchemy

我有大型数据库,它有表" menu_category"(model menu.Category)和" kitchen_category"(model kitchen.Category)。

Model" ProductDetail"这两种型号都有外键:

from kitchen import Category as KitchenCategory

class Detail(Base):
    __tablename__ = 'menu_productdetail'

    category_id = Column(ForeignKey('menu_category.id'))
    category = relationship(Category)

    kitchen_category_id = Column(ForeignKey('kitchen_category.id'))
    kitchen_category = relationship(KitchenCategory)

此代码产生错误:

  

reverse_property'类别' on关系Category.detail_collection引用关系Detail.category,它不引用mapper Mapper | Category | kitchen_category

但是,如果我将模型kitchen.Category重命名为kitchen.KitchenCategory:

from kitchen import KitchenCategory
class Detail(Base):
    __tablename__ = 'menu_productdetail'

    category_id = Column(ForeignKey('menu_category.id'))
    category = relationship(Category, backref='details')
    brands = relationship("Brand", secondary=brand_product_detail_association_table)

    kitchen_category_id = Column(ForeignKey('kitchen_category.id'))
    kitchen_category = relationship(KitchenCategory, backref='details')

然后它运作良好。我做错了什么?

0 个答案:

没有答案