Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 :(

like image 890
McKay Avatar asked Mar 12 '23 12:03

McKay


1 Answers

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.

like image 123
GwynBleidD Avatar answered Apr 01 '23 21:04

GwynBleidD