title: python - Flask-sqlalchemy: sqlalchemy.exc.InvalidRequestError
body: <p>I'm building a backend for my iOS app and I choose to use Flask framework.</p>
  
  <p>For the database part, I use <code>flask-sqlalchemy</code> to connect my <code>mysql</code> database.</p>
  
  <p>When I put all my test code in a <strong>single</strong> file, everything works fine.</p>
  
  <p><strong>test.py</strong></p>
  
  <pre><code>from flask import Flask
  from flask.ext.sqlalchemy import SQLAlchemy
  
  app = Flask(__name__)
  app.config['SQLALCHEMY_DATABASE_URI'] = "mysql://root:pwd@localhost/test"
  db = SQLAlchemy(app)
  
  
  class player(db.Model):
      id = db.Column(db.Integer, primary_key=True)
      username = db.Column(db.String(80), unique=True)
      email = db.Column(db.String(120), unique=True)
  
      def __init__(self, username, email):
          self.username = username
          self.email = email
  
      def __repr__(self):
          return '&lt;User %r&gt;' % self.username
  
  db.create_all()
  </code></pre>
  
  <p>However, when I split the code into different files in  order to make the whole project structured, I got an error.</p>
  
  <p>Here's my file structure:</p>
  
  <pre><code>.
  ├── config.py
  ├── app.py
  ├── app
      ├── __init__.py
      ├── api.py
      └── models.py
  └── venv
  </code></pre>
  
  <p><strong>config.py</strong></p>
  
  <pre><code>SQLALCHEMY_DATABASE_URI = "mysql://root:pwd@localhost/test"
  </code></pre>
  
  <p><strong>app.py</strong></p>
  
  <pre><code>from app import app
  
  if __name__ == '__main__':
      app.run(debug=True)
  </code></pre>
  
  <p><strong>__init.py__</strong></p>
  
  <pre><code>from flask import Flask
  from flask_sqlalchemy import SQLAlchemy
  
  import config
  
  app = Flask(__name__)
  app.config.from_object("config")
  db = SQLAlchemy(app)
  
  import models
  db.create_all()
  </code></pre>
  
  <p><strong>models.py</strong></p>
  
  <pre><code>from app import db
  
  class player(db.Model):
      id = db.Column(db.Integer, primary_key=True)
      username = db.Column(db.String(80), unique=True)
      email = db.Column(db.String(120), unique=True)
  
      def __init__(self, username, email):
          self.username = username
          self.email = email
  
      def __repr__(self):
          return '&lt;User %r&gt;' % self.username
  </code></pre>
  
  <p>When I ran <code>__init__.py</code> to create all the tables in <code>models.py</code>, I got error</p>
  
  <blockquote>
    <p>sqlalchemy.exc.InvalidRequestError: Table 'player' is already defined for this MetaData instance.  Specify 'extend_existing=True' to redefine options and columns on an existing Table object.</p>
  </blockquote>
  
  <p><strong>And when I check the database, I found that table <code>player</code> is created.</strong></p>
  
  <p>It seems that sqlalchemy is trying to create the tables repeatedly. </p>
  
  <p>Why is that?</p>