Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8590227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:14:51+00:00 2026-06-11T23:14:51+00:00

I need help creating SqlAlchemy query. I’m doing a Flask project where I’m using

  • 0

I need help creating SqlAlchemy query.

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.

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:

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 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T23:14:52+00:00Added an answer on June 11, 2026 at 11:14 pm

    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
                )
        )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need help in creating a query for my database. I have 2 tables,
Need help with a query that I wrote: I have three tables Company id
I need some help creating a query for my mySQL database. I have recently
I have an advanced query/report that I need help creating in Access 2007. The
I need help creating the best possible regular expression for this problem. I have
I need help in creating an query interface with access database. In brief, with
I need help creating an SQL query to count rows broken out on two
I have a pretty basic blog app on rails, and I need help creating
I need help in creating a xml file from my php code. I created
I need some help creating a query. Heres a sample table: Segment Name 1

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.