3

I have a field "CharField" in a model, when user create a project, how to make a IntegerField/CharField be an default auto-incrementing field in Django as 1000001, 1000002? or year + 00001, 00002, 0003 etc, I cannot have more than one AutoField, and the field already has much data.

Elsa
  • 1
  • 1
  • 8
  • 27
  • Use `AutoField`: https://docs.djangoproject.com/en/2.0/ref/models/fields/#autofield – solarissmoke Oct 17 '18 at 08:43
  • 1
    Possible duplicate of [How to make an auto increment integer field Django](https://stackoverflow.com/questions/21128899/how-to-make-an-auto-increment-integer-field-django) – Sayse Oct 17 '18 at 08:43
  • You cannot auto increment a string because strings aren't sequential – Sayse Oct 17 '18 at 08:44
  • @solarissmoke Thank you, but that solution doesn't work. I cannot have more than one AutoField, and the field already has much data. – Elsa Oct 17 '18 at 09:16
  • @Sayse Thank you, but that solution doesn't work. I cannot have more than one AutoField, and the field already has much data. – Elsa Oct 17 '18 at 09:16
  • I don't think you can do anything from django. IN django just create a normal AutoField. In databse you need to change the auto increment value. See [this question](https://stackoverflow.com/questions/1485668/how-to-set-initial-value-and-auto-increment-in-mysql) for more details. If you are using any other db than mySql search google for your database accordingly. – Vaibhav Vishal Oct 17 '18 at 09:25

1 Answers1

3

Solution is make your field as Auto-field. If you make it auto-field then django won't add primary key which cause error

can't have more than one AutoField. You need to explicitly specify the primary key as

field = models.AutoField(primary_key=True) # or False

To start counting from 1000001 best way is to modify migration file. It depends in the database you are using. If you using Postgres then it looks like this. Edit operations in migration or run the SQL command DB command prompt:

operations = [
    migrations.CreateModel(...),
    # mysql specific
    migrations.RunSQL('alter table tablename auto_increment=1000001'),
]

The alter command will change depends on the database you are using.

If you want a custom AutoField then you can create a char field and make editable false. Then specify value manually. Refer: https://techstream.org/Web-Development/Custom-Auto-Increment-Field-Django

a_k_v
  • 1,558
  • 7
  • 18
  • Hi, thank you for helping me with this issue, I am using Postgres, when I executed `update tablename set auto_increment=1000001;`, 3371 records had been updated, and when I executed `./manage.py migrate`, I got a error of `django.db.utils.DataError: setval: value 0 is out of bounds for sequence "tablename_auto_increment_seq" (1..9223372036854775807)` – Elsa Oct 18 '18 at 01:34
  • The error should be `django.db.utils.IntegrityError: could not create unique index "tablename_auto_increment_0e4a9f27_uniq" DETAIL: Key (auto_increment)=(1000001) is duplicated.` , because I have several records about this field in the database. – Elsa Oct 18 '18 at 02:25
  • Did you set stating position for auto increment field. alter table tablename auto_increment=1000001 will do that for you. or ALTER SEQUENCE field_name RESTART WITH 1000001; I didn't tested it. So I don't know which one will work. – a_k_v Oct 18 '18 at 04:24
  • Hi, yes, I already set stating position for auto increment field, alter table tablename auto_increment=1000001, the question is the auto increment field has many records, auto increment field of database now has 3371 records = 1000001, – Elsa Oct 18 '18 at 05:53
  • 1) You set you auto increment field as primary key. You cant allow duplicate values. 2) I think in postgres auto increment field is of type SERIAL and ALTER SEQUENCE field_name RESTART WITH 1000001; is what you need to set initial value. – a_k_v Oct 18 '18 at 06:07