Make primary key with two or more field in Django

Most of the time, people don’t actually need their composite (multi-column) key to be the primary key.

Django operates best with surrogate keys –  that is, it automatically defines an autoincrement field called id and sets that to be the primary key. That is suitable for almost all the users.

If you then need to enforce a unique constraint on your model across two or more fields, you can use the unique_together setting in the inner Meta class.

class Student(models.Model):
    Name = models.CharField(max_length=100)
    Roll = models.CharField(max_length=20)
    Department = models.CharField(max_length=15)

    class Meta:
        unique_together = (('Roll', 'Department'),)

It would act as a surrogate primary key column.

Sets of field names that, taken together, must be unique:

unique_together = (('Roll', 'Department'),)

This is a tuple of tuples that must be unique when considered together. It’s used in the Django admin and is enforced at the database level (i.e., the appropriate UNIQUE statements are included in the CREATE TABLE statement).

For convenience, unique_together can be a single tuple when dealing with a single set of fields :

unique_together = ('Roll', 'Department')

9 thoughts on “Make primary key with two or more field in Django

  1. Hi Shashank,

    Thanks for the tutorial. I have a table that uses three columns unique_together as composite keys. However, this doesn’t seem to work. I get this error when I check the table at the admin site “(1054, “Unknown column ‘table.id’ in ‘field list'”)”..this is because django expects a pk to be specified otherwise it creates one. Have you encountered such and what was you way around it if you have? Thanks a bunch

    • Hi ,

      Can you give me little extra information regarding your model. Sometimes this problem comes when you update your existing model. probably, this is a database level problem where you are trying to access a field which doesn’t exist in database.

      Thanks.

  2. I did a database first design and used inspectdb to extract the models from MySQL. The error arose when I decided to manage the tables from the admin site.I decided to introduce an id column as a pk to solve the problem for now. ..Thanks.

    • unique_together makes sure there is a database level constraint & when you save your model it gets verified.
      as far as retrieving data is concerned it remains as it is.
      you can do something like

      Students.objects.get(Roll=”Rollnumbervalue”,Department=”Departmentvalue”)

  3. In my case I have two primary key(RollNo, Dept) and when I create first record (1, IT), It is ok.
    But when I create second record(1, Electronics), the first record is replaced with second record in database.
    The first record does not exost anymore.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s