Skip to content

fix(mssql/query-generation): fix duplicate columns in ORDER BY statement - #9173

Closed
balazsnemeth wants to merge 1 commit into
sequelize:masterfrom
balazsnemeth:bugfix/mssql-multiple-orderby
Closed

balazsnemeth wants to merge 1 commit into
sequelize:masterfrom
balazsnemeth:bugfix/mssql-multiple-orderby

Conversation

@balazsnemeth

@balazsnemeth balazsnemeth commented Mar 12, 2018 •

Copy link
Copy Markdown

Pull Request check-list

  • Does npm run test or npm run test-DIALECT pass with this change (including linting)?
    The DIALECT=mssql npm run test-docker-integration pass with this change (including linting).
  • Does the description below contain a link to an existing issue (Closes #[issue]) or a description of the issue you are solving?
  • Have you added new tests to prevent regressions?
  • Is a documentation update included (if this change modifies existing APIs, or introduces new ones)?
  • Did you follow the commit message conventions explained in CONTRIBUTING.md?

Description of change

Query:

{
   "include":[
      {
         "as":"Pull",
         "include":[
            {
               "as":"Status",
               "where":{
                  "Name":"New"
               },
               "$disallowAttributes":true
            }
         ],
         "$disallowAttributes":true
      },
      {
         "as":"TimeZone",
         "$disallowAttributes":true
      },
      {
         "as":"CreatedBy",
         "$disallowAttributes":true
      },
      {
         "as":"ModifiedBy",
         "$disallowAttributes":true
      },
      {
         "as":"Owner",
         "$disallowAttributes":true
      }
   ],
   "where":{
      "RowVersion":{
         "gte":2
      }
   },
   "order":[
      [
         {
            "as":"Pull"
         },
         {
            "as":"Status"
         },
         "Name"
      ]
   ],
   "limit":10,
   "offset":0
}

Wrong generated mssql sql:
SELECT [PullDetail].[Id], [PullDetail].[PullId], [PullDetail].[SkuId], [PullDetail].[MoldsPulled], [PullDetail].[UnitWasteCollected], [PullDetail].[JumboBoxesFromSheet], [PullDetail].[BigBoxesFromSheet], [PullDetail].[SmallBoxesFromSheet], [PullDetail].[EachFromSheet], [PullDetail].[JumboBoxes], [PullDetail].[BigBoxes], [PullDetail].[SmallBoxes], [PullDetail].[Each], [PullDetail].[QtyBoxJumbo], [PullDetail].[QtyBoxBig], [PullDetail].[QtyBoxSmall], [PullDetail].[Variance], [PullDetail].[RowVersion], [PullDetail].[TimeZoneId], [PullDetail].[CreatedById], [PullDetail].[CreatedOn], [PullDetail].[CreatedOnServer], [PullDetail].[ModifiedById], [PullDetail].[ModifiedOn], [PullDetail].[ModifiedOnServer], [PullDetail].[OwnerId], [Pull].[Id] AS [Pull.Id], [Pull].[Name] AS [Pull.Name], [Pull].[Day] AS [Pull.Day], [Pull].[Shift] AS [Pull.Shift], [Pull].[StatusId] AS [Pull.StatusId], [Pull].[FileId] AS [Pull.FileId], [Pull].[FileWebUrl] AS [Pull.FileWebUrl], [Pull].[RowVersion] AS [Pull.RowVersion], [Pull].[TimeZoneId] AS [Pull.TimeZoneId], [Pull].[CreatedById] AS [Pull.CreatedById], [Pull].[CreatedOn] AS [Pull.CreatedOn], [Pull].[CreatedOnServer] AS [Pull.CreatedOnServer], [Pull].[ModifiedById] AS [Pull.ModifiedById], [Pull].[ModifiedOn] AS [Pull.ModifiedOn], [Pull].[ModifiedOnServer] AS [Pull.ModifiedOnServer], [Pull].[OwnerId] AS [Pull.OwnerId], [Pull->Status].[Id] AS [Pull.Status.Id], [Pull->Status].[Code] AS [Pull.Status.Code], [Pull->Status].[Name] AS [Pull.Status.Name], [Pull->Status].[TimeZoneId] AS [Pull.Status.TimeZoneId], [Pull->Status].[CreatedById] AS [Pull.Status.CreatedById], [Pull->Status].[CreatedOn] AS [Pull.Status.CreatedOn], [Pull->Status].[CreatedOnServer] AS [Pull.Status.CreatedOnServer], [Pull->Status].[ModifiedById] AS [Pull.Status.ModifiedById], [Pull->Status].[ModifiedOn] AS [Pull.Status.ModifiedOn], [Pull->Status].[ModifiedOnServer] AS [Pull.Status.ModifiedOnServer], [Pull->Status].[OwnerId] AS [Pull.Status.OwnerId], [TimeZone].[Id] AS [TimeZone.Id], [TimeZone].[Name] AS [TimeZone.Name], [TimeZone].[UTCOffset] AS [TimeZone.UTCOffset], [CreatedBy].[Id] AS [CreatedBy.Id], [CreatedBy].[UserName] AS [CreatedBy.UserName], [CreatedBy].[Salary_Value] AS [CreatedBy.Salary_Value], [CreatedBy].[Salary_Base] AS [CreatedBy.Salary_Base], [CreatedBy].[Password] AS [CreatedBy.Password], [CreatedBy].[CurrencyId] AS [CreatedBy.CurrencyId], [CreatedBy].[ExchangeRate] AS [CreatedBy.ExchangeRate], [CreatedBy].[DeletedOn] AS [CreatedBy.DeletedOn], [CreatedBy].[FirstName] AS [CreatedBy.FirstName], [CreatedBy].[LastName] AS [CreatedBy.LastName], [CreatedBy].[RowVersion] AS [CreatedBy.RowVersion], [CreatedBy].[TimeZoneId] AS [CreatedBy.TimeZoneId], [CreatedBy].[CreatedById] AS [CreatedBy.CreatedById], [CreatedBy].[CreatedOn] AS [CreatedBy.CreatedOn], [CreatedBy].[CreatedOnServer] AS [CreatedBy.CreatedOnServer], [CreatedBy].[ModifiedById] AS [CreatedBy.ModifiedById], [CreatedBy].[ModifiedOn] AS [CreatedBy.ModifiedOn], [CreatedBy].[ModifiedOnServer] AS [CreatedBy.ModifiedOnServer], [CreatedBy].[OwnerId] AS [CreatedBy.OwnerId], [ModifiedBy].[Id] AS [ModifiedBy.Id], [ModifiedBy].[UserName] AS [ModifiedBy.UserName], [ModifiedBy].[Salary_Value] AS [ModifiedBy.Salary_Value], [ModifiedBy].[Salary_Base] AS [ModifiedBy.Salary_Base], [ModifiedBy].[Password] AS [ModifiedBy.Password], [ModifiedBy].[CurrencyId] AS [ModifiedBy.CurrencyId], [ModifiedBy].[ExchangeRate] AS [ModifiedBy.ExchangeRate], [ModifiedBy].[DeletedOn] AS [ModifiedBy.DeletedOn], [ModifiedBy].[FirstName] AS [ModifiedBy.FirstName], [ModifiedBy].[LastName] AS [ModifiedBy.LastName], [ModifiedBy].[RowVersion] AS [ModifiedBy.RowVersion], [ModifiedBy].[TimeZoneId] AS [ModifiedBy.TimeZoneId], [ModifiedBy].[CreatedById] AS [ModifiedBy.CreatedById], [ModifiedBy].[CreatedOn] AS [ModifiedBy.CreatedOn], [ModifiedBy].[CreatedOnServer] AS [ModifiedBy.CreatedOnServer], [ModifiedBy].[ModifiedById] AS [ModifiedBy.ModifiedById], [ModifiedBy].[ModifiedOn] AS [ModifiedBy.ModifiedOn], [ModifiedBy].[ModifiedOnServer] AS [ModifiedBy.ModifiedOnServer], [ModifiedBy].[OwnerId] AS [ModifiedBy.OwnerId], [Owner].[Id] AS [Owner.Id], [Owner].[UserName] AS [Owner.UserName], [Owner].[Salary_Value] AS [Owner.Salary_Value], [Owner].[Salary_Base] AS [Owner.Salary_Base], [Owner].[Password] AS [Owner.Password], [Owner].[CurrencyId] AS [Owner.CurrencyId], [Owner].[ExchangeRate] AS [Owner.ExchangeRate], [Owner].[DeletedOn] AS [Owner.DeletedOn], [Owner].[FirstName] AS [Owner.FirstName], [Owner].[LastName] AS [Owner.LastName], [Owner].[RowVersion] AS [Owner.RowVersion], [Owner].[TimeZoneId] AS [Owner.TimeZoneId], [Owner].[CreatedById] AS [Owner.CreatedById], [Owner].[CreatedOn] AS [Owner.CreatedOn], [Owner].[CreatedOnServer] AS [Owner.CreatedOnServer], [Owner].[ModifiedById] AS [Owner.ModifiedById], [Owner].[ModifiedOn] AS [Owner.ModifiedOn], [Owner].[ModifiedOnServer] AS [Owner.ModifiedOnServer], [Owner].[OwnerId] AS [Owner.OwnerId] FROM [CreativeMines].[PullDetail] AS [PullDetail] LEFT OUTER JOIN ( [CreativeMines].[Pull] AS [Pull] INNER JOIN [CreativeMines].[PullStatus] AS [Pull->Status] ON [Pull].[StatusId] = [Pull->Status].[Id] AND [Pull->Status].[Name] = N'New' ) ON [PullDetail].[PullId] = [Pull].[Id] LEFT OUTER JOIN [MetaBase].[TimeZone] AS [TimeZone] ON [PullDetail].[TimeZoneId] = [TimeZone].[Id] LEFT OUTER JOIN [MetaBase].[User] AS [CreatedBy] ON [PullDetail].[CreatedById] = [CreatedBy].[Id] LEFT OUTER JOIN [MetaBase].[User] AS [ModifiedBy] ON [PullDetail].[ModifiedById] = [ModifiedBy].[Id] LEFT OUTER JOIN [MetaBase].[User] AS [Owner] ON [PullDetail].[OwnerId] = [Owner].[Id] WHERE [PullDetail].[RowVersion] >= 2 ORDER BY [Pull->Status].[Name] DESC ORDER BY [PullDetail].[Id] OFFSET 0 ROWS FETCH NEXT 10 ROWS ONLY;

(Observe the two OrderBy clause at the end).

After the fix, the produces SQL:

SELECT [PullDetail].[Id], [PullDetail].[PullId], [PullDetail].[SkuId], [PullDetail].[MoldsPulled], [PullDetail].[UnitWasteCollected], [PullDetail].[JumboBoxesFromSheet], [PullDetail].[BigBoxesFromSheet], [PullDetail].[SmallBoxesFromSheet], [PullDetail].[EachFromSheet], [PullDetail].[JumboBoxes], [PullDetail].[BigBoxes], [PullDetail].[SmallBoxes], [PullDetail].[Each], [PullDetail].[QtyBoxJumbo], [PullDetail].[QtyBoxBig], [PullDetail].[QtyBoxSmall], [PullDetail].[Variance], [PullDetail].[RowVersion], [PullDetail].[TimeZoneId], [PullDetail].[CreatedById], [PullDetail].[CreatedOn], [PullDetail].[CreatedOnServer], [PullDetail].[ModifiedById], [PullDetail].[ModifiedOn], [PullDetail].[ModifiedOnServer], [PullDetail].[OwnerId], [Pull].[Id] AS [Pull.Id], [Pull].[Name] AS [Pull.Name], [Pull].[Day] AS [Pull.Day], [Pull].[Shift] AS [Pull.Shift], [Pull].[StatusId] AS [Pull.StatusId], [Pull].[FileId] AS [Pull.FileId], [Pull].[FileWebUrl] AS [Pull.FileWebUrl], [Pull].[RowVersion] AS [Pull.RowVersion], [Pull].[TimeZoneId] AS [Pull.TimeZoneId], [Pull].[CreatedById] AS [Pull.CreatedById], [Pull].[CreatedOn] AS [Pull.CreatedOn], [Pull].[CreatedOnServer] AS [Pull.CreatedOnServer], [Pull].[ModifiedById] AS [Pull.ModifiedById], [Pull].[ModifiedOn] AS [Pull.ModifiedOn], [Pull].[ModifiedOnServer] AS [Pull.ModifiedOnServer], [Pull].[OwnerId] AS [Pull.OwnerId], [Pull->Status].[Id] AS [Pull.Status.Id], [Pull->Status].[Code] AS [Pull.Status.Code], [Pull->Status].[Name] AS [Pull.Status.Name], [Pull->Status].[TimeZoneId] AS [Pull.Status.TimeZoneId], [Pull->Status].[CreatedById] AS [Pull.Status.CreatedById], [Pull->Status].[CreatedOn] AS [Pull.Status.CreatedOn], [Pull->Status].[CreatedOnServer] AS [Pull.Status.CreatedOnServer], [Pull->Status].[ModifiedById] AS [Pull.Status.ModifiedById], [Pull->Status].[ModifiedOn] AS [Pull.Status.ModifiedOn], [Pull->Status].[ModifiedOnServer] AS [Pull.Status.ModifiedOnServer], [Pull->Status].[OwnerId] AS [Pull.Status.OwnerId], [TimeZone].[Id] AS [TimeZone.Id], [TimeZone].[Name] AS [TimeZone.Name], [TimeZone].[UTCOffset] AS [TimeZone.UTCOffset], [CreatedBy].[Id] AS [CreatedBy.Id], [CreatedBy].[UserName] AS [CreatedBy.UserName], [CreatedBy].[Salary_Value] AS [CreatedBy.Salary_Value], [CreatedBy].[Salary_Base] AS [CreatedBy.Salary_Base], [CreatedBy].[Password] AS [CreatedBy.Password], [CreatedBy].[CurrencyId] AS [CreatedBy.CurrencyId], [CreatedBy].[ExchangeRate] AS [CreatedBy.ExchangeRate], [CreatedBy].[DeletedOn] AS [CreatedBy.DeletedOn], [CreatedBy].[FirstName] AS [CreatedBy.FirstName], [CreatedBy].[LastName] AS [CreatedBy.LastName], [CreatedBy].[RowVersion] AS [CreatedBy.RowVersion], [CreatedBy].[TimeZoneId] AS [CreatedBy.TimeZoneId], [CreatedBy].[CreatedById] AS [CreatedBy.CreatedById], [CreatedBy].[CreatedOn] AS [CreatedBy.CreatedOn], [CreatedBy].[CreatedOnServer] AS [CreatedBy.CreatedOnServer], [CreatedBy].[ModifiedById] AS [CreatedBy.ModifiedById], [CreatedBy].[ModifiedOn] AS [CreatedBy.ModifiedOn], [CreatedBy].[ModifiedOnServer] AS [CreatedBy.ModifiedOnServer], [CreatedBy].[OwnerId] AS [CreatedBy.OwnerId], [ModifiedBy].[Id] AS [ModifiedBy.Id], [ModifiedBy].[UserName] AS [ModifiedBy.UserName], [ModifiedBy].[Salary_Value] AS [ModifiedBy.Salary_Value], [ModifiedBy].[Salary_Base] AS [ModifiedBy.Salary_Base], [ModifiedBy].[Password] AS [ModifiedBy.Password], [ModifiedBy].[CurrencyId] AS [ModifiedBy.CurrencyId], [ModifiedBy].[ExchangeRate] AS [ModifiedBy.ExchangeRate], [ModifiedBy].[DeletedOn] AS [ModifiedBy.DeletedOn], [ModifiedBy].[FirstName] AS [ModifiedBy.FirstName], [ModifiedBy].[LastName] AS [ModifiedBy.LastName], [ModifiedBy].[RowVersion] AS [ModifiedBy.RowVersion], [ModifiedBy].[TimeZoneId] AS [ModifiedBy.TimeZoneId], [ModifiedBy].[CreatedById] AS [ModifiedBy.CreatedById], [ModifiedBy].[CreatedOn] AS [ModifiedBy.CreatedOn], [ModifiedBy].[CreatedOnServer] AS [ModifiedBy.CreatedOnServer], [ModifiedBy].[ModifiedById] AS [ModifiedBy.ModifiedById], [ModifiedBy].[ModifiedOn] AS [ModifiedBy.ModifiedOn], [ModifiedBy].[ModifiedOnServer] AS [ModifiedBy.ModifiedOnServer], [ModifiedBy].[OwnerId] AS [ModifiedBy.OwnerId], [Owner].[Id] AS [Owner.Id], [Owner].[UserName] AS [Owner.UserName], [Owner].[Salary_Value] AS [Owner.Salary_Value], [Owner].[Salary_Base] AS [Owner.Salary_Base], [Owner].[Password] AS [Owner.Password], [Owner].[CurrencyId] AS [Owner.CurrencyId], [Owner].[ExchangeRate] AS [Owner.ExchangeRate], [Owner].[DeletedOn] AS [Owner.DeletedOn], [Owner].[FirstName] AS [Owner.FirstName], [Owner].[LastName] AS [Owner.LastName], [Owner].[RowVersion] AS [Owner.RowVersion], [Owner].[TimeZoneId] AS [Owner.TimeZoneId], [Owner].[CreatedById] AS [Owner.CreatedById], [Owner].[CreatedOn] AS [Owner.CreatedOn], [Owner].[CreatedOnServer] AS [Owner.CreatedOnServer], [Owner].[ModifiedById] AS [Owner.ModifiedById], [Owner].[ModifiedOn] AS [Owner.ModifiedOn], [Owner].[ModifiedOnServer] AS [Owner.ModifiedOnServer], [Owner].[OwnerId] AS [Owner.OwnerId] FROM [CreativeMines].[PullDetail] AS [PullDetail] LEFT OUTER JOIN ( [CreativeMines].[Pull] AS [Pull] INNER JOIN [CreativeMines].[PullStatus] AS [Pull->Status] ON [Pull].[StatusId] = [Pull->Status].[Id] AND [Pull->Status].[Name] = N'New' ) ON [PullDetail].[PullId] = [Pull].[Id] LEFT OUTER JOIN [MetaBase].[TimeZone] AS [TimeZone] ON [PullDetail].[TimeZoneId] = [TimeZone].[Id] LEFT OUTER JOIN [MetaBase].[User] AS [CreatedBy] ON [PullDetail].[CreatedById] = [CreatedBy].[Id] LEFT OUTER JOIN [MetaBase].[User] AS [ModifiedBy] ON [PullDetail].[ModifiedById] = [ModifiedBy].[Id] LEFT OUTER JOIN [MetaBase].[User] AS [Owner] ON [PullDetail].[OwnerId] = [Owner].[Id] WHERE [PullDetail].[RowVersion] >= 2 ORDER BY [Pull->Status].[Name] DESC OFFSET 0 ROWS FETCH NEXT 10 ROWS ONLY;

@balazsnemeth
balazsnemeth force-pushed the bugfix/mssql-multiple-orderby branch from b8c241f to 2c67400 Compare March 12, 2018 13:31
@codecov

codecov Bot commented Mar 12, 2018 •

Copy link
Copy Markdown

Codecov Report

Merging #9173 into master will not change coverage.
The diff coverage is n/a.

@balazsnemeth
balazsnemeth force-pushed the bugfix/mssql-multiple-orderby branch from 2c67400 to f4d97a3 Compare March 12, 2018 13:52
@balazsnemeth

Copy link
Copy Markdown
Author

This should fix #6706 as well

@balazsnemeth

Copy link
Copy Markdown
Author

@sushantdhiman , @hisorange what do you think, how can I improve this?

@sushantdhiman sushantdhiman left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you link me the issue which this PR is trying to fix?

Fix LGTM, but this will need a integration test

@lerit

lerit commented Apr 10, 2018

Copy link
Copy Markdown

i had test,This pull request fix #9008 as well

@lerit

lerit commented Apr 13, 2018

Copy link
Copy Markdown

@sushantdhiman can you please review this pull request,i test this in my project and all work fine,and this pull will not change coverage,anything else we should do to improve this? thanks!

@sushantdhiman

Copy link
Copy Markdown
Contributor

I have reviewed this PR, it needs a test to prevent regression

@balazsnemeth

Copy link
Copy Markdown
Author

@sushantdhiman sorry for bothering you too frequently, and thanks for maintaining this awesome tool. 👍
Previously you wrote: "need a integration test"
Could you please help me on which test do you mean exactly? All the tests are passes, that probably means, we have no regression, am I correct? You meant I should create a test which would fail without this PR to show what is fixed here?

@sushantdhiman

Copy link
Copy Markdown
Contributor

You meant I should create a test which would fail without this PR to show what is fixed here?

Exactly, this will prevent future regressions

  1. Use testcase from Include Where Cause Wrong Query Generated Only In MSSQL Dialect #9008 (comment)
  2. Create regressions.test.js here, take test structure from here maybe

@sushantdhiman

Copy link
Copy Markdown
Contributor

@balazsnemeth Thanks for your fix and @lerit thanks for your SSCCE, I have used both to fix this issue #9307 , this will be released in next v5 beta release

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants