当我尝试填充数据库时,为什么会收到位置参数错误?

时间:2017-02-07 18:27:41

标签: python-3.x flask sqlalchemy flask-sqlalchemy

我目前正在运行一个脚本,该脚本应该创建一个SQLAlchemy数据库,该数据库应该由以下信息填充;此DB链接到基于Flask的任务列表应用程序。我正在使用SQLAlchemy 2.1。 这是db_create.py脚本。

# project/db_create.py
from views import db
from models import Task
from datetime import date

# create the database and the db table

db.create_all()
# insert data

db.session.add(Task("Finish this", date(2016, 9, 22), 10, 1))
db.session.add(Task("Finish Python", date(2016, 10, 3), 10, 1))

# commit the changes
db.session.commit()

现在我每次尝试创建数据库时都会收到此错误:

/Users/Paul/Desktop/RealPython/flasktaskr/ENV/lib/python3
.6/site-packages/flask_sqlalchemy/__init__.py:800: UserWarning:    SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future.  Set it to True to suppress this warning.
warnings.warn('SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and   will be disabled by default in the future.  Set it to True to suppress this warning.')
Traceback (most recent call last):
File "db_create.py", line 8, in <module>
db.session.add(Task("Finish this", date(2016, 9, 22), 10, 1))
TypeError: __init__() takes 1 positional argument but 5 were given

我已经在应用程序中的一个文件中找到了一个init()函数,这可能对你有所帮助:

def __init__(self, name, due_date, priority, status):
    self.name = name
    self.due_date = due_date
    self.priority = priority
    self.status = status

当我运行db_create.py文件时,数据库已创建,但是,它无法使用该原始文件中的数据填充数据库。

为什么我收到错误,为什么数据库无法填充?

1 个答案:

答案 0 :(得分:1)

您需要在__init__()

的sqlalchemy类定义中添加Task方法

有关示例,请参阅docs

第一个位置参数总是自我(阅读有关面向对象的编程以了解更多信息),但实质上这个错误是说你传递参数而你的类定义并不知道如何处理它们。

相关问题