我想与sequelize js建立三元关联,但我不知道该怎么做。我尝试了两种不同的方法:
此转换导致另一个名为course的表。
这里我将关系的实体调用到结果表中。
FormationModel.js
var FormationModel = sequelize.define('formationModel', {
idFormation: {
type: Sequelize.INTEGER,
field: 'idFormation',
primaryKey: true,
autoIncrement: true
},
name: {
type: Sequelize.STRING,
field: 'name',
allowNull: false
},
}, {
freezeTableName: true,
timestamps: false,
});
FormationModel.sync({
force: false
}).then(function () {
});
module.exports = FormationModel;
LectiveYearModel.js
var LectiveYearModel = sequelize.define('lectiveYearModel', {
lectiveYear: {
type: Sequelize.STRING,
field: 'lectiveYear',
primaryKey: true
}
}, {
freezeTableName: true,
timestamps: false,
});
LectiveYearModel.sync({
force: false
}).then(function () {
});
module.exports = LectiveYearModel;
PeriodModel
var PeriodModel = sequelize.define('periodModel', {
idPeriod: {
type: Sequelize.INTEGER,
field: 'idPeriod',
primaryKey: true,
autoIncrement: true
},
name: {
type: Sequelize.STRING,
field: 'name',
allowNull: false
},
}, {
freezeTableName: true,
timestamps: false,
});
PeriodModel.sync({
force: false
}).then(function () {
});
module.exports = PeriodModel;
CourseModel.js
var CourseModel = sequelize.define('courseModel', {
idPeriod: {
type: Sequelize.INTEGER,
field: 'idPeriod',
references : {
model: PeriodModel,
key: 'idPeriod'
},
primaryKey: true
},
idFormation: {
type: Sequelize.INTEGER,
field: 'idFormation',
references : {
model: FormationModel,
key: 'idFormation'
},
primaryKey: true
},
idLectiveYear: {
type: Sequelize.STRING,
field: 'idLectiveYear',
references : {
model: LectiveYearModel,
key: 'lectiveYear'
},
primaryKey: true
}
}, {
freezeTableName: true, // Model tableName will be the same as the model name
timestamps: false,
});
CourseModel.sync({
force: false
}).then(function () {
});
module.exports = CourseModel;
这几乎是相同的,但我在实体中使用belongsTo。
Formation.js
var Course = require('./Course');
var Formation = sequelize.define('formation', {
id: {
type: Sequelize.INTEGER,
field: 'id',
primaryKey: true,
autoIncrement: true
},
name: {
type: Sequelize.STRING,
field: 'name',
allowNull: false
},
}, {
freezeTableName: true,
timestamps: false,
});
Formation.belongsToMany(Course, {as: 'formationFk', through: 'course', foreignKey: 'idFormation'});
Formation.sync({
force: false
}).then(function () {
});
module.exports = Formation;
LectiveYear.js
var LectiveYear = sequelize.define('lectiveYear', {
lectiveYear: {
type: Sequelize.STRING,
field: 'lectiveYear',
primaryKey: true
}
}, {
freezeTableName: true,
timestamps: false,
});
LectiveYear.belongsToMany(Course, {as: 'lectiveyearFk', through: 'course', foreignKey: 'idLectiveYear'});
LectiveYear.sync({
force: false
}).then(function () {
});
module.exports = LectiveYear;
Period.js
var Period = sequelize.define('period', {
idPeriod: {
type: Sequelize.INTEGER,
field: 'idPeriod',
primaryKey: true,
autoIncrement: true
},
name: {
type: Sequelize.STRING,
field: 'name',
allowNull: false
},
}, {
freezeTableName: true,
timestamps: false,
});
Period.belongsToMany(Course, {as: 'periodFk', through: 'course', foreignKey: 'idPeriod'});
Period.sync({
force: false
}).then(function () {
});
module.exports = Period;
Course.js
var Course = sequelize.define('course', {
idPeriod: {
type: Sequelize.INTEGER,
field: 'idPeriod',
primaryKey: true
},
idLectiveYear: {
type: Sequelize.STRING,
field: 'idLectiveYear',
primaryKey: true
},
idFormation: {
type: Sequelize.INTEGER,
field: 'idFormation',
primaryKey: true
}
}, {
freezeTableName: true,
timestamps: false,
});
Course.sync({
force: false
}).then(function () {
});
module.exports = Course;