-1

I am working with SQLite and peewee and have run into a TypeError that im not sure how to fix. Here is my program.

file = 'network.db'
if os.path.exists(file):
os.remove(file)

db = pw.SqliteDatabase(pw.Model)

class BaseModel(pw.Model):
    class Meta:
        database = db

class User(BaseModel):
    '''I define my User Class here'''

class Status(BaseModel):
    '''I define my Status Class here'''

def main():
    db.connect()
    db.execute_sql('PRAGMA foreign_keys = ON;')
    db.create_tables([
        User,
        Status])
 
   users = [
            (user1, test1, name1),
            (user2, test2, name3),
            (user3, test3, name3)]

    for account in users:
        try:
            with db.transaction():
                new_account = User.create(
                    user_id=account[0],
                    email=account[1],
                    user_name=account[2],
                    user_last_name=account[3])
                new_account.save()
    except Exception as e:

    status = [
            (status1, user1, text1),
            (status2, user2, text3),
            (status3, user3, text3)]

    for message in status:
        try:
            with db.transaction():
                new_status = Status.create(
                    status_id=message[0],
                    user_id=message[1],
                    status_text=message[2],
                )
            new_status.save()
        except Exception as e:

    db.close()

main()

When I run this it gives me this error:

Traceback (most recent call last):
  File "C:\Documents\folder\network_model.py", line 105, in <module> 
func()
  File "C:\Documents\folder\assignment-03-parkertheoj\network_model.py", line 52, in func
db.connect()
  File "C:\Documents\folder\venv\lib\site-packages\peewee.py", line 3177, in connect
self._state.set_connection(self._connect())
  File "C:\Documents\folder\venv\lib\site-packages\peewee.py", line 3521, in _connect
conn = sqlite3.connect(self.database, timeout=self._timeout,
TypeError: expected str, bytes or os.PathLike object, not ModelBase

And it looks to me that there error is coming from the peewee module itself, but I'm not sure how to fix that. Any help would be appreciated! Thanks!

1 Answers1

0

Where on earth did you get this: db = pw.SqliteDatabase(pw.Model)

It should be

db = pw.SqliteDatabase('path/to/database-file.db')
coleifer
  • 24,887
  • 6
  • 60
  • 75