Error IntegrityError 1062 Duplicate entry 1-1-1 for key CSID

0 votes

I have a model with a unique_together defined for 3 fields to be unique together:

class MyModel(models.Model):
    clid = models.AutoField(primary_key=True, db_column='CLID')
    csid = models.IntegerField(db_column='CSID')
    cid = models.IntegerField(db_column='CID')
    uuid = models.CharField(max_length=96, db_column='UUID', blank=True)

    class Meta(models.Meta):
        unique_together = [
            ["csid", "cid", "uuid"],
        ]

Now, if I attempt to save a MyModel instance with an existing csid+cid+uuid combination, I would get:

IntegrityError: (1062, "Duplicate entry '1-1-1' for key 'CSID'")

Which is correct. But, is there a way to customize that key name? (CSID in this case)pyt

Jul 3, 2020 in Python by kartik
• 37,510 points
1,740 views

1 answer to this question.

0 votes

Hello @kartik,

Changing index name in ./manage.py sqlall output.

You could run ./manage.py sqlall yourself and add in the constraint name yourself and apply manually instead of syncdb.

$ ./manage.py sqlall test
BEGIN;
CREATE TABLE `test_mymodel` (
    `CLID` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
    `CSID` integer NOT NULL,
    `CID` integer NOT NULL,
    `UUID` varchar(96) NOT NULL,
    UNIQUE (`CSID`, `CID`, `UUID`)
)
;

COMMIT;

e.g.

$ ./manage.py sqlall test
BEGIN;
CREATE TABLE `test_mymodel` (
    `CLID` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
    `CSID` integer NOT NULL,
    `CID` integer NOT NULL,
    `UUID` varchar(96) NOT NULL,
    UNIQUE constraint_name (`CSID`, `CID`, `UUID`)
)
;

COMMIT;

Hope it helps!!
Thank You!!

answered Jul 3, 2020 by Niroj
• 82,880 points

Related Questions In Python

0 votes
1 answer

'Syntax Error: invalid syntax' for no apparent reason

You're missing a close paren in this ...READ MORE

answered Aug 13, 2018 in Python by Priyaj
• 58,090 points
2,214 views
0 votes
1 answer

Python error: No handlers could be found for logger “xhtml2pdf”

The only solution is to define a ...READ MORE

answered Sep 5, 2018 in Python by Priyaj
• 58,090 points
1,913 views
0 votes
1 answer

How to temporarily disable a foreign key constraint in MySQL?

Hello @kartik, To turn off foreign key constraint ...READ MORE

answered Jun 23, 2020 in Python by Niroj
• 82,880 points
2,030 views
0 votes
1 answer

How do I use Django templates without the rest of Django?

Hello @kartik, Let's say you have this important ...READ MORE

answered Jun 23, 2020 in Python by Niroj
• 82,880 points
1,176 views
0 votes
2 answers
0 votes
1 answer

Error:setup script exited with error: command 'x86_64-linux-gnu-gcc' failed with exit status 1

Hello @kartik, Try installing these packages. sudo apt-get install ...READ MORE

answered Jun 5, 2020 in Python by Niroj
• 82,880 points
6,664 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP