title: SqlAlchemy and Flask, how to query many-to-many relationship
body: <p>I need help creating SqlAlchemy query.</p>
  
  <p>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.</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>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: </p>
  
  <pre><code>SELECT dish_id FROM restaurant_dish WHERE restaurant_id == id
  </code></pre>
  
  <p>What I have managed to get done but not working:</p>
  
  <pre><code>x = Restaurant.query.filter_by(Restaurant.restaurant_dish.contains(name)).all()
  </code></pre>
  
  <p>Thanks for help and I also appreciate tutorials that can point me in the right direction(the official documentation goes over my head).</p>