gpt4 book ai didi

sql - 如何编写可能来自不同表的 "belongs to"关联?

转载 作者:行者123 更新时间:2023-12-03 22:37:49 27 4
gpt4 key购买 nike

如何为属于模型 B 或模型 C 但不属于两者的模型 A 编写关联?

假设我有一个员工模型、承包商模型和一个事件模型。关联如下:
员工有很多事件。
承包商有许多事件。
事件属于承包商或员工。

我是否创建了一个连接表employee_contractor 并说事件属于employee_contractor?

这可能非常简单,但是对于 Sequelize/DB 编程来说非常陌生,我很难理解何时使用以及使用什么。我知道可能有这类问题的答案,但我不知道用什么语言来正确地表达我的问题以找到它们。

// 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;
};

最佳答案

在这种情况下,我建议将事件拆分为两个不同的事件:employeeEventsContractorEvents(以便在以后查询操作时更容易地利用 Sequelize 关联),并在每种类型的事件上使用简单的 belongsTo,对 Actor 使用 hasMany
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;
};

关于sql - 如何编写可能来自不同表的 "belongs to"关联?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56141838/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com