Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/317.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
Python 为什么在使用Flask SQLAlchemy填充数据库时出现非外观错误?_Python_Orm_Flask_Sqlalchemy_Flask Sqlalchemy - Fatal编程技术网

Python 为什么在使用Flask SQLAlchemy填充数据库时出现非外观错误?

Python 为什么在使用Flask SQLAlchemy填充数据库时出现非外观错误?,python,orm,flask,sqlalchemy,flask-sqlalchemy,Python,Orm,Flask,Sqlalchemy,Flask Sqlalchemy,我是SQLAlchemy的新手,正在为我当前的项目使用SQLAlchemy。我遇到了一个让我难堪的错误: sqlalchemy.orm.exc.UnmappedInstanceError: Class 'flask_sqlalchemy._BoundDeclarativeMeta' is not mapped; was a class (app.thing.Thing) supplied where an instance was required? 我试着在Instantiation中更改符

我是SQLAlchemy的新手,正在为我当前的项目使用SQLAlchemy。我遇到了一个让我难堪的错误:

sqlalchemy.orm.exc.UnmappedInstanceError: Class 'flask_sqlalchemy._BoundDeclarativeMeta' is not mapped; was a class (app.thing.Thing) supplied where an instance was required?
我试着在Instantiation中更改符号,并移动一些东西。问题可能与此有关,还是与其他有关

这是我的模块(
thing.py
),其中的类继承自Flask SQLAlchemy的
db.Model
(如SQLAlchemy的
Base
类):

下面是我在
thing_wrangler.py
模块中填充数据库的调用:

import thing
from app import db

def populate_database(number_to_add):
    for each_thing in range(number_to_add):
        a_thing = thing.Thing
        a_thing.stuff_1 = 1
        a_thing.stuff_2 = 2
        a_thing.some_stuff = [1,2,3,4,5]
        db.session.add(a_thing)
    db.session.commit()

您需要创建模型的实例。它应该是
a\u thing=thing.thing()
,而不是
a\u thing=thing.thing()。注意括号。由于您重写了
\uuuu init\uuuu
,因此还需要修复它,以便将
self
作为第一个参数

import thing
from app import db

def populate_database(number_to_add):
    for each_thing in range(number_to_add):
        a_thing = thing.Thing
        a_thing.stuff_1 = 1
        a_thing.stuff_2 = 2
        a_thing.some_stuff = [1,2,3,4,5]
        db.session.add(a_thing)
    db.session.commit()