Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flask 炼金术创造了所有的不存在';行不通_Flask_Sqlalchemy_Flask Sqlalchemy - Fatal编程技术网

Flask 炼金术创造了所有的不存在';行不通

Flask 炼金术创造了所有的不存在';行不通,flask,sqlalchemy,flask-sqlalchemy,Flask,Sqlalchemy,Flask Sqlalchemy,我在这里看到了一些类似的问题,但我没有发现任何帮助。我正在尝试使用烧瓶测试和烧瓶固定装置编写单元测试。自从Flask Fixtures调用传递给它的db上的create\u all之后,我遇到了一个问题,在本例中,它实际上并没有创建表。我选中了db.metadata.tables.keys,其中显示了使用db.Model config.py: import os from config import BASE_DIR # SQLALCHEMY_DATABASE_URI = 'sqlite://

我在这里看到了一些类似的问题,但我没有发现任何帮助。我正在尝试使用烧瓶测试和烧瓶固定装置编写单元测试。自从Flask Fixtures调用传递给它的db上的
create\u 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()

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)})}
{''u bind':无,
“_fk_memos”:defaultdict(list,{}),
“_架构”:set(),
“_序列”:{},
'naming_convention':不可变的dict({'ix':'ix_u%(列0_标签)s'}),
“架构”:无,
“tables”:不可变dict({'gr_merchant_ftp_info':表('gr_merchant_ftp_info',元数据(bind=None),列('id',biginger(),Table=,primary_key=True,nullable=False),列('merchant_id',biginger(),Table=),列('chain_id',biginger(),Table=),列('hostname',字符串(长度=128),表=,nullable=False),列('username',字符串(长度=128),表=,可空=False),列('password',字符串(长度=128),表=,可空=False),列('path',字符串(长度=512),表=,可空=False),列('install_ts',DateTime(时区=True),表=,server_default=default子句(,for_update=False)),列('parsed_file_base_path',String(长度=256),表=),模式=None),'gr_article_product_mapping':表('gr_article_product_mapping',元数据(bind=None),列('id',biginger(),Table=,primary_key=True,nullable=False,server_default=default子句(,for_update=False)),列('product_id',biginger(),Table=),列('article_id',String(长度=128),表=),列('install_ts',DateTime(时区=True),table=,server\u default=default子句(,for\u update=False)),Column('store\u code',String(length=128),table=),Column('conversion\u factor',Float(precision=53),table=,server\u default=default子句(,for\u update=False)),schema=None),'gr\u store\u merchant\u mapping':table('gr\u store\u merchant\u-mapping',元数据(bind=None),Column('id',biginger(),table=,primary_key=True,nullable=False,server_default=default子句(,for_update=False)),Column('merchant_id',biginger(),table=),Column('store_id',biginger(),table=),Column('install_ts',DateTime(timezone=True),table=,server_default=default子句(,for_update=False)),Column('store_code',String(长度=128),table=),schema=None)})}

任何指针都将非常感谢,谢谢!

我找到了答案。问题是我使用了多个数据库,而且我没有在测试配置中指定
SQLALCHEMY\u BINDS
。尽管
create\u all
现在可以创建我的表,因为它通过了所有绑定,但后来我遇到了一个新问题res显然不支持它,它只是在插入数据时使用默认引擎。我通过保持
SQLALCHEMY\u DATABASE\u URI
与我的绑定相同来解决它,因此两者都连接到同一个数据库。这是一个非常粗糙的解决方案,但这是我目前拥有的。

单元测试非常有用,但这是一种令人痛苦的狗屎在屁股里。我不能从你的问题中分辨出这里是否发生了实际的堆栈跟踪、警告或无声的故障。我也不能分辨你的ORM模型是否能够独立工作,但当你尝试使用它们进行单元测试时失败了。不过,我发现这是无声的“失败”。