简体   繁体   English

连接两个表,其中一个表具有不同的外键

[英]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.

相关问题 根据一张表中的纬度和经度是否存在于另一张表的多边形中来连接两张表(来自不同的数据库) - Joining two tables (from different databases) based on whether lat and long from one table are present in the polygon of the other 如何建立一个具有不同模型的两个外键关系的模型 - How to make a model having two foreign Key relations with the different models Django - 根据其内容引用两个表之一的外键 - Django - Foreign key referencing one of two tables depending on its content 从一个表到不同表的 Peewee 外键 - Peewee foreign key from one table to different tabels 使用Django连接和遍历外键表 - Joining and iterating through foreign-key tables with Django 如何在一个对象中获取键值对,同时在 Python 中的 SQLAlchemy 中连接两个表? - How to get key-value pair in one object while joining two tables in SQLAlchemy in Python? 如何根据存在于具有外键关系的不同表中的字段从表中获取行? - How to fetch rows from a table based on a field which is present in a different table having a foreign key relationship with it? 在 sqlite3 中引用(外键)表的一列通过连接其他两个表中的两列 - In sqlite3 reference(foreign key) a column of a table by join of two columns in other two tables 两个带有外键的表引用另一个NameError - Two tables with foreign key referencing the other, NameError 通过外键过滤 Django 中的联接表中的数据 - Filtering data from joining table in Django by foreign key
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM