[英]How to do a Many to Many relationship in Sequelize?
這些是我的模型:員工
const { DataTypes } = require('sequelize');
const sequelize = require('../sequelize')
const Staff = sequelize.define('Staff', {
name: {
type: DataTypes.STRING,
allowNull: false,
},
SSN: {
type: DataTypes.INTEGER,
},
email:{
type: DataTypes.STRING,
}
}, {
timestamps: true
});
Staff.associate = function (models) {
Staff.belongsToMany(models.Technology, {through: models.StaffTechnology});
};
module.exports = Staff;
技術:
const { DataTypes } = require('sequelize');
const sequelize = require('../sequelize')
const Technology = sequelize.define('Technology', {
name: {
type: DataTypes.STRING,
allowNull: false,
},
description: {
type: DataTypes.STRING,
}
}, {
timestamps: true
});
Technology.associate = function (models) {
Technology.belongsToMany(models.Staff, { through: models.StaffTechnology});
};
module.exports = Technology;
這將是連接表:
const { DataTypes } = require('sequelize');
const sequelize = require('../sequelize')
const Staff = require('../../database/models/staff.model');
const Technology = require('../../database/models/technology.model');
const StaffTechnology = sequelize.define('StaffTechnology', {
experience: {
type: DataTypes.INTEGER,
},
StaffID: {
type: DataTypes.INTEGER,
references: {
model: Staff,
key: 'id'
}
},
TechnologyID: {
type: DataTypes.INTEGER,
references: {
model: Technology,
key: 'id'
}
},
}, {
timestamps: true
});
StaffTechnology.associate = function (models) {
//StaffTechnology.hasMany(models.Staff, { foreignKey: 'StaffID' });
//StaffTechnology.hasMany(models.Technology, { foreignKey: 'TechnologyID' });
};
module.exports = StaffTechnology
現在我不能做 Staff.findAll({include:Technology}),因為它給我一個錯誤,說 Staff 和 Technology 沒有關聯,但我看到了 Sequelize 文檔,我看到了一個非常相似的例子。 我想要的是返回 All Staffs 及其技術的可能性,Staff 可以擁有許多技術,而技術可以屬於許多 Staffs,但實際上它們是通過 StaffTechnology 連接表連接的。
您需要在 index.js 中聲明關聯(或在您初始化數據庫的位置)。 您可以通過要求模型並直接聲明關聯來手動完成
Model1.association(Model2, {
through: "JoinTable"
}
Model2.association(Model1, {
through: "JoinTable"
}
或者以編程方式執行(如果您已經在使用該方法來初始化您的數據庫):
'use strict'
const fs = require('fs');
const path = require('path');
const Sequelize = require('sequelize');
const basename = path.basename(__filename);
const config = {
"username": "root",
"password": "YOUR ROOT PASSWORD HERE",
"database": "YOUR DATABASE NAME HERE",
"host": "127.0.0.1",
"dialect": "mysql"
}
const db = {}
let sequelize = new Sequelize(config.database, config.username, config.password, config);
fs.readdirSync(__dirname)
.filter(file => {
return (file.indexOf('.') !== 0)
&& (file !== basename)
&& (file.slice(-3) === '.js');
})
.forEach(file => {
const model = require(path.join(__dirname, file))(sequelize, Sequelize.DataTypes);
db[model.name] = model;
});
Object.keys(db).forEach(modelName => {
if (db[modelName].associate) {
db[modelName].associate(db);
}
});
db.sequelize = sequelize;
db.Sequelize = Sequelize;
module.exports = db;
^ 此代碼將初始化和關聯合二為一。 您需要確保您的 model 文件和初始化文件在同一個文件夾中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.