简体   繁体   中英

Why peewee ignores column's unique definition?

I am trying to create tables User and Task . In User I make user_id column unique, but peewee ignores it. There is no unique column when I look:

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'

Works for me:

db = SqliteDatabase(':memory:')

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

User.create_table()

Results in the following 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")

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