2

In mysql if you want to start your auto-increment key from 2456 you can write AUTO_INCREMENT=2456

Also in PostgreSQL we can do this by using Sequence

CREATE SEQUENCE serial START 2456;
INSERT INTO distributors VALUES (nextval('serial'), 'nothing');

But in Django Rest Framework or Django by default auto-increment starts from 1. If I have a model like this

class Content(models.Model):
     title = models.CharField(max_length=70, blank=False, default='')
     description = models.CharField(max_length=200,blank=False, default='')
     published = models.BooleanField(default=False)

How can I make sure that the first Content starts from 2456?

Sujoy
  • 69
  • 1
  • 5
  • Does this answer your question? [Reset auto increment counter in postgres](https://stackoverflow.com/questions/5342440/reset-auto-increment-counter-in-postgres) – JPG Oct 05 '20 at 18:51
  • No. That is from the database point of view.. I need to auto-increment from Django models. By default in django model, the primary key is an auto-increment key that starts from 1. I want that auto-increment key to start from a certain number. – Sujoy Oct 05 '20 at 18:57
  • You can use [**`RunSQL`**](https://docs.djangoproject.com/en/3.1/ref/migration-operations/#runsql) migration – JPG Oct 05 '20 at 18:59
  • I am not familiar with RunSQL. So thank you for providing an example. – Sujoy Oct 05 '20 at 19:18

1 Answers1

3

Create a RunSQL--(Django Doc) operation

from django.db import migrations


class Migration(migrations.Migration):
    dependencies = [
        ('sample', '0003_content'),
    ]

    operations = [
        migrations.RunSQL(
            "ALTER SEQUENCE sample_content_id_seq RESTART WITH 1453"
        ),
    ]
JPG
  • 82,442
  • 19
  • 127
  • 206