'mysql 8 gives error on creating table ( running migrate command in django )

Django3.2 mysql 5.7

I have a model name hOEEHcQtEckeEGroJCMSGBYeRBDgddrPmvRbFSXFBtktNohn and its length is 48 characters and it has 1 not null field. On mysql5.7 it is working fine.

But when try to upgrade with mysql8+ migrations throws error Identifier name is too long.

This model generates following name in mysql8.5 abcdefg_hOEEHcQtEckeEGroJCMSGBYeRBDgddrPmvRbFSXFBtktNohn _chk_1 it has 65 characters.

mysql8 limit info

I have a question what are the possible solutions to fix this issue on mysql8 ?



Solution 1:[1]

You can specify the name at the database side with the db_table Meta option [Django-doc]. For example with:

class BlackboardLearnerAssessmentDataTransmissionAudit(models.Model):
    # …
    
    class Meta:
        db_table = 'transmission_audit'

You can thus first rename the table and then upgrade the database. If you can start from scratch, I would advise removing the migrations where you start using the BlackboardLearnerAssessmentDataTransmissionAudit model. You thus create all the other models with a single migration such that you never use this long name in the first place, but this assumes that you can start migrating from an empty database.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Willem Van Onsem