Flask:在启动时插入一些测试数据吗?

时间:2018-11-07 00:53:45

标签: python flask flask-sqlalchemy

我在烧瓶第一次启动时试图将一些虚拟数据插入数据库。我正在使用flask-migrate

创建表格

我跑步:

  • flask db init
  • flask db migrate
  • flask db upgrade

我检查了迁移脚本,一切看起来都很好。

接下来,我启动Dbeaver并打开test.db文件,并验证是否确实创建了我的模型中的表。

我甚至在Dbeaver中运行insert into keywords (keyword) values ('test');只是为了很好地验证我可以插入一些数据。

但是,当我通过flask run启动我的应用程序时,我得到了sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such table: keywords ??

回溯将在existing = Keyword.query.filter(Keyword.keyword == k.lower()).one_or_none()函数内的create_app上引发错误。

整个create_app函数如下:

def create_app(config='metadata.settings'):
    app = Flask(__name__, instance_relative_config=True)

    app.config.from_object(config)
    load_extensions(app)  # sets db.init_app(app)

    @app.before_first_request
    def insert_test_data():
        test_record = {
            "metadata_version": "1.1",
            "name": "foo-package",
            "version": "0.1",
            "platforms": [
                "UNKNOWN"
            ],
            "summary": "Hello Foo Package",
            "keywords": "foo pandas numpy",
            "home_page": "https://gitlab.blah.com/my-examples/model-bar",
            "author": "John Smith",
            "author_email": "john.smith@gmail.com",
            "classifiers": [
                "Programming Language :: Python :: 3",
                "Operating System :: Microsoft :: Windows :: Windows 10",
                "Development Status :: 1 - Planning",
                "Topic :: Software Development :: Testing :: Mocking",
                "Topic :: Software Development :: Testing"
            ]
        }

        def add_record(test_record):
            rec = PackageMetadata()
            rec.name = test_record['name'],
            rec.version = test_record['version'],
            rec.desc = test_record['summary'],
            rec.home_page = test_record['home_page'],
            rec.author = test_record['author'],
            rec.author_email = test_record['author_email']

            keywords = test_record['keywords'].split(' ')
            for keyword in keywords:
                keyword_to_add = add_keyword(keyword)
                rec.keywords.append(keyword_to_add)

            classifiers = test_record['classifiers']
            for classifier in classifiers:
                classifier_to_add = add_classifier(classifier)
                rec.classifiers.append(classifier_to_add)

            try:
                db.session.add(rec)
                db.session.commit()
            except Exception as e:
                print(e)

        def add_keyword(k):
            existing = Keyword.query.filter(Keyword.keyword == k.lower()).one_or_none()
            if existing is not None:
                return existing
            else:
                new_keyword = Keyword()
                new_keyword.keyword = k.lower()
                return new_keyword

        def add_classifier(k):
            existing = Classifier.query.filter(Classifier.classifier == k).one_or_none()
            if existing is not None:
                return existing
            else:
                new_classifier = Classifier()
                new_classifier.classifier = k
                return new_classifier
        add_record(test_record)
    return app

1 个答案:

答案 0 :(得分:1)

在DB中创建的表的名称与您在模型文件中使用的名称不同。您需要在架构文件中进行验证。