title: How to get a SQL Alchemy object of distinct items with filter on related objects
body: <p>I am trying to get a SQL Alchemy query of distinct items below filtering on related objects, the equivalent of the below query:</p>
  
  <pre><code>SELECT distinct items.item_id, items.item_name
  FROM items
  INNER JOIN categories as cat on items.category_id = cat.category_id
  INNER JOIN stores on cat.store_id = stores.store_id
  WHERE store.store_id = 123    
  </code></pre>
  
  <p>I have created the models as below with foreign keys included but when I run the query below it does not filter correctly.   </p>
  
  <pre><code>items_query = (db.session.query(Store, Item)
                 .filter(Store.store_id == 123)
                 ).all()
  
  
  
  #SQL Alchemy Models 
  class Store(db.Model):
      __tablename__ = 'stores'
      store_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
      store_name = db.Column(db.String(100), unique=True, nullable=False)
  
      def __repr__(self):
          return '&lt;Store&gt;'+str(self.store_name)
  
  class Category(db.Model):
      __tablename__ = 'categories'
      category_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
      category_name = db.Column(db.String(100), unique=True, nullable=False)
      store_id = db.Column(db.Integer, db.ForeignKey('stores.store_id'))
      store = db.relationship('Store', backref=db.backref('categories', lazy='dynamic'))
  
      def __repr__(self):
          return '&lt;Category&gt;'+str(self.category_name)
  
  class Item(db.Model):
      __tablename__ = 'items'
      item_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
      item_name = db.Column(db.String(150), unique=True, nullable=False)
      category_id = db.Column(db.Integer, db.ForeignKey('categories.category_id'))
      category = db.relationship('Category', backref=db.backref('items', lazy='dynamic'))
  
      def __repr__(self):
          return '&lt;Item&gt;'+str(self.item_name)
  </code></pre>
  
  <p>Could anyone assist me to form the query better?</p>