sqlite 为什么peewee忽略了列的唯一定义?

eqqqjvef  于 2022-11-24  发布在  SQLite
关注(0)|答案(1)|浏览(156)

我试图创建表UserTask。在User中,我使user_id列唯一,但peewee忽略了它。当我查看时,没有唯一列:

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

class User(BaseModel):
    user_id = IntegerField(unique=True, null=False)

    class Meta:
        table_name = 'User'

class Task(BaseModel):
    task_content = TextField(null=False)
    completed = BooleanField(null=False, default=False)
    user_id = ForeignKeyField(User, backref='tasks', field='user_id', on_delete='CASCADE', on_update='CASCADE',
                              null=False)

    class Meta:
        table_name = 'Task'
wgmfuz8q

wgmfuz8q1#

适合我:

db = SqliteDatabase(':memory:')

class User(db.Model):
    user_id = IntegerField(unique=True)

User.create_table()

生成以下SQL:

CREATE TABLE IF NOT EXISTS "user" (
  "id" INTEGER NOT NULL PRIMARY KEY,
  "user_id" INTEGER NOT NULL)
CREATE UNIQUE INDEX IF NOT EXISTS "user_user_id" ON "user" ("user_id")

相关问题