title: Flask-Sqlalchemy, multiple databases with bind and foreign keys
body: <p>I have set up an application using <code>flask</code>, and a few models using <code>Flask-SQLAlchemy</code>. I want to store a few JSON-objects into a separate database, as this database will not be frequently read, only written to. Other data is stored in the main-database. </p>
  
  <p>This works fine, except I would like to set up a relationsship between a table in the JSON-database, and the main-database. If I keep the tables in the same database, this works great(by commenting out <code>__bind_key__ = 'forms'</code>), but as I said, I would like to have the databases separate.</p>
  
  <p>I've tried looking at various solutions to this, among other related posts here on StackOverflow. I am clearly doing something wrong.</p>
  
  <p>Currently i get this error, when running <code>python models.py</code>: </p>
  
  <pre><code>(psycopg2.ProgrammingError) relation "filled_form_data" does not exist
   sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) relation "filled_form_data" does not exist
  [SQL: '\nCREATE TABLE filled_form_modified    (\n\tid SERIAL NOT NULL, \n\tdate TIMESTAMP WITHOUT TIME ZONE, \n\tfilled_form_data_id INTEGER, \n\tPRIMARY KEY (id), \n\tUNIQUE (id), \n\tFOREIGN KEY(filled_form_data_id) REFERENCES filled_form_data (id)\n)\n\n']
  </code></pre>
  
  <p>I'm thinking I would need to specify the database somehow in there also, but I though <code>sqlalchemy</code> would do that for me. I am a bit confused by the documantation-page here: <a href="http://flask-sqlalchemy.pocoo.org/2.1/binds/" rel="nofollow noreferrer">http://flask-sqlalchemy.pocoo.org/2.1/binds/</a></p>
  
  <p>I would really appreciate your help on this.</p>
  
  <h1>models.py</h1>
  
  <pre><code>from datetime import datetime
  from flask_sqlalchemy import SQLAlchemy
  from flask import Flask
  db = SQLAlchemy()
  
  
  
  class FilledFormData(db.Model):
      """Table of json-data for forms."""
      __bind_key__ = 'forms'
      __tablename__ = 'filled_form_data'
      id = db.Column(db.Integer, primary_key=True, unique=True)
      request_form = db.Column(db.JSON)  # All data from the users-form
      form_data = db.Column(db.JSON)  # All data actually used to fill the pdf.
  
  
  class FilledFormModified(db.Model):
      """Table of modification-dated for FilledForm-model."""
      id = db.Column(db.Integer, primary_key=True, unique=True)
      date = db.Column(db.DateTime, default=datetime.utcnow)
      filled_form_data_id = db.Column(db.Integer, db.ForeignKey(FilledFormData.id))
      filled_form_data = db.relationship(
          FilledFormData, primaryjoin='FilledFormModified.filled_form_data_id==FilledFormData.id')  # noqa
      info = {'bind_key': 'forms'}
  
  
  
  
  app = Flask(__name__)
  app.config['SQLALCHEMY_DATABASE_URI'] = 'postgres:///varmekabler'
  app.config['SQLALCHEMY_BINDS'] = {
      'products': 'postgres:///vk_products',
      'forms': 'postgres:///vk_forms'
  }
  db.init_app(app)
  with app.app_context():
  
      db.drop_all()
      db.create_all()
  </code></pre>