associations - belongsTo - hasMany - getting instance from belongsTo - sequelize -
provided have following models:
module.exports = function (sequelize, datatypes) { var workingcalendar = sequelize.define('workingcalendar', { date: datatypes.dateonly, isworking: datatypes.boolean, }, { indexes: [{ unique: true, fields: ['periodid', 'date'] }] }, { classmethods: { associate: function (models) { workingcalendar.belongsto(models.period); } } }); return workingcalendar; }; module.exports = function(sequelize, datatypes) { var period = sequelize.define('period', { name: datatypes.string, numberofperiods: datatypes.integer }, { classmethods: { associate: function(models) { period.hasmany(models.workingcalendar); } } }); return period; };
and trying period
through workingcalendar
follows:
return models.workingcalendar .findall({ attributes: [ 'periodid', 'date' ], include: [ { model: models.period } ], group: ['date', 'periodid'] });
i'm getting following error: unhandled rejection error: period not associated workingcalendar!
yet work other way around.
my question: why can't period
through workingcalendar
? , have make sure can?
i have tried putting foreignkey
attribute on association wel as
binding no avail sadly. welcome!
so found it. indexes
should in same object classmethods
wrong
module.exports = function (sequelize, datatypes) { var workingcalendar = sequelize.define('workingcalendar', { date: datatypes.dateonly, isworking: datatypes.boolean, }, { indexes: [{ unique: true, fields: ['periodid', 'date'] }] }, { classmethods: { associate: function (models) { workingcalendar.belongsto(models.period); } } }); return workingcalendar; };
right
module.exports = function (sequelize, datatypes) { var workingcalendar = sequelize.define('workingcalendar', { date: datatypes.dateonly, isworking: datatypes.boolean, }, { indexes: [{ unique: true, fields: ['periodid', 'date'] }], classmethods: { associate: function (models) { workingcalendar.belongsto(models.period); } } }); return workingcalendar; };
Comments
Post a Comment