Marc Rasmussen
Marc Rasmussen

Reputation: 20555

Sequelize where relation.attribute is equal to

I have the following definition:

var Team = sequelize.define('academy_team', {
    id: DataTypes.INTEGER,
    name: DataTypes.STRING,
    academy_id: DataTypes.INTEGER,
    is_complete: DataTypes.INTEGER,
    started: DataTypes.INTEGER
}, {
    freezeTableName: true,
    instanceMethods: {
        startedTeams: function(onSuccess, onError) {
            Team.findAll({
                    include: [{
                        all: true
                    }],
                    where: {
                        model: academy,
                        division_id: 19
                    }
                }, {})
                .ok(onSuccess).error(onError);
        }
    }
});

academy = sequelize.define('academy', {
    id: DataTypes.INTEGER,
    name: DataTypes.STRING,
    organization_id: DataTypes.INTEGER,
    division_id: DataTypes.INTEGER,
    status_id: DataTypes.INTEGER
}, {
    freezeTableName: true,
    instanceMethods: {}
});

With the following relationship:

Team.belongsTo(academy, {foreignKey: 'academy_id'});

Now as you can see from my (failed) attempt i am trying to select the teams where academy.division_id = 19.

However this does not work

I have also tried the following:

Team.findAll({include: [{all: true}], where: {division_id: 19 }}, {})
.ok(onSuccess).error(onError);

Sadly this produced the following sql:

where academy_team.division_id = 19

Upvotes: 0

Views: 1068

Answers (1)

Jan Aagaard Meier
Jan Aagaard Meier

Reputation: 28788

You can't use include all if you need where conditions

Team.findAll({
  include: [
    { model: academy, where: { division_id: 19 }
  ]
});

Upvotes: 2

Related Questions