0

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;
};
HexxNine
  • 446
  • 9
  • 23
  • So a single Event cannot belong to multiple Employees, or any Contractor at the same time ? – mohdule May 15 '19 at 08:20
  • @Mohdule correct an Event belongs to only one person and that person can either be a Contractor or Employee. Example of an event would be like. Contractor Mike badged into the server room. Or Employee Dan badged into the Data Center. – HexxNine May 16 '19 at 05:25

1 Answers1

0

In this case i would recommend splitting the event into two distinct events: employeeEvents and ContractorEvents (to make it easier to leverage sequelize associations upon later querying actions), and use a simplebelongsTo on each type of event and hasMany on the actors.

Employee

// 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
    Employee.hasMany(models.EmployeeEvent);
  };
  return Employee;
};

EmployeeEvents

// EmployeeEvent Model
'use strict';
module.exports = (sequelize, DataTypes) => {
  const EmployeeEvent = sequelize.define('Event', {
    reason: DataTypes.STRING,
    escort: DataTypes.STRING,
    // other fields
    //...

  }, {});
  EmployeeEvent.associate = function(models) {
    // associations can be defined here
    EmployeeEvent.belongsTo(models.Employee);
  };
  return EmployeeEvent;
};

Contractor

// 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
    Contractor.hasMany(models.ContractorEvent);
  };
  return Contractor;
};

ContractorEvents

// ContractorEvent Model
'use strict';
module.exports = (sequelize, DataTypes) => {
  const ContractorEvent = sequelize.define('Event', {
    reason: DataTypes.STRING,
    escort: DataTypes.STRING,
    // other fields
    //...

  }, {});
  ContractorEvent.associate = function(models) {
    // associations can be defined here
    ContractorEvent.belongsTo(models.Employee);
  };
  return ContractorEvent;
};
mohdule
  • 528
  • 7
  • 16