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 8564015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:06:05+00:00 2026-06-11T17:06:05+00:00

I was curious about pk’s and how they work. Django automatically has the primary

  • 0

I was curious about pk’s and how they work. Django automatically has the primary key for all models set to an auto incrementing integer field, but is this normally the best option in terms of efficiency?

What are the scenarios where leaving the pk as an incrementing integer serves more harm than somehow creating a custom one? And what are custom pk’s that people usually use?

EDIT: One last question. For most of my previous projects, I have used the primary key publically in the URL as a get parameter, for example: website.com/1234/ where 1234 is the pk ‘id’ of a queried model. Is this considered bad design or is doing this a commonality?

Thanks!

  • 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-06-11T17:06:06+00:00Added an answer on June 11, 2026 at 5:06 pm

    A primary key is just a special type of index for data in a table. One constraint of the primary key is that it must be unique. When you perform a lookup using the primary key in the where clause, the retrieval of that data is very fast.

    Having an auto-incrementing primary key doesn’t serve much purpose if your queries will never perform a lookup using the primary key or join its data with another table based on a primary key. In this case, its just a wasted index and you’d be better off using a primary key from other columns in your table (if you can satisfy the unique constraint). If it doesn’t make sense to have an auto-incrementing pkey, and you also can’t have a unique index, then just create the table without the pkey and make sure you always use the most efficient index when fetching, updating or deleting single records.

    I can’t think of any reason where leaving the pkey as an auto-incrementing integer can do more harm than good, only waste space if never used and become extraneous. As pointed out in comments, a sequential primary key may expose information or make you vulnerable to certain social engineering attacks when the pkey is the only required piece of information to access data.

    You may use a custom primary key if you want to use a composite index as the primary key. Another instance may be a table that tracks items that all have a unique serial number. In this case, you could use the serial number as the pkey instead of an auto-incrementing ID since the serial number is also unique and would likely be used in most lookups or searches.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was curious about what all locations JVM looks for executing a program? I'm
I am curious about a scenario set up like the following example: Here is
I've been curious about the impact of not having an explicit primary constructor in
I'm just curious about this really, does anyone know why they broke convention on
Curious about how developers more experienced with Django than I might handle this: I
I'm curious about the possibility to alter href attributes of all generated links. Let's
Just curious about what would be the django way of achieving the following :
I was just curious about this to see how this might work. $number =
Curious about running multiple xvfb displays: I have between 10-50 instances of a script
Being curious about how maven works exactly, I've checked out its source codes. Astonishingly,

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.