简体   繁体   中英

SQLite3 OperationalError: Table XYZ has no column named ABC

I am new to peewee, so please forgive me if this is a stupid question. I have searched on Google and in the peewee cookbook, but found no solution so far.

So, I have the following models to four of my DB tables:

class games_def(Model):
    id = PrimaryKeyField()
    name = TextField()
    class Meta:
        database = dbmgr.DB

class users_def(Model):
    id = PrimaryKeyField()
    first_name = TextField()
    last_name = TextField()
    class Meta:
        database = dbmgr.DB

class sessions(Model):
    id = PrimaryKeyField()
    game = ForeignKeyField(games_def, related_name = 'sessions')
    user = ForeignKeyField(users_def, related_name = 'sessions')
    comment = TextField()
    class Meta:
        database = dbmgr.DB

class world_states(Model):
    session = ForeignKeyField(sessions)
    time_step = IntegerField()
    world_state = TextField()
    class Meta:
        database = dbmgr.DB

Using these models I connect to an SQLite3 DB via peewee, which works fine. After the connection has been established I do the following in my main Python code:

models.world_states.create(session = 1, time_step = 1)

However, this gives me the following error:

sqlite3.OperationalError: table world_states has no column named session_id

That is basically correct, the table world_state does indeed not contain such a column.
However, I cannot find any reference to "session_id" in my code at all.

Whe does peewee want to use that "session_id" colum name?
Do I miss something essentially here?

When you specify a ForeignKeyField() peewee expects to use a column ending in _id based on their own name. Your wold_states.session field thus results in an column named session_id .

You can override this by setting db_column for that field:

class world_states(Model):
    session = ForeignKeyField(sessions, db_column='session')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM