简体   繁体   English

SQLite3 OperationalError:表XYZ没有名为ABC的列

[英]SQLite3 OperationalError: Table XYZ has no column named ABC

I am new to peewee, so please forgive me if this is a stupid question. 我是peewee的新手,所以如果这是一个愚蠢的问题,请原谅我。 I have searched on Google and in the peewee cookbook, but found no solution so far. 我在Google和peewee食谱上搜索过,但到目前为止还没有找到解决办法。

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. 使用这些模型,我通过peewee连接到SQLite3数据库,工作正常。 After the connection has been established I do the following in my main Python code: 建立连接后,我在我的主要Python代码中执行以下操作:

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. 这基本上是正确的,表world_state确实不包含这样的列。
However, I cannot find any reference to "session_id" in my code at all. 但是,我在代码中找不到任何对“session_id”的引用。

Whe does peewee want to use that "session_id" colum name? peewee想要使用那个“session_id”列名吗?
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. 当您指定ForeignKeyField() peewee希望根据自己的名称使用以_id结尾的列。 Your wold_states.session field thus results in an column named session_id . 因此,您的wold_states.session字段会生成名为session_id的列。

You can override this by setting db_column for that field: 您可以通过为该字段设置db_column来覆盖它:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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