Sequelize.jsを使用して、イベントテーブルとユーザーテーブルの多対多関係を確立したいとします。ユーザはより多くのイベントに参加することができ、1つのイベントはより多くの参加者を有する。したがって、私はevent_userの中間テーブルを持ってきて、そのモデルを作成しました。Sequelize.jsでテーブル間の多対多の関係を確立するにはどうすればいいですか?
マイ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;
ここ
は、すべての3つのモデルです
ありがとうございました!私はSequelizeの初期化に関するあなたの助言に従った。しかし、私自身もカスタマイズしたID(例:eventid)を使用することは、Sequelizeにはあまり適していないことに気付きました(または、それは正しく処理する方法がわかりません)。私はEventUser.belongsTo(Event)を使用しました。私のevent_user_modelでのみ。js。今、関係が確立されました。もう一度ありがとう! :) –
'eventId'プライマリキーの問題は何ですか? – piotrbienias
@piotrbienias私は[同様の質問](http://stackoverflow.com/q/42850631/1420197) - 多分あなたが助けることができます。 MeとEmmaは、Sequelizeを使用してこの多対多の関係を実装する方法を理解するためにまだ苦労しています。あなたが良い答えを投稿した場合、賞金を授与することもできます! –