How to implement composite primary keys in Django?

76 Views Asked by At

Currently, I am using Django version 3.2.3 I have created this model where columns c1,c2 and c3 form composite primary keys


class myModel(models.Model):
    _DATABASE = "my_db"
    c1 = models.CharField(db_column='c1', primary_key=True, max_length=20)
    c2 = models.CharField(db_column='c2', max_length=20)
    c3 = models.CharField(db_column='c3', max_length=20)
    c4 = models.CharField(db_column='c4', max_length=50)
    c5 = models.CharField(db_column='c5', max_length=1024)

    class Meta:
        managed = False
        db_table = 'my_table'
        unique_together = (('c1', 'c2', 'c3'),)
`

unique_together is not working Django is considering only c1 as primary key

0

There are 0 best solutions below