flask / sqlalchemy _init_()添加数据库条目时的TypeError?

时间:2017-02-07 00:38:38

标签: 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更改为其他名称,我仍然遇到同样的问题。

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

我不确定您从哪里继承__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,但您也可以使用普通的位置参数。