Hi I have the following index.js
var fs = require('fs');
var path = require('path');
var Sequelize = require('sequelize');
var CONFIG = require('../config/config');
var sequelize = new Sequelize(CONFIG.database, CONFIG.user, CONFIG.password, {
host: CONFIG.host,
dialect: CONFIG.dialect,
port: CONFIG.port,
operatorsAliases: Sequelize.Op,
logging: false
});
var db = {};
fs.readdirSync(__dirname)
.filter(function (file) {
return (file.indexOf('.') !== 0) && (file !== 'index.js');
})
.forEach(function (file) {
var model = sequelize.import(path.join(__dirname, file));
db[model.name] = model;
});
Object.keys(db).forEach(function (modelName) {
if ('associate' in db[modelName]) {
db[modelName].associate(db);
}
});
db.sequelize = sequelize;
db.Sequelize = Sequelize;
module.exports = db;
And I would like to include twice a model with different conditions, so I plan to use alias on this case:
module.exports = function (sequelize, DataTypes) {
var TSection = sequelize.define('TSection', {
id: {
type: DataTypes.INTEGER(11),
allowNull: false,
primaryKey: true,
autoIncrement: true
},
name: {
type: DataTypes.STRING(500),
allowNull: true
},
TestId: {
type: DataTypes.INTEGER(11),
allowNull: false,
primaryKey: true,
field: 'test_id',
references: {
model: 'test',
key: 'id'
}
},
},
{
tableName: 't_sections'
});
TSection.associate = function (models) {
TSection.belongsTo(models.Test), {
foreignKey: 'id',
as: 'sections'
},
TSection.hasMany(models.TQuestion), {
foreignKey: 'id'
}
};
return TSection;
};
and
module.exports = function (sequelize, DataTypes) {
var Test = sequelize.define('Test', {
id: {
type: DataTypes.INTEGER(11),
allowNull: false,
primaryKey: true,
autoIncrement: true
},
name: {
type: DataTypes.STRING(500),
allowNull: true,
},
},
{
tableName: 'tests'
})
Test.associate = function (models) {
Test.hasMany(models.TSection), {
foreignKey: 'id'
}
}
return Test;
};
When I try to get the info like:
const test = await Test.findOne({
attributes: ['id', 'name'],
include: [{
model: TSection,
attributes: ['id', 'name'],
}, {
model: TSection,
as: 'sections'
}]
});
I have this error and I don't know what might be causing it: NOTE: I've tried several combinations including and not including the said model
UnhandledPromiseRejectionWarning: Error: Error: SequelizeEagerLoadingError: TSection is associated to Test using an alias. You've included an alias (sections), but it does not match the alias(es) defined in your association (TSections).
I case you are wondering my objective is to count the number of total sections, but to retrieve only one (the one that matches a given condition)
Thanks!
You added extra brackets in associations. They should look like this:
TSection.associate = function (models) {
TSection.belongsTo(models.Test, {
foreignKey: 'id',
as: 'sections'
});
TSection.hasMany(models.TQuestion, {
foreignKey: 'id'
});
};
and
Test.associate = function (models) {
Test.hasMany(models.TSection, {
foreignKey: 'id'
})
}