title: sqlalchemy how to bind metadata to the table
body: <p>I have tried the solution mentioned in <a href="https://stackoverflow.com/questions/36744650/sqlalchemy-create-tables">sqlalchemy create tables</a> and also referred to flask documentation <a href="http://flask-sqlalchemy.pocoo.org/2.1/contexts/" rel="nofollow noreferrer">http://flask-sqlalchemy.pocoo.org/2.1/contexts/</a>. I am facing problems creating the database tables using create_all(). Here is my code.</p>
  
  <pre><code>&gt;&gt;&gt; from flask import Flask
  &gt;&gt;&gt; app = Flask('myflaskapp')
  &gt;&gt;&gt; from flask_sqlalchemy import SQLAlchemy
  &gt;&gt;&gt; app.url_map.strict_slashes = False
  &gt;&gt;&gt; app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql+psycopg2://my_dbuser:my_dbuser_password@localhost/my_dev_db'
  &gt;&gt;&gt; app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
  &gt;&gt;&gt; from myflaskapp.models import AdRequest, AdResult
  &gt;&gt;&gt; AdRequest.__table__
  Table('ad_request', MetaData(bind=None), Column('id', Integer(), table=&lt;ad_request&gt;, primary_key=True, nullable=False), Column('status', String(length=10), table=&lt;ad_request&gt;, default=ColumnDefault('NEW')), Column('status_msg', String(length=100), table=&lt;ad_request&gt;, default=ColumnDefault('')), Column('query_str', String(length=100), table=&lt;ad_request&gt;), schema=None)
  &gt;&gt;&gt; db = SQLAlchemy(app)
  &gt;&gt;&gt; db.engine
  Engine(postgresql+psycopg2://my_dbuser:***@localhost/my_dev_db)
  &gt;&gt;&gt; db.metadata.create_all(db.engine)
  &gt;&gt;&gt; db.engine
  Engine(postgresql+psycopg2://my_dbuser:***@localhost/my_dev_db)
  &gt;&gt;&gt; db
  &lt;SQLAlchemy engine=postgresql+psycopg2://my_dbuser:***@localhost/my_dev_db&gt;
  &gt;&gt;&gt; db.session.commit()
  </code></pre>
  
  <p>On the postgres console a "\dt" does not list the new table ad_request that should get created as a result of running create_all.</p>
  
  <p>Furthermore, I added debug as follows app['SQLALCHEMY_ECHO']=True and I get the following output which I cannot decipher.</p>
  
  <pre><code>&gt;&gt;&gt; app.config['SQLALCHEMY_ECHO']=True
  &gt;&gt;&gt; from models import *
  &gt;&gt;&gt; with app.app_context():
  ...   db.create_all()
  ... 
  2017-07-17 10:24:09,766 INFO sqlalchemy.engine.base.Engine select relname from pg_class c join pg_namespace n on n.oid=c.relnamespace where pg_catalog.pg_table_is_visible(c.oid) and relname=%(name)s
  2017-07-17 10:24:09,766 INFO sqlalchemy.engine.base.Engine {'name': 'ad_request'}
  2017-07-17 10:24:09,768 INFO sqlalchemy.engine.base.Engine select relname from pg_class c join pg_namespace n on n.oid=c.relnamespace where pg_catalog.pg_table_is_visible(c.oid) and relname=%(name)s
  2017-07-17 10:24:09,769 INFO sqlalchemy.engine.base.Engine {'name': 'ad_result'}
  2017-07-17 10:24:09,772 INFO sqlalchemy.engine.base.Engine 
  CREATE TABLE ad_request (
      id SERIAL NOT NULL, 
      status VARCHAR(10), 
      status_msg VARCHAR(100), 
      query_str VARCHAR(100), 
      PRIMARY KEY (id)
  )
  2017-07-17 10:24:09,772 INFO sqlalchemy.engine.base.Engine {}
  2017-07-17 10:24:09,779 INFO sqlalchemy.engine.base.Engine COMMIT
  2017-07-17 10:24:09,780 INFO sqlalchemy.engine.base.Engine 
  CREATE TABLE ad_result (
      id SERIAL NOT NULL, 
      uuid_s VARCHAR(50), 
      score FLOAT, 
      ad_request_id INTEGER, 
      PRIMARY KEY (id), 
      FOREIGN KEY(ad_request_id) REFERENCES ad_request (id)
  )
  2017-07-17 10:24:09,780 INFO sqlalchemy.engine.base.Engine {}
  2017-07-17 10:24:09,784 INFO sqlalchemy.engine.base.Engine COMMIT
  </code></pre>
  
  <p>I noticed that AdRequest.<strong>table</strong> shows Table('ad_request', MetaData(bind=None), ...
  How can I make AdRequest bind to the db.metadata ?
  Following is my models.py</p>
  
  <pre><code># models.py
  from flask_sqlalchemy import SQLAlchemy
  from flask_marshmallow import Marshmallow
  
  
  db = SQLAlchemy()
  ma = Marshmallow()
  
  
  class AdRequest(db.Model):
      id = db.Column(db.Integer, primary_key=True, autoincrement=True)
      status = db.Column(db.String(10), default="NEW")
      status_msg = db.Column(db.String(100), default="")
      query_str = db.Column(db.String(100))
  
      def __init__(self, query_str):
          self.query_str = query_str
  
      def __repr__(self):
          return '&lt;AdRequest %r, %r, %r, %r&gt;' % (self.id, self.query_str, self.status, self.status_msg)
  
      def as_dict(self):
          return {'id': self.id,
                  'query_str': self.query_str,
                  'status': self.status,
                  'status_msg': self.status_msg,
                  }
  
  
  class AdResult(db.Model):
      id = db.Column(db.Integer, primary_key=True)
      uuid_s = db.Column(db.String(50))
      score = db.Column(db.Float)
      ad_request_id = db.Column(db.Integer, db.ForeignKey('ad_request.id'))
  
      def __init__(self, uuid_s, score, ad_request_id):
          self.uuid_s = uuid_s
          self.score = score
          self.ad_request_id = ad_request_id
  
      def __repr__(self):
          return '&lt;AdResult %r, %r, %s&gt;' % (self.id, self.uuid_s, self.ad_request_id)
  
  
  class AdRequestSchema(ma.Schema):
      class Meta:
          # Fields to expose
          fields = ('id', 'status', 'status_msg', 'query_str')
  </code></pre>