How do I write an association for a Model A that belongs to either Model B or Model C but not both?
Say I have a Employee Model, Contractor Model, and an Event Model. Associated as follows:
Employee has many Events.
Contractor has many Events.
Event belongs to either a Contractor or Employee.
Do I create a joined table employee_contractor and say Event belongs to employee_contractor?
This is probably pretty trivial but being very new to Sequelize/DB programming I'm having a hard time understanding when and what to use. I know there are probably answers to this kind of question out there but I dont know the the language to properly word my question to find them.
// Employee model
'use strict';
module.exports = (sequelize, DataTypes) => {
const Employee = sequelize.define('Employee', {
firstName: DataTypes.STRING,
lastName: DataTypes.STRING,
// other fields
//...
}, {});
Employee.associate = function(models) {
// associations can be defined here
};
return Employee;
};
// Contractor model
'use strict';
module.exports = (sequelize, DataTypes) => {
const Contractor = sequelize.define('Contractor', {
firstName: DataTypes.STRING,
lastName: DataTypes.STRING,
company: DataTypes.STRING,
// other fields
//...
}, {});
Contractor.associate = function(models) {
// associations can be defined here
};
return Contractor;
};
// Event Model
'use strict';
module.exports = (sequelize, DataTypes) => {
const Event = sequelize.define('Event', {
reason: DataTypes.STRING,
escort: DataTypes.STRING,
// other fields
//...
}, {});
Event.associate = function(models) {
// associations can be defined here
};
return Event;
};