seo 添加唯一约束

class TinyUrl(Model):
    id = BigAutoField()
    user_id = IntegerField(null=False)
    short_uuid = CharField(max_length=255, null=False, help_text='长度为 6 位')
    long_url_hash = CharField(max_length=64, null=False)
    long_url = CharField(max_length=2048, null=False)
    created_at = DateTimeField(
        null=False,
        constraints=[SQL('DEFAULT CURRENT_TIMESTAMP')],
        help_text='使用数据库时间'
    )
    updated_at = DateTimeField(
        null=False,
        constraints=[
            SQL('DEFAULT CURRENT_TIMESTAMP'),
            SQL('ON UPDATE CURRENT_TIMESTAMP'),
        ]
    )

    class Meta:
        database = db
        table_name = 'tiny_url'
        indexes = (
            (('short_uuid',), True),
            (('user_id', 'long_url_hash'), True),
        )


使用 indexes 就可以了

参考文档:peewee Multi-column indexes

图片.png


universe_king
3.4k 声望680 粉丝