flask-SQLAlChemy CREATE_ALL不起作用 [英] Flask-SQLAlchemy create_all doesn't work
本文介绍了flask-SQLAlChemy CREATE_ALL不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在这里看到一些与此类似的问题,但我找不到任何有帮助的问题。我正在尝试使用Flask测试和Flask固定器编写单元测试。自从flask-fixtures在传递给它的db上调用create_all
以来,我遇到了一个问题,在本例中,它实际上并没有创建表。我检查了db.metadata.tables.keys
,其中显示了使用db.Model
创建的所有模型的表
config.py
:
import os
from config import BASE_DIR
# SQLALCHEMY_DATABASE_URI = 'sqlite:///test-db'
SQLALCHEMY_DATABASE_URI = 'sqlite:////Users/XXX/projects/XXX-api/app/inventory/test-db'
testing = True
debug = True
FIXTURES_DIRS = (
os.path.join(BASE_DIR, "inventory/"),
)
执行此操作时表格创建失败:
from flask_api import FlaskAPI
from flask.ext.sqlalchemy import SQLAlchemy
from .models import db
app = FlaskAPI(__name__)
app.config.from_object('app.test_config')
ctx = app.app_context()
ctx.push()
db.init_app(app)
db.create_all()
avars(db.metadata)
提供以下内容:
{'_bind': None,
'_fk_memos': defaultdict(list, {}),
'_schemas': set(),
'_sequences': {},
'naming_convention': immutabledict({'ix': 'ix_%(column_0_label)s'}),
'schema': None,
'tables': immutabledict({'gr_merchant_ftp_info': Table('gr_merchant_ftp_info', MetaData(bind=None), Column('id', BigInteger(), table=<gr_merchant_ftp_info>, primary_key=True, nullable=False), Column('merchant_id', BigInteger(), table=<gr_merchant_ftp_info>), Column('chain_id', BigInteger(), table=<gr_merchant_ftp_info>), Column('hostname', String(length=128), table=<gr_merchant_ftp_info>, nullable=False), Column('username', String(length=128), table=<gr_merchant_ftp_info>, nullable=False), Column('password', String(length=128), table=<gr_merchant_ftp_info>, nullable=False), Column('path', String(length=512), table=<gr_merchant_ftp_info>, nullable=False), Column('install_ts', DateTime(timezone=True), table=<gr_merchant_ftp_info>, server_default=DefaultClause(<sqlalchemy.sql.elements.TextClause object at 0x11007dd10>, for_update=False)), Column('parsed_file_base_path', String(length=256), table=<gr_merchant_ftp_info>), schema=None), 'gr_article_product_mapping': Table('gr_article_product_mapping', MetaData(bind=None), Column('id', BigInteger(), table=<gr_article_product_mapping>, primary_key=True, nullable=False, server_default=DefaultClause(<sqlalchemy.sql.elements.TextClause object at 0x11006fc10>, for_update=False)), Column('product_id', BigInteger(), table=<gr_article_product_mapping>), Column('article_id', String(length=128), table=<gr_article_product_mapping>), Column('install_ts', DateTime(timezone=True), table=<gr_article_product_mapping>, server_default=DefaultClause(<sqlalchemy.sql.elements.TextClause object at 0x11007d090>, for_update=False)), Column('store_code', String(length=128), table=<gr_article_product_mapping>), Column('conversion_factor', Float(precision=53), table=<gr_article_product_mapping>, server_default=DefaultClause(<sqlalchemy.sql.elements.TextClause object at 0x11007d210>, for_update=False)), schema=None), 'gr_store_merchant_mapping': Table('gr_store_merchant_mapping', MetaData(bind=None), Column('id', BigInteger(), table=<gr_store_merchant_mapping>, primary_key=True, nullable=False, server_default=DefaultClause(<sqlalchemy.sql.elements.TextClause object at 0x10f67f790>, for_update=False)), Column('merchant_id', BigInteger(), table=<gr_store_merchant_mapping>), Column('store_id', BigInteger(), table=<gr_store_merchant_mapping>), Column('install_ts', DateTime(timezone=True), table=<gr_store_merchant_mapping>, server_default=DefaultClause(<sqlalchemy.sql.elements.TextClause object at 0x11007d650>, for_update=False)), Column('store_code', String(length=128), table=<gr_store_merchant_mapping>), schema=None)})}
如有任何提示,我们将不胜感激,谢谢!
推荐答案
我想出来了。问题是我使用了多个数据库,并且没有在TEST_CONFIG中指定SQLALCHEMY_BINDS
。不过,后来我面临了一个新问题,尽管create_all
现在可以创建我的表,但由于它经历了所有绑定,Flask-fixtures显然不支持它,它在插入数据时只使用默认引擎。我通过保持SQLALCHEMY_DATABASE_URI
与我的BIND相同来解决这个问题,因此两者都连接到同一个数据库。这是一个非常粗糙的解决方案,但这就是我目前的解决方案。
这篇关于flask-SQLAlChemy CREATE_ALL不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文