[英]Joining two tables with one table having a different foreign key
I'm attempting to join two tables together, however, I keep receving the errors of: 我试图将两个表连接在一起,但是,我不断收到以下错误:
sqlalchemy.exc.InvalidRequestError: Could not find a FROM clause to join from. sqlalchemy.exc.InvalidRequestError:找不到要从中加入的FROM子句。 Tried joining to , but got: Can't find any foreign key relationships between 'recipe' and 'ingredient'.
尝试加入,但得到:在“配方”和“成分”之间找不到任何外键关系。
and 和
sqlalchemy.exc.NoForeignKeysError: Can't find any foreign key relationships between 'recipe' and 'ingredient'. sqlalchemy.exc.NoForeignKeysError:找不到“配方”和“成分”之间的任何外键关系。
class Recipe(db.Model):
query_class = RecipeQuery
__tablename__ = 'recipe'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.Text)
description = db.Column(db.Text)
directions = db.Column(db.Text)
prep_time = db.Column(db.String(15))
cook_time = db.Column(db.String(15))
image = db.Column(db.Text)
ingredients = db.relationship('Ingredient', secondary=ingredients)
credit = db.Column(db.String)
search_vector = db.Column(TSVectorType('name', 'description', 'directions'))
class Ingredient(db.Model):
query_class = IngredientQuery
__tablename__ = 'ingredient'
id = db.Column(db.Integer, primary_key=True)
original = db.Column(db.Text)
name = db.Column(db.Integer, db.ForeignKey('ingredient_name.id'))
amount = db.Column(db.String(10))
unit = db.Column(db.String(20))
modifiers = db.Column(db.Text)
search_vector = db.Column(TSVectorType('original'))
ingredients = db.Table('ingredients',
db.Column('recipe', db.Integer, db.ForeignKey('recipe.id')),
db.Column('ingredient', db.Integer, db.ForeignKey('ingredient.id'))
)
I've tried selecting the items three different ways, all fail with the same error. 我尝试以三种不同的方式选择项目,但均失败,并出现相同的错误。
try1 = db.session.query(models.Recipe).join(models.Ingredient, secondary=ingredients)
try2 = db.session.query(models.Recipe).join(models.Ingredient)
try3 = db.session.query(models.Recipe).join('ingredients')
It looks similar to the example given for a many-to-many relationship given at http://docs.sqlalchemy.org/en/rel_0_9/orm/relationships.html The only real difference being that Ingredient has a foreign key, which might be throwing it off? 它看起来类似于在http://docs.sqlalchemy.org/en/rel_0_9/orm/relationships.html中给出的多对多关系示例。唯一的真正不同是Ingredients具有外键,这可能被扔掉吗? If so, I'm still not sure how to fix that issue.
如果是这样,我仍然不确定如何解决该问题。
Thanks 谢谢
Does one of these work if you explicitly specify the relationship in the join? 如果您在联接中明确指定关系,这些方法之一是否起作用?
db.session.query(models.Recipe).\
join(models.Ingredient, models.Recipe.ingredients)
db.session.query(models.Recipe).\
join(models.Recipe.ingredients)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.