Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask-SQLAlchemy - TypeError: __init__() takes only 1 position

I just want to create and test db with flask-sqlalchemy. DB created yet succesfuly. My code:

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

Error:

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

If I trying to do this without arguments program returns this:

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

error starts on line with

db.session.commit()
like image 277
Illia Ananich Avatar asked May 11 '16 15:05

Illia Ananich


1 Answers

According to their 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...

Meaning your code failed because the constructor expects keyword arguments. You could fix it by:

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

or overriding the __init__...in which case you should also include a **kwargs and a call to super.

like image 164
Ullauri Avatar answered Oct 10 '22 17:10

Ullauri