2010-05-13 132 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]