我想通過使用Sequelize.js在Events表和Users表之間建立多對多關係。用戶可以參與更多的活動,一個活動可以有更多的參與者。因此,我帶了一個event_user的中間表,並創建了它的模型。如何使用Sequelize.js建立表之間的多對多關係?
這裏是所有3種型號:
我users_model.js:
var EventUser = require('./event_user_model.js');
//_______________________Init & Config Sequelize__________________
const Sequelize = require("sequelize");
const sequelize = new Sequelize('db', 'root', '', {
host: 'localhost',
dialect: 'mysql',
pool: {
max: 5,
min: 0,
idle: 10000
}
});
//____________Declare table structure ________________________
var User = sequelize.define('user', {
id: {
type: Sequelize.STRING,
primaryKey: true,
},
password: {
type: Sequelize.STRING,
},
email: {
type: Sequelize.STRING
},
user_id: { //Foreign Key. Do I even need to put it here at all?
type: Sequelize.INTEGER,
references: {
model: "events_user",
key: "user_id",
}
},
}, {
freezeTableName: true
});
//__________________Establish relationships with other tables_________
User.belongsTo(EventUser,{foreignKey:user_id});
//_____________________________________________________________________
User.sync({force:true}).then(function() {
return User.create({
id:'ORD0',
password: 'mypass',
email: '[email protected]',
user_id:1
});
}).then(c => {
console.log("User Created", c.toJSON());
})
.catch(e => console.error(e));
//______________________________________________________________________
module.exports = User;
我events_model.js:
var EventUser = require('./event_user_model.js');
//______________Init & Config Sequelize_________
const Sequelize = require("sequelize");
const sequelize = new Sequelize('millesime_admin', 'root', '', {
host: 'localhost',
dialect: 'mysql',
pool: {
max: 5,
min: 0,
idle: 10000
}
});
//______________________Declare table structure __________________
var Event = sequelize.define('event', {
eventid: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true,
},
date: {
type: Sequelize.DATE
},
title: {
type: Sequelize.STRING,
},
event_id: { //Foreign Key . Do I even need to put it here at all?
type: Sequelize.INTEGER,
references: {
model: "event_user",
key: "event_id" // Surely here I do something wrong!!
}
},
}, {
freezeTableName: true
});
//____________Establish relationships with other tables_________
Event.belongsTo(EventUser, {foreignKey: event_id});
//________________________Create table____________
Event.sync().then(function() {
return Event.create({
eventid:1,
title: 'Event1',
date: new Date(24, 9, 2016),
event_id: 1,
});
}).then(c => {
console.log("Created event", c.toJSON());
}).catch(e => console.error(e));
//________________________________________________________
module.exports = Event;
而且我event_user_model.js:
var User = require('./users_model.js');
var Event= require('./events_model.js');
//______________________________Initialize & Config Sequelize__________________
const Sequelize = require("sequelize");
const sequelize = new Sequelize('millesime_admin', 'root', '', {
host: 'localhost',
dialect: 'mysql',
pool: {
max: 5,
min: 0,
idle: 10000
}
});
//_______________Declare table structure ________________________
var EventUser = sequelize.define('eventuser', {
eventuserid: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true,
},
user_id: {
type: Sequelize.INTEGER,
references: {
model: "users",
key: "id",
}
},
event_id: {
type: Sequelize.INTEGER,
references: {
model: "event",
key: "eventid",
}
},
reservationConfirmation:{
type:Sequelize.BOOLEAN,
},
attendance:{
type:Sequelize.BOOLEAN
},
},
{
freezeTableName: true
});
//___________________Establish relationships with other tables_________
EventUser.hasMany(User, {foreignKey:id});
//EventUser.hasMany(Event, {forignKey:eventid});
//__________________________Create table_______________________
EventUser.sync(/*{force: true}*/).then(function() {
return EventUser.create({
eventuserid:1,
event_id:1,
user_id: 1,
reservationConfirmation: true,
attendance: true
});
}).then(c => {
console.log("Created", c.toJSON());
}).catch(e => console.error(e));
//___________________________________________________
module.exports = EventUser;
謝謝你的回答!我遵循了關於Sequelize初始化的建議。但是,我自己也注意到,使用自定義id(例如:eventid)並不是Sequelize友好的(或者是,但我不知道如何正確處理它)。我用EventUser.belongsTo(Event);只在我的event_user_model。JS。現在這種關係已經確立。再一次感謝你! :) –
'eventId'主鍵有什麼問題? – piotrbienias
@piotrbienias我有[類似的問題](http://stackoverflow.com/q/42850631/1420197) - 也許你可以幫忙。我和艾瑪仍在努力理解如何使用Sequelize實現這種多對多關係。如果你發佈了一個很好的答案,我也可以授予獎勵! –