2010-05-13 103 views
1

亲爱的大家,我下面就http://www.sqlalchemy.org/docs/mappers.html#many-to-many的SQLAlchemy:多对多的关系错误

#This is actually a VIEW 
tb_mapping_uGroups_uProducts = Table('mapping_uGroups_uProducts', metadata, 
    Column('upID', Integer, ForeignKey('uProductsInfo.upID')), 
    Column('ugID', Integer, ForeignKey('uGroupsInfo.ugID')) 
) 

tb_uProducts = Table('uProductsInfo', metadata, 
    Column('upID', Integer, primary_key=True) 
) 
mapper(UnifiedProduct, tb_uProducts) 

tb_uGroupsInfo = Table('uGroupsInfo', metadata, 
    Column('ugID', Integer, primary_key=True) 
) 
mapper(UnifiedGroup, tb_uGroupsInfo, properties={ 
    'unifiedProducts': relation(UnifiedProduct, secondary=tb_mapping_uGroups_uProducts, backref="unifiedGroups") 
}) 

其中uProduct和uGroup之间的关系是N描述的多对多的关系:M。

当我运行以下

sess.query(UnifiedProduct).join(UnifiedGroup).distinct()[:10] 

我收到错误:

sqlalchemy.exc.ArgumentError: Can't find any foreign key relationships between 'uProductsInfo' and 'uGroupsInfo' 

我在做什么错?

编辑:我是上的MyISAM不支持forigen键

回答

2

存在的SQLAlchemy的模式外键定义就足够了,他们不是实际的表是强制性的。您的模型之间没有直接外部关系,所以SQLAlchemy无法找到它们。指定关系明确加入:

sess.query(UnifiedProduct).join(UnifiedProduct.unifiedGroups).distinct()[:10]