![](/img/trans.png)
[英]Joining two tables (from different databases) based on whether lat and long from one table are present in the polygon of the other
[英]Joining two tables with one table having a different foreign key
我试图将两个表连接在一起,但是,我不断收到以下错误:
sqlalchemy.exc.InvalidRequestError:找不到要从中加入的FROM子句。 尝试加入,但得到:在“配方”和“成分”之间找不到任何外键关系。
和
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'))
)
我尝试以三种不同的方式选择项目,但均失败,并出现相同的错误。
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')
它看起来类似于在http://docs.sqlalchemy.org/en/rel_0_9/orm/relationships.html中给出的多对多关系示例。唯一的真正不同是Ingredients具有外键,这可能被扔掉吗? 如果是这样,我仍然不确定如何解决该问题。
谢谢
如果您在联接中明确指定关系,这些方法之一是否起作用?
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.