我已經看到很多類似於此處發佈的問題,但我沒有發現任何幫助。我正在嘗試使用Flask-Testing和Flask-Fixtures編寫單元測試。我遇到了問題,因爲Flask-Fixtures在傳遞給它的數據庫上調用create_all
,在這種情況下,它實際上並不創建表。我檢查db.metadata.tables.keys
這表明所有型號的表創建使用db.Model
Flask-SQLAlchemy create_all不起作用
config.py
:
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()
答:當我在做這個
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/"),
)
表的創建失敗vars(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)})}
任何指針將不勝感激,謝謝!
單元測試是如此有用,但這是一種屁股疼痛的屁股。我無法從您的問題中判斷是否存在實際的堆棧跟蹤,警告或此處發生的無聲故障。我也不知道你的ORM模型是否可以自己工作,但當你嘗試使用它們進行單元測試時會失敗。 – AlexLordThorsen
這是'失敗'的默默,我想通了。 – madhukar93