title: Flask inherited classes of tables in multiple identical databases using __bind_key__
body: <p>I'm trying to build an admin control panel that brings together 4 different ecommerce sites. The sites all have identical database structures (all MySQL).</p>
  
  <h1>What's Going Wrong?</h1>
  
  <p>I get 404 Not Found on any order ID and site I put in. No matter how I mix it I can not get any record to come up. Always a 404 and I have no idea why. SO here I am.</p>
  
  <h1>Code</h1>
  
  <p>I tried to do this by creating base model classes of every table. Then creating inherited clases of those base classes with a different bind key dependent on the DB it is meant for. This is a summarised view of the code - if you'd need anymore than this let me know:</p>
  
  <h2>basemodels.py</h2>
  
  <pre><code>MyOrderClass(db.Model):
      __tablename__ = 'messytablename'
      id = db.Column('order_id', db.Integer, primary_key=True)
      order_total = db.Column(db.Float)
      order_status = db.Column(db.String(1))
  </code></pre>
  
  <h2>site2models.py</h2>
  
  <pre><code>class Site2Order(MyOrderClass):
      __bind_key__ = 'site2'
  </code></pre>
  
  <h2>__init__.py</h2>
  
  <pre><code>app = Flask(__name__)
  app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://user:pass@localhost/site'
  app.config['SQLALCHEMY_BINDS'] = {
      'site1':'mysql://user:pass@localhost/site1',
      'site2':'mysql://user:pass@localhost/site2',
      'site3':'mysql://user:pass@localhost/site3',
      'site4':'mysql://user:pass@localhost/site4'
  }
  </code></pre>
  
  <h2>views.py</h2>
  
  <pre><code>@app.route('/order/&lt;site&gt;/&lt;orderid&gt;')
  def show_order(site, orderid):
      if site == 'site1':
          orderObject = Site1Order
      if site == 'site2':
          orderObject = Site2Order
      if site == 'site3':
          orderObject = Site3Order
      if site == 'site4':
          orderObject = Site4Order
  
      order = orderObject.query.get(orderid)
      return render_template('order.html', order=order)
  </code></pre>
  
  <p>The original sites are built in PHP and have less than tidy structures and naming conventions. </p>
  
  <p>Thank you for your time.</p>