Skip to content

Wrong query result with include association #9228

@ALjean

Description

@ALjean

What are you doing?

Hello.
After last updates sequelize. Tests for my application broke. I have got wrong query result.

The method that produces the wrong result

 async getApplication(product_id) {
            return sequelize.models.Application.findOne({
                include: [{
                    association: sequelize.models.Application.associations.products,
                    where: {id:product_id}
                }]
            });
        }

I have next data in the database:
application_product table data
| id | app_id |
| 14 | 1 |
| 15 | 2 |

application table data
| id | name |
| 1 | test_1 |
| 2 | test_2 |

Models

class Application extends sequelize.Model {
    static associate(models) {
        Application.hasMany(models.ApplicationProduct, {foreignKey: 'app_id', as: 'products'});
    }
}

Application.init({
    id: {
        type: DataTypes.INTEGER.UNSIGNED,
        primaryKey: true,
        autoIncrement: true
    },
//....
});

class ApplicationProduct extends sequelize.Model {

    static associate(models) {
        ApplicationProduct.belongsTo(models.Application, {foreignKey: 'app_id', as: 'application'});
    }
}

ApplicationProduct.init({

    id: {
        type: DataTypes.INTEGER.UNSIGNED,
        primaryKey: true,
        autoIncrement: true
    },
    app_id: {
        type: DataTypes.INTEGER.UNSIGNED,
        allowNull: false
    },

    //....
});

What do you expect to happen?

I wanted get the same result when i set different args.

When i use "sequelize": "=4.36.0" I get a working query.

SELECT `Application`.*, `products`.`id` AS `products.id`, `products`.`app_id` AS `products.app_id`,  FROM `applications` AS `Application` WHERE ( SELECT `app_id` FROM `application_products` AS `products` WHERE (`products`.`app_id` = `Application`.`id` AND `products`.`id` = 15) LIMIT 1 ) IS NOT NULL LIMIT 1) AS `Application` INNER JOIN `application_products` AS `products` ON `Application`.`id` = `products`.`app_id` AND `products`.`id` = 15;

(products.app_id=Application.idANDproducts.id = 15) We have diff

What is actually happening?

I execute query with two different args But I get different results
If product_id = 14 I get the application. But if I try send 15 I don't get any result

SELECT `Application`.*, `products`.`id` AS `products.id`, `products`.`app_id` AS `products.app_id`,  FROM `applications` AS `Application` WHERE ( SELECT `app_id` FROM `application_products` AS `products` WHERE (`products`.`app_id` = `Application`.`id`) LIMIT 1 ) IS NOT NULL LIMIT 1) AS `Application` INNER JOIN `application_products` AS `products` ON `Application`.`id` = `products`.`app_id` AND `products`.`id` = 14
SELECT `Application`.*, `products`.`id` AS `products.id`, `products`.`app_id` AS `products.app_id`,  FROM `applications` AS `Application` WHERE ( SELECT `app_id` FROM `application_products` AS `products` WHERE (`products`.`app_id` = `Application`.`id`) LIMIT 1 ) IS NOT NULL LIMIT 1) AS `Application` INNER JOIN `application_products` AS `products` ON `Application`.`id` = `products`.`app_id` AND `products`.`id` = 15

In first query application will be found but the following calls will give an empty result. Data in the database exists.

Dialect: mysql
Database version: 5.6
Sequelize version: 4.37.4
Tested with latest release: Yes

Note : Your issue may be ignored OR closed by maintainers if it's not tested against latest version OR does not follow issue template.

Metadata

Metadata

Assignees

No one assigned

    Labels

    regressionFor issues and PRs. A bug that does not happen in an earlier version of Sequelize.type: bugDEPRECATED: replace with the "bug" issue type

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions