migrations.RunSQL 1

migrations.RunSQL

def set_pg_sequence(apps, schema_editor):
    if connection.vendor == "postgresql":
        # Reset sequence of user so Postgres doesn't crash on an existing user
        # ID 100 for our uploader user.
        # http://stackoverflow.com/a/3698777/85461
        migrations.RunSQL(
            """
            SELECT pg_catalog.setval(
                pg_get_serial_sequence('auth_user', 'id'), MAX(id)
            ) FROM auth_user;
            """.format(table_name=settings.AUTH_USER_MODEL)
        )

Here is what the above code is Doing:
1. We’re checking if the database is Postgres.
2. If it is, we’re running a SQL query to reset the sequence of the user ID.
3. We’re using the max ID of the user table to set the sequence.
4. We’re using the AUTH_USER_MODEL setting to get the table name.

Similar Posts