title: How to query Many-To-Many SQLAlchemy
body: <p><a href="https://i.stack.imgur.com/P6LZu.png" rel="noreferrer"><img src="https://i.stack.imgur.com/P6LZu.png" alt="enter image description here"></a></p>
  
  <p>Import <code>Flask</code> and <code>SQLAlchemy</code> modules first:</p>
  
  <pre><code>from flask import Flask
  from flask_sqlalchemy import SQLAlchemy
  </code></pre>
  
  <p>Declare the <code>app</code> and <code>db</code> objects:</p>
  
  <pre><code>app = Flask(__name__)
  app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///inquestion.db' 
  db = SQLAlchemy(app)
  </code></pre>
  
  <p>There are three tables: <code>Artist</code>, <code>Album</code> and <code>Genre</code>. The <code>Artist</code> object can be linked to multiple <code>Albums</code>. And the <code>Album</code> object can be linked to multiple <code>Artists</code>. The <code>albums_to_artists_table</code> is to keep the relationship between the <code>Artists</code> and <code>Albums</code> tight:</p>
  
  <pre><code>albums_to_artists_table = db.Table('albums_to_artists_table',
                            db.Column('album_id', db.Integer, db.ForeignKey('album.id')),
                            db.Column('artist_id', db.Integer, db.ForeignKey('artist.id')))
  
  class Genre(db.Model):
      id = db.Column(db.Integer, primary_key=True)
      name = db.Column(db.String(80), unique=True)
  
  
  class Album(db.Model):
      id = db.Column(db.Integer, primary_key=True)
      name = db.Column(db.String(80), unique=True)
      genre_id = db.Column(db.Integer, db.ForeignKey('genre.id'))
  
      artists = db.relationship('Artist', backref='albums', lazy='dynamic', secondary=albums_to_artists_table)
  
  class Artist(db.Model):
      id = db.Column(db.Integer, primary_key=True)
      name = db.Column(db.String(80), unique=True)
      _albums = db.relationship('Album', secondary=albums_to_artists_table, backref=db.backref('albums_to_artists_table_backref', lazy='dynamic')) 
  </code></pre>
  
  <p>So we have the <code>Artist</code> linked to the <code>Album</code> which is linked to <code>Genre</code> and it looks like this: <code>Artist</code> > <code>Album</code> > <code>Genre</code>.</p>
  
  <p>Having this setup in place we go ahead and create the <code>Genre</code> object first:</p>
  
  <pre><code>db.drop_all()
  db.create_all()
  
  genre = Genre(name='Heavy Metal')
  db.session.add(genre)
  db.session.commit()
  </code></pre>
  
  <p>Then two albums:</p>
  
  <pre><code>album1 = Album(name='Ride the Lightning', genre_id = genre.id)
  album2 = Album(name='Master of Puppets ', genre_id = genre.id)
  db.session.add(album1)
  db.session.add(album2)
  db.session.commit()
  </code></pre>
  
  <p>And the artist:</p>
  
  <pre><code>artist = Artist(name='Metallica',  _albums=[album1, album2])
  
  db.session.add(artist)
  db.session.commit()
  </code></pre>
  
  <p>After the database created we can query what <code>Albums</code> are linked to <code>Genre</code>:</p>
  
  <pre><code>print Album.query.filter_by(genre_id=1).all()
  </code></pre>
  
  <p>and what <code>Artists</code> are linked to <code>Album</code>:</p>
  
  <pre><code>print Artist.query.filter(Artist._albums.any(id=album1.id)).all()
  </code></pre>
  
  <p>Now I would like to query all the <code>Artists</code> that are linked to a <code>Genre</code> passing the genre.id. How to achieve it?</p>