简体   繁体   中英

How do I make a primary key without auto-increment?

I've been trying to create a model that has a primary key, but I don't want that primary key to auto increment.

I know I can specify the value each time, but I want the field to be required that I specify it (hopefully enforced by the database and django), and fail fast if I forget.

It seemed logical that I would be able to say auto_increment=False on my field, but that isn't supported by the field :(

Just create id field with primary_key=True explicitly in your model:

class SomeModel(models.Model):
    id = models.IntegerField(primary_key=True)

That way it won't be auto-incremented, but it will still be an primary key.

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