gpt4 book ai didi

python - Flask-SQLAlchemy - 类型错误 : __init__() takes only 1 position

转载 作者:行者123 更新时间:2023-11-28 21:10:04 24 4
gpt4 key购买 nike

我只想用 flask-sqlalchemy 创建和测试数据库。数据库已成功创建。我的代码:

class Entry(db.Model):
id = db.Column(db.Integer, primary_key=True)
occurences = db.Column(db.String(80), unique=True)

a = Entry('run.py:27')

错误:

    a = Entry('run.py:27')
TypeError: __init__() takes 1 positional argument but 2 were given

如果我在没有参数的情况下尝试这样做,程序会返回:

qlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such table:        entry [SQL: 'INSERT INTO entry (occurences) VALUES (?)'] [parameters: (None,)]

错误开始于

db.session.commit()

最佳答案

根据他们的 docs :

SQLAlchemy adds an implicit constructor to all model classes which accepts keyword arguments for all its columns and relationships. If you decide to override the constructor for any reason, make sure to keep accepting **kwargs and call the super constructor with those **kwargs to preserve this behavior...

这意味着您的代码失败了,因为构造函数需要关键字参数。您可以通过以下方式修复它:

# instantiating Entry with the occurences keyword
a = Entry(occurences='run.py:27')

或覆盖 __init__...在这种情况下,您还应该包含一个 **kwargs 和对 super 的调用。

关于python - Flask-SQLAlchemy - 类型错误 : __init__() takes only 1 position,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37166492/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com