Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

222
Views
Does Django index Autofield / ID keys in PostgreSQL?

Django's docs state that id fields created with AutoField are indexed:

id is indexed by the database and is guaranteed to be unique.

Similarly it applies an index to every FK relationship.

However, in PostgreSQL whilst FKs appear to be indexed, IDs are not. Here's an example:

class TestModelBase(models.Model):
    name = models.CharField(max_length=50)
    fkfield = models.ForeignKey(TestModelFK, blank=True, null=True,
                                on_delete=models.CASCADE)
    m2mfield = models.ManyToManyField(TestModelM2M, related_name='base_m2m')

This model appears to apply the fkfield index, but not the id autofield. From PGAdmin below:

enter image description here

Am I missing something?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

PostgreSQL automatically creates indexes for primary keys. From the docs:

Adding a primary key will automatically create a unique B-tree index on the column or group of columns listed in the primary key, and will force the column(s) to be marked NOT NULL.

It appears that PGAdmin does not show those indexes. This mailing list thread is the best source I could find.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!