python - flask/sqlalchemy _init_() 添加数据库条目时出现类型错误?

标签 python postgresql flask flask-sqlalchemy

我在一个小的测试程序中使用 SQLite,最近搬到了 Postgres。但是,当我尝试将用户添加到我的数据库时,我现在遇到了问题。我没有在 Postgres 上的 SQLite 上遇到这个问题。

我的数据库方案;

class User(UserMixin, db.Model):
    __tablename__ = 'users'
    id = db.Column(db.Integer, primary_key=True, nullable=False)
    username = db.Column(db.String(64), index=True, nullable=False, unique=True)
    password = db.Column(db.String(128), nullable=False)
    user_roles = db.Column(db.String(64), nullable=False)

我尝试添加条目时的表单;

username = request.form['email']
plain_pass = request.form['pass']
hash_pass = hash_password(plain_pass)
role = 'instructor'
add_user = User(username=username,
                password=hash_pass,
                user_roles=role) # This is the line from the error
# I can browse the DB and I know the that table/column is created
# In pyCharm it comes up as an unexpected argument?
db.session.add(add_user)
db.session.commit()

这是完整的轨迹;

    Traceback (most recent call last):
  File "C:\Python34\lib\site-packages\flask\app.py", line 1994, in __call__
    return self.wsgi_app(environ, start_response)
  File "C:\Python34\lib\site-packages\flask\app.py", line 1985, in wsgi_app
    response = self.handle_exception(e)
  File "C:\Python34\lib\site-packages\flask\app.py", line 1540, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Python34\lib\site-packages\flask\_compat.py", line 33, in reraise
    raise value
  File "C:\Python34\lib\site-packages\flask\app.py", line 1982, in wsgi_app
    response = self.full_dispatch_request()
  File "C:\Python34\lib\site-packages\flask\app.py", line 1614, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "C:\Python34\lib\site-packages\flask\app.py", line 1517, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Python34\lib\site-packages\flask\_compat.py", line 33, in reraise
    raise value
  File "C:\Python34\lib\site-packages\flask\app.py", line 1612, in full_dispatch_request
    rv = self.dispatch_request()
  File "C:\Python34\lib\site-packages\flask\app.py", line 1598, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "C:\Users\chrisutpg\Desktop\Programing\UTPPv2\app\mod_auth\views.py", line 18, in show_signup
    forms.Signup()
  File "C:\Users\chrisutpg\Desktop\Programing\UTPPv2\app\mod_auth\forms.py", line 15, in Signup
    user_roles=role)
TypeError: __init__() got an unexpected keyword argument 'user_roles'

我很困惑。我也尝试过将 user_roles 更改为其他名称,但我仍然遇到同样的问题。

如有任何帮助,我们将不胜感激。

最佳答案

我不确定你从哪里继承了 __init__() 方法,但如果你明确定义一个它可能会解决问题:

class User(UserMixin, db.Model):
    __tablename__ = 'users'
    id = db.Column(db.Integer, primary_key=True, nullable=False)
    username = db.Column(db.String(64), index=True, nullable=False, unique=True)
    password = db.Column(db.String(128), nullable=False)
    user_roles = db.Column(db.String(64), nullable=False)

    def __init__(self, **kwargs):
        self.username = kwargs.get('username')
        self.password = kwargs.get('password')
        self.user_roles = kwargs.get('user_roles')

我在这里使用 **kwargs,但您也可以使用普通的位置参数。

关于python - flask/sqlalchemy _init_() 添加数据库条目时出现类型错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42079798/

相关文章:

python - 在 Python 中生成 Chebyshev 多项式的系数

python - 如何使用python matplotlib库从WAV文件中提取数据?

python - 如何使用 Python 子进程在 Windows 中进行搜索

python - 在一个字符串中追加和替换对象

python - Bcrypt salt 将字节对象视为字符串并且不会散列密码

c++ - QSqlQuery 将 QByteArray 作为字符串插入 PostgreSQL

postgresql - pgpool-II 认证失败

sql - Postgresql 删除重复的反向对

python - 使用 ajax 间歇性出现 Flask-Login 问题

python - Random.randint() 刷新后不起作用。