Search code examples
pythonpeewee

Python- peewee takes 2 positional arguments but 3 were given


I'm new at learn Python and in course python- connect to database sqlite in this code:

import os
import pewee

baza =  pewee.SqliteDatabase('test.db')

class BazaModel():
 database = baza

class Klasa(BazaModel):
 nazwa =  pewee.CharField(null=False)
 profil =  pewee.CharField(default='')

class Uczen(BazaModel):
 imie =  pewee.CharField(null=False)
 nazwisko =  pewee.CharField(null=False)
 klasa =  pewee.ForeignKeyField(Klasa, related_name='uczniowie')

baza.connect() 
baza.create_tables([Klasa, Uczen], True)

I have problem error :

baza.create_tables([Klasa, Uczen], True) TypeError: create_tables() takes 2 positional arguments but 3 were given

Course is from 2016, so I think have a newer peewee version, but I don't know how fix it...


Solution

  • The 3 arguments are your database, the list of tables, and the boolean : some_obj.some_fun(obj1, obj2) is translated by python as some_fun(some_obj, obj1, obj2). This explains that it tells you you have 3 positional arguments, not 2.

    You can see in peewee doc that create_tables only takes one extra positional argument (two with the database). The third argument you're using is now a keyword argument (not a positional one), you have to specify what it is, i.e. you should write :

    baza.create_tables([Klasa, Uczen], safe=True)