[英]Flask-SQLAlchemy not creating tables using create_all()
I have a Flask app that is using Flask-SQLAlchemy. 我有一个使用Flask-SQLAlchemy的Flask应用程序。 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. 在我的单元测试中,我初始化应用程序和数据库,然后调用db.create_all()
,由于某种原因,它看起来没有拿起我的任何模型,所以不创建任何表。
I'm using both __tablename__
and __bind_key__
in my models as I have two databases. 我在我的模型中同时使用了__tablename__
和__bind_key__
,因为我有两个数据库。
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: 在我的单元测试中减少我的setUp()
方法的版本:
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? Flask-SQLAlchemy如何知道在哪里查找我的模型来创建关联表?
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: 在单元测试中,您不应该创建SQLAlchemy对象('db')的新实例,您应该导入模型所从的实例:
models.py: models.py:
from flask.ext.sqlalchemy import SQLAlchemy
db = SQLAlchemy()
class Contact(db.Model):
...
tests.py: 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()
your SQLALCHEMY_DATABASE_URI is wrong, read here how to configuration your engine Sqlite database http://docs.sqlalchemy.org/en/rel_0_9/core/engines.html#sqlite 您的SQLALCHEMY_DATABASE_URI错误,请在此处阅读如何配置您的引擎Sqlite数据库http://docs.sqlalchemy.org/en/rel_0_9/core/engines.html#sqlite
if you never experience, see this repo and apply to your apps :) https://github.com/Fird0s/Banda-Maps/blob/master/models.py 如果您从未体验过,请参阅此回购并应用于您的应用程序:) https://github.com/Fird0s/Banda-Maps/blob/master/models.py
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.