title: Using bulk_insert_mappings
body: <p>I am trying to do a bulk insert of a large list of dictionaries of the form:</p>
  
  <pre><code>results = [{'attribute': u'SEX', 'value_d': 0.0, 'value_s': u'M', 'sid': 1L}, 
             {'attribute': u'SEX', 'value_d': 0.0, 'value_s': u'M', 'sid': 2L}, 
             {'attribute': u'SEX', 'value_d': 0.0, 'value_s': u'M', 'sid': 3L},     
             {'attribute': u'SEX', 'value_d': 0.0, 'value_s': u'M', 'sid': 4L},
             ...
  ]
  </code></pre>
  
  <p>After reading about 'executemany' and bulk_insert_mappings, I decided to try the later, since it looked much simpler to work with.</p>
  
  <p>Here are the 3 lines of code to execute this, using the naive assumption that since I have a list of dictionaries this would work out of the box:</p>
  
  <pre><code>Session = sessionmaker(bind=engine)
  s = Session()
  s.bulk_insert_mappings(Results,results)
  </code></pre>
  
  <p>My Results model is:</p>
  
  <pre><code>class Results(db.Model):
      __tablename__ = 'results'
      id = Column(Integer, primary_key=True, autoincrement=True)
      sid = Column(Integer)
      attribute = Column(String(2048))
      value_s = Column(String(2048))
      value_d = Column(Float)
  </code></pre>
  
  <p>No errors are thrown when I run this, but the data are not being inserted.</p>
  
  <p>Not sure where to go with this now...</p>
  
  <p>** EDIT **</p>
  
  <p>As requested, the imports for this were:</p>
  
  <p>In module where call was made:</p>
  
  <pre><code>os.environ['MPLCONFIGDIR'] = tempfile.mkdtemp()
  import pandas as pd
  from sqlalchemy.sql import label, distinct
  from sqlalchemy.orm import joinedload_all
  from .app import s, e
  </code></pre>
  
  <p>In flask app.py module:</p>
  
  <pre><code>import os
  from flask import Flask
  from flask.ext.sqlalchemy import SQLAlchemy
  from sqlalchemy import *
  from sqlalchemy.orm import *
  from sqlalchemy.ext.declarative import declarative_base
  
  
  # Define Flask app
  app = Flask(__name__)
  # grab main config from Flask-Appbuilder
  app.config.from_pyfile('/Library/WebServer/wsgi/rest_api/cardioCatalogue/config.py')
  
  from flask import current_app
  
  URI = current_app.config['SQLALCHEMY_DATABASE_URI']
  
  from threading import Lock
  
  # Flask declarative base: defifne tables/models simultaneously
  e = create_engine(URI, echo=True)
  s = Session(e)
  
  db_session = scoped_session(sessionmaker(autocommit=False,
                                           autoflush=False,
                                           bind=e))
  Base = declarative_base()
  Base.query = db_session.query_property()
  Base.metadata.create_all(e)
  </code></pre>