microsoft/mssql-django

Error when inspecting table with nonclustered index

quimey opened this issue · 1 comments

quimey commented

Software versions

  • Django: 4.2.9
  • mssql-django: 1.3
  • python: 3.11.7
  • SQL Server: Microsoft SQL Server 2022 (RTM-CU10-GDR) (KB5033592) - 16.0.4100.1 (X64)
  • OS: Fedora 39

Table schema and Model

CREATE TABLE [dbo].[NewTable] (
    [column_1] INT NOT NULL,
    [column_2] INT NOT NULL,
    CONSTRAINT [PK_NewTable] PRIMARY KEY CLUSTERED ([column_1] ASC)
);
GO
CREATE UNIQUE NONCLUSTERED INDEX [Index_NewTable_1]
    ON [dbo].[NewTable]([column_2] ASC)
    INCLUDE([column_1]);
GO

Problem description and steps to reproduce
Running inspectdb I get:

$ python manage.py inspectdb NewTable
...
class Newtable(models.Model):
    column_1 = models.IntegerField(primary_key=True)
    column_2 = models.IntegerField()

    class Meta:
        managed = False
        db_table = 'NewTable'
        unique_together = (('column_2', 'column_1'),)

Expected behavior and actual behavior
The expected result is a unique constraint only in column2. Unique together doesn't really make sense because column_1 is already unique.
For example [(1, 1), (1, 2)] fulfill the unique together constraint but they can't be inserted in my table because column_2 is not unique.
The expected output is:

class Newtable(models.Model):
    column_1 = models.IntegerField(primary_key=True)
    column_2 = models.IntegerField(unique=True)

    class Meta:
        managed = False
        db_table = 'NewTable'
mShan0 commented

Thanks for the report. We will look into this.