Opened 9 years ago

Closed 9 years ago

#25813 closed Bug (duplicate)

inspectdb generates incorrect field names for unique_together

Reported by: Minsoo Kim Owned by: nobody
Component: Database layer (models, ORM) Version: 1.8
Severity: Normal Keywords: inspectdb, MySQL
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Minsoo Kim)

Hi, I'm currently using Django with MySQL and having some trouble with models.py generated by inspectdb command.

So, DDL First :

CREATE TABLE YDB_Collects (
    COriginal_Data_Type_ID VARCHAR(16) NOT NULL,
    CTask_Name VARCHAR(16) NOT NULL,
    PRIMARY KEY (COriginal_Data_Type_ID, CTask_Name),
    INDEX FK_COLLECTS_TASK (CTask_Name),
    CONSTRAINT FK_COLLECTS_ORIGINAL_DATA_TYPE FOREIGN KEY (COriginal_Data_Type_ID) REFERENCES YDB_Original_Data_Type (Original_Data_Type_ID),
    CONSTRAINT FK_COLLECTS_TASK FOREIGN KEY (CTask_Name) REFERENCES YDB_Task (Task_Name)
)

And Django's 'inspectdb' command gave me this model :

class YdbCollects(models.Model):
    coriginal_data_type = models.ForeignKey('YdbOriginalDataType', db_column='COriginal_Data_Type_ID')  # Field name made lowercase.
    ctask_name = models.ForeignKey('YdbTask', db_column='CTask_Name')  # Field name made lowercase.

    class Meta:
        managed = False
        db_table = 'ydb_collects'
        unique_together = (('COriginal_Data_Type_ID', 'CTask_Name'),)

And when I run 'makemigrations' command, It gives me error message : 'unique-together refers to the non-existent field 'COriginal_Data_Type_ID' and 'CTask_Name'.

When I change

 unique_together = (('COriginal_Data_Type_ID', 'CTask_Name'),)

Into

unique_together = (('coriginal_data_type ', 'ctask_name'),)

then It goes OK but is this correct way to go?

Seems like code below has different schema from my DDL...Original foreign key I defined was id of data type, not data type itself.

Did I do something wrong here? Why does unique_together() has column name, not field names? Every Django models generated with MySQL's PRIMARY_KEY(Key1, Key2, ...) gives me this error!

Change History (4)

comment:1 by Minsoo Kim, 9 years ago

Description: modified (diff)

comment:2 by Tim Graham, 9 years ago

Keywords: inspectdb added; indpectdb removed

Might be a duplicate of #25274 -- could you test the patch there?

in reply to:  2 comment:3 by Minsoo Kim, 9 years ago

Replying to timgraham:

Might be a duplicate of #25274 -- could you test the patch there?

Thanks, Now it works good. If it's duplicate, should I remove this ticket?

comment:4 by Tim Graham, 9 years ago

Resolution: duplicate
Status: newclosed
Summary: models.py by 'inspectdb' command gives me errorinspectdb generates incorrect field names for unique_together
Note: See TracTickets for help on using tickets.
Back to Top