sqlalchemy类中一个表的两个主键

时间:2018-10-26 02:12:19

标签: python sqlalchemy foreign-keys

我正在尝试使用外键将一个表中的两列链接到另一表中的两列。表recipe_ingredientsingredients一对多

from sqlalchemy import Column, Integer, Text, ForeignKey, Float, Boolean
from sqlalchemy.orm import relationship

from barlibrary.models.meta import Base

class RecipeIngredient(Base):
    __tablename__ = 'recipe_ingredients'
    id = Column(Integer, primary_key=True)
    ingredient_id = Column(Integer, ForeignKey('ingredients.id'))
    ingredient_name = Column(Text, ForeignKey('ingredients.id'))

    ingredient = relationship('Ingredient', back_populates='recipe_ingredient')

class Ingredient(Base):
    __tablename__ = 'ingredients'
    id = Column(Integer, primary_key=True)
    name = Column(Text, unique=True)

    recipe_ingredient = relationship('RecipeIngredient', back_populates='ingredient')

但是我得到了错误

sqlalchemy.exc.AmbiguousForeignKeysError: Could not determine join 
condition between parent/child tables on relationship 
Ingredient.recipe_ingredient - there are multiple foreign key paths linking 
the tables.  Specify the 'foreign_keys' argument, providing a list of those 
columns which should be counted as containing a foreign key reference to 
the parent table.

我相信这仅仅是因为我需要指定在链接ID的同一行中将recipe_ingredients.name设置为ingredients.name。我对SQL或sqlalchemy不太熟悉,无法确切地知道如何指定这种关系

0 个答案:

没有答案