Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask-SQLAlchemy not creating tables using create_all()

I have a Flask app that is using Flask-SQLAlchemy. In my unit tests, I initialise the app and the DB, then call db.create_all() and for some reason it looks like it's not picking up any of my models so isn't creating any tables.

I'm using both __tablename__ and __bind_key__ in my models as I have two databases.

My config:

SQLALCHEMY_DATABASE_URI = 'sqlite://'

SQLALCHEMY_BINDS = {
    'db1': SQLALCHEMY_DATABASE_URI,
    'db2': SQLALCHEMY_DATABASE_URI
}

Cut down version of my setUp() method in my unit tests:

from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy

class APITestCase(unittest.TestCase):

    app = Flask(__name__)
    db = SQLAlchemy(app)
    db.create_all()

Here's an example of two of my models, one from each bind type:

class Contact(db.Model):

    __tablename__ = 'contact'
    __bind_key__ = 'db1'

    id = db.Column(db.Integer, primary_key=True)
    first_name = db.Column(db.String(255))
    last_name = db.Column(db.String(255))
    ...

    def __init__(first_name, last_name):
        self.first_name = first_name
        self.last_name = last_name
        ...

class BaseUser(db.Model):

    __tablename__ = 'User__GeneralUser'
    __bind_key__ = 'db2'

    id = db.Column(db.Integer, primary_key=True)
    username = db.Column('Username', db.String(100))
    first_name = db.Column('FirstName', db.String(100))
    last_name = db.Column('Surname', db.String(100))
    ...

    def __init__(username, first_name, last_name):
        self.username = username
        self.first_name = first_name
        self.last_name = last_name
        ...

Have I missed something glaringly obvious? How does Flask-SQLAlchemy know where to look for my models to create the associated tables?

like image 362
Chris McKinnel Avatar asked Feb 25 '13 14:02

Chris McKinnel


People also ask

How do you create a table using SQLAlchemy in Flask?

Step 1 - Install the Flask-SQLAlchemy extension. Step 2 - You need to import the SQLAlchemy class from this module. Step 3 - Now create a Flask application object and set the URI for the database to use. Step 4 - then use the application object as a parameter to create an object of class SQLAlchemy.

What is the difference between SQLAlchemy and flask-SQLAlchemy?

One of which is that Flask-SQLAlchemy has its own API. This adds complexity by having its different methods for ORM queries and models separate from the SQLAlchemy API. Another disadvantage is that Flask-SQLAlchemy makes using the database outside of a Flask context difficult.


1 Answers

In your unit tests you shouldn't create a new instance of the SQLAlchemy object ('db'), you should import the instance from which your models descend:

models.py:

from flask.ext.sqlalchemy import SQLAlchemy
db = SQLAlchemy()

class Contact(db.Model):
    ...

tests.py:

from models import db
from flask import Flask
import unittest

class TestExample(unittest.TestCase):

    def setUp(self):
        self.app = Flask(__name__)
        db.init_app(self.app)
        with self.app.app_context():
            db.create_all()
like image 134
DazWorrall Avatar answered Nov 14 '22 23:11

DazWorrall