Я просто создаю или обновляю таблицу, имеющую связь «многие ко многим» с другой таблицей, например, здесь: Company <-> Industry <-> CompanyIndustryRelation.
Industry. js
'use strict';
module.exports = (sequelize, DataTypes) => {
const Industry = sequelize.define('Industry', {
industry_name: DataTypes.STRING,
}, {
timestamps: false,
underscored: true,
tableName: 'industry',
});
Industry.associate = function(models) {
Industry.belongsToMany(models.Company, {
through: 'company_industry_relation', foreignkey: 'industry_id'
});
};
return Industry;
};
Company. js
'use strict';
module.exports = (sequelize, DataTypes) => {
const Company = sequelize.define('Company', {
company_name: DataTypes.STRING,
}, {
timestamps: false,
underscored: true,
tableName: 'company',
});
Company.associate = function(models) {
Company.belongsToMany(models.Industry, {
through: 'company_industry_relation', foreignKey: 'company_id'
});
};
return Company;
};
CompanyIndustryRelation. js
'use strict';
module.exports = (sequelize, DataTypes) => {
const CompanyIndustryRelation = sequelize.define('CompanyIndustryRelation', {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
},
}, {
timestamps: false,
underscored: true,
tableName: 'company_industry_relation',
});
return CompanyIndustryRelation;
};
В настоящее время таблица отрасли уже построена, как показано ниже. ![enter image description here](https://i.stack.imgur.com/KLxYn.png)
Промышленность отраслевых массивов = [{label: 'Accounting'}, {label: 'Computer Science'}]
CompanyName: 'ApolloIT'
Я хочу создать новую запись о компании с указанным отраслевым массивом и companyName.
Заранее спасибо!