Skip to content

Include Where Cause Wrong Query Generated Only In MSSQL Dialect #9008

Closed

Description

@lerit

What are you doing?

two model, one relate to another,i want to query with some condition in associate model
LoginLog=sequelize.define( 'LoginLog', { ID: { field: 'id', type: DataTypes.INTEGER(11), primaryKey: true, autoIncrement: true }, UserID: { field: 'userid', type: DataTypes.UUID, allowNull: false } }, { classMethods: { associate(models) { this.belongsTo(models.User, { foreignKey: 'UserID' }) } } } )

User=sequelize.define( 'User', { UserID: { field: 'userid', type: DataTypes.UUID, primaryKey: true }, UserName: { field: 'username', type: DataTypes.STRING(50), allowNull: false } } )

`loginModel.findAll({
      where: {},
      include: [
        {
          model: User,
          where: {
            UserName: { $like: '%Bob%' }
          }
        }
      ],
      order: [[User, 'UserName', 'DESC']],
      offset: 10,
      limit: 10
    })`

What do you expect to happen?

generate correct sql and query result

What is actually happening?

_But the output was:
two order by was generate,and throw error from sqlserver

_Output:
SELECT [ LoginLog ].[ id ] AS [ ID ], [ LoginLog ].[ userid ] AS [ UserID ],[ USER ].[ userid ] AS [ USER.UserID ], [ USER ].[ username ] AS [ USER.UserName ] FROM [ public_loginlog ] AS [ LoginLog ] INNER JOIN [ public_user ] AS [ USER ] ON [ LoginLog ].[ userid ] = [ USER ].[ userid ] AND ( [ USER ].[ username ] LIKE N '%Bob%' ) ORDER BY [ USER ].[ username ] ORDER BY [ LoginLog ].[ id ] OFFSET 10 ROWS FETCH NEXT 10 ROWS ONLY;

__Dialect:mssql
__Dialect version:tedious 2.3.1
__Database version:sqlserver 2014
__Sequelize version:4.32.7
__Tested with latest release:Yes

i find this appear when:
1.only in mssql.postgresql and mysql work fine!
2.only when use pagination:limit and offset
3.only when where in include
maybe cause from mssql dialect query-generator.js#L837
some problem like this problem,and not solve by now
#8804
i think my problem give an easiest example of reproduction,please help me!

Activity

  1. changed the title [-]Include Where Cause Wrong Query Generated Only In MSSQL DIalect[/-] [+]Include Where Cause Wrong Query Generated Only In MSSQL Dialect[/+] on Feb 7, 2018
  2. sushantdhiman commented on Feb 9, 2018

    @sushantdhiman
    Contributor
  3. balazsnemeth commented on Mar 8, 2018

    @balazsnemeth
  4. lerit commented on Mar 9, 2018

    @lerit
    Author

    this is the sscce code,but i found this has no mssql dialect command in package.json script section.

    `'use strict';

    /*

    • Copy this file to ./sscce.js
    • Add code from issue
    • npm run sscce-{dialect}
      */

    const Sequelize = require('./index');
    const sequelize = require('./test/support').createSequelizeInstance();

    const LoginLog = sequelize.define('LoginLog', {
    ID: {
    field: 'id',
    type: DataTypes.INTEGER(11),
    primaryKey: true,
    autoIncrement: true
    },
    UserID: {
    field: 'userid',
    type: DataTypes.UUID,
    allowNull: false
    }
    });

    const User = sequelize.define('User', {
    UserID: {
    field: 'userid',
    type: DataTypes.UUID,
    primaryKey: true
    },
    UserName: {
    field: 'username',
    type: DataTypes.STRING(50),
    allowNull: false
    }
    });

    LoginLog.belongsTo(User, {
    foreignKey: 'UserID',
    constraints: false
    });

    function problemQuery() {
    return LoginLog.findAll({
    where: {},
    include: [{
    model: User,
    where: {
    UserName: {
    $like: '%Bob%'
    }
    }
    }],
    order: [
    [User, 'UserName', 'DESC']
    ],
    offset: 10,
    limit: 10
    });
    }

    sequelize.sync({
    force: true
    })
    .then(() => problemQuery());`

  5. lerit commented on Mar 9, 2018

    @lerit
    Author

    @sushantdhiman please reopen this issue and help me ,thanks!

  6. added
    type: bug
    on Mar 9, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions