Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7523011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:40:25+00:00 2026-05-30T02:40:25+00:00

Using Django with a PostgreSQL (8.x) backend, I have a model where I need

  • 0

Using Django with a PostgreSQL (8.x) backend, I have a model where I need to skip a block of ids, e.g. after giving out 49999 I want the next id to be 70000 not 50000 (because that block is reserved for another source where the instances are added explicitly with id – I know that’s not a great design but it’s what I have to work with).

What is the correct/safest place for doing this?

I know I can set the sequence with

SELECT SETVAL(
    (SELECT pg_get_serial_sequence('myapp_mymodel', 'id')),
    70000,
    false
);

but when does Django actually pull a number from the sequence?
Do I override MyModel.save(), call its super and then grab me a cursor and check with

SELECT currval(
    (SELECT pg_get_serial_sequence('myapp_mymodel', 'id'))
);

?

I believe that a sequence may be advanced by django even if saving the model fails, so I want to make sure whenever it hits that number it advances – is there a better place than save()?

P.S.: Even if that was the way to go – can I actually figure out the currval for save()’s session like this? if I grab me a connection and cursor, and execute that second SQL statement, wouldn’t I be in another session and therefore not get a currval?

Thank you for any pointers.

EDIT: I have a feeling that this must be done at database level (concurrency issues) and posted a corresponding PostgreSQL question – How can I forward a primary key sequence in PostgreSQL safely?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T02:40:26+00:00Added an answer on May 30, 2026 at 2:40 am

    My next thought was to not use a sequence for the primary key, but rather always specify the id explicitly from a separate counter table which I check/update before using its latest number – that should be safe from concurrency issues. The only problem is that although I have a single place where I add model instances, other parts of django or third-party apps may still rely on an implicit id, which I don’t want to break.

    But that same mechanism happens to be easily implemented on postgres level – I believe this is the solution:

    • Don’t use SERIAL for the primary key, use DEFAULT my_next_id()
    • Follow the same logic as for “single level gapless sequence” – http://www.varlena.com/GeneralBits/130.php – my_next_id() does an update followed by a select
    • Instead of just increasing by 1, check if a boundary was crossed and if so, increase even further
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using django-nonrel and django-mongodb-engine I have a Django model stored on PostgreSQL
I have a Django-based webapp that is using PostgreSQL as the backend via psycopg2.
I have a strange behaviour on my Django/PostgreSQL system. After saving a model object
Using sqlite3 and Django I want to change to PostgreSQL and keep all data
I have an existing PostgreSQL database that I'm using for a new Django site.
Using Django 1.3 with PostgreSQL 9.0, I have a multi-step object creation function/view, where:
It's my first time using PostgreSQL 8.4.2 with Django(I have always used MySQL or
I'm using Django and Python 2.6, and I want to grow my application using
I'm using django and when users go to www.website.com/ I want to point them
I just started using django for development. At the moment, I have the following

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.