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!
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.