我正在尝试使用外键将一个表中的两列链接到另一表中的两列。表recipe_ingredients
与ingredients
一对多
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
不太熟悉,无法确切地知道如何指定这种关系