I have these models:
module.exports = function(sequelize, DataTypes) {
const Tool = sequelize.define('Tool', {
Price: {type: DataTypes.STRING(255)},
PurchaseDate: DataTypes.DATE,
});
return Tool;
};
module.exports = function(sequelize, DataTypes) {
const RepairOrder = sequelize.define('RepairOrder', {
OrderNumber: {type: DataTypes.STRING(255)}
}, {
classMethods: {
associate(models) {
RepairOrder.belongsTo(models.Tool, {as: 'Tool'});
}
}
});
return RepairOrder;
};
module.exports = function(sequelize, DataTypes) {
const Hammer = sequelize.define('Hammer', {
Color: {type: DataTypes.STRING(255)}
},
{
classMethods: {
associate(models) {
Hammer.belongsTo(models.Tool, {as: 'Tool'});
}
}
});
return Hammer;
};
module.exports = function(sequelize, DataTypes) {
const Spanner = sequelize.define('Spanner', {
Size: {type: DataTypes.STRING(255)}
},
{
classMethods: {
associate(models) {
Hammer.belongsTo(models.Tool, {as: 'Tool'});
}
}
});
return Spanner;
};
Because of the belongsTo methods Hammer and Spanner have a ToolID column. Now I want to receive all Repair Orders with the Tool information, so I join the tools into the Repair Orders.
RepairOrder.findAll({
attributes: ['ID', 'OrderNumber'],
include: [{
model: Tool, as: 'Tool'
}]
});
But I also want to know which type(Hammer or Spanner) of tool it is when selecting the Repair Order, so how can I join both into the Tool? Something like
RepairOrder.findAll({
attributes: ['ID', 'OrderNumber'],
include: [{
model: Tool, as: 'Tool',
include: [{'JOIN ON "Hammer"."ToolID" = "Tool"."ID"'},
{'JOIN ON "Spanner"."ToolID" = "Tool"."ID"'}]
}]
});
via frichter
No comments:
Post a Comment