Python 为什么在使用Flask SQLAlchemy填充数据库时出现非外观错误?
我是SQLAlchemy的新手,正在为我当前的项目使用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.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()