Christopher Nelson Christopher Nelson - 4 years ago 162
Python Question

flask/sqlalchemy _init_() TypeError when adding DB entry?

I was using SQLite for a small testing program and recently moved to Postgres. However I am now getting an issue when I try to add a user to my database. I didn't get this issue on SQLite only on Postgres.

My DB scheme;

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)


The form when I try to add an entry;

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()


here is the full trace;

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'


I'm pretty baffled. I have also tried changing user_roles to other names and I still get the same issue.

Any help would be much appreciated.

Answer Source

I'm not sure where you're inheriting the __init__() method from, but if you explicitly define one it will likely solve the issue:

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')

I'm using **kwargs here, but you could use normal positional arguments as well.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download