SqlAlchemy和Flask,如何查询多对多关系

时间:2022-02-16 20:14:09

I need help creating SqlAlchemy query.

我需要帮助创建SqlAlchemy查询。

I'm doing a Flask project where I'm using SqlAlchemy. I have created 3 tables: Restaurant, Dish and restaurant_dish in my models.py file.

我正在做一个Flask项目,我正在使用SqlAlchemy。我在models.py文件中创建了3个表:Restaurant,Dish和restaurant_dish。

restaurant_dish = db.Table('restaurant_dish',
    db.Column('dish_id', db.Integer, db.ForeignKey('dish.id')),
    db.Column('restaurant_id', db.Integer, db.ForeignKey('restaurant.id'))
)

class Restaurant(db.Model):
    id = db.Column(db.Integer, primary_key = True)
    name = db.Column(db.String(64), index = True)

    restaurant_dish = db.relationship('Dish', secondary=restaurant_dish,
        backref=db.backref('dishes', lazy='dynamic'))


class Dish(db.Model):
    id = db.Column(db.Integer, primary_key = True)
    name = db.Column(db.String(64), index = True)
    info = db.Column(db.String(256), index = True)

I have added data to the restaurant_dish table and it should be working correctly. Where I need help is understanding how to correctly get a Dish using Restaurant. Raw SQL would be something like this:

我已将数据添加到restaurant_dish表中,它应该正常工作。我需要帮助的地方是了解如何使用餐厅正确获取菜肴。原始SQL将是这样的:

SELECT dish_id FROM restaurant_dish WHERE restaurant_id == id

What I have managed to get done but not working:

我设法完成但没有工作:

x = Restaurant.query.filter_by(Restaurant.restaurant_dish.contains(name)).all()

Thanks for help and I also appreciate tutorials that can point me in the right direction(the official documentation goes over my head).

感谢您的帮助,我也非常感谢教程可以指出我正确的方向(官方文档在我的头上)。

1 个解决方案

#1


45  

The semantic of the relationship doesn't look right. I think it should be something like:

这种关系的语义看起来不正确。我认为应该是这样的:

class Restaurant(db.Model):
    ...

    dishes = db.relationship('Dish', secondary=restaurant_dish,
        backref=db.backref('restaurants'))

Then, to retrieve all the dishes for a restaurant, you can do:

然后,要检索餐馆的所有菜肴,您可以:

x = Dish.query.filter(Dish.restaurants.any(name=name)).all()

This should generate a query like:

这应该生成如下查询:

SELECT dish.*
FROM dish
WHERE
    EXISTS (
        SELECT 1
        FROM restaurant_dish
        WHERE
            dish.id = restaurant_dish.dish_id
            AND EXISTS (
                SELECT 1
                FROM restaurant
                WHERE
                    restaurant_dish.restaurant_id = restaurant.id
                    AND restaurant.name = :name
            )
    )

#1


45  

The semantic of the relationship doesn't look right. I think it should be something like:

这种关系的语义看起来不正确。我认为应该是这样的:

class Restaurant(db.Model):
    ...

    dishes = db.relationship('Dish', secondary=restaurant_dish,
        backref=db.backref('restaurants'))

Then, to retrieve all the dishes for a restaurant, you can do:

然后,要检索餐馆的所有菜肴,您可以:

x = Dish.query.filter(Dish.restaurants.any(name=name)).all()

This should generate a query like:

这应该生成如下查询:

SELECT dish.*
FROM dish
WHERE
    EXISTS (
        SELECT 1
        FROM restaurant_dish
        WHERE
            dish.id = restaurant_dish.dish_id
            AND EXISTS (
                SELECT 1
                FROM restaurant
                WHERE
                    restaurant_dish.restaurant_id = restaurant.id
                    AND restaurant.name = :name
            )
    )