2017-03-14 178 views
2

我想通過使用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; 

回答

2

一是錯誤的事情是每個模型文件來創建新的Sequelize實例。您需要創建一個創建sequelize實例的單個文件,該實例將在每個模型定義文件中導入。在你的情況下,結構可以如下

- models 
    - sequelize.js 
    - event.js 
    - user.js 
    - eventUser.js 

每個文件應該是這樣的:

// sequelize.js 
var Sequelize = require('sequelize'); 

var sequelize = new Sequelize(
    'millesime_admin', 
    'root', 
    '', 
    { 
     host: 'localhost', 
     dialect: 'mysql', 
     pool: { 
      max: 5, 
      min: 0, 
      idle: 10000 
     } 
    } 
); 

module.exports = sequelize; 

現在,當你有sequelize實例創建,您可以定義模型

// event.js 
var sequelize = require('./sequelize'); 

var Event = sequelize.define(...); 

module.exports = Event; 

// user.js 
var sequelize = require('./sequelize'); 

var User = sequelize.define(...); 

module.exports = User; 

現在您可以創建最後一個模型EventUser以及EventUser之間的多對多關係。這種關係將在eventUser.js文件,其中EventUser模型定義發生

// eventUser.js 
var sequelize = require('./sequelize'); 
var User  = require('./user'); 
var Event  = require('./event'); 

var EventUser = sequelize.define(...); 

User.belongsToMany(Event, { through: EventUser }); 
Event.belongsToMany(User, { through: EventUser }); 
+0

謝謝你的回答!我遵循了關於Sequelize初始化的建議。但是,我自己也注意到,使用自定義id(例如:eventid)並不是Sequelize友好的(或者是,但我不知道如何正確處理它)。我用EventUser.belongsTo(Event);只在我的event_user_model。JS。現在這種關係已經確立。再一次感謝你! :) –

+0

'eventId'主鍵有什麼問題? – piotrbienias

+0

@piotrbienias我有[類似的問題](http://stackoverflow.com/q/42850631/1420197) - 也許你可以幫忙。我和艾瑪仍在努力理解如何使用Sequelize實現這種多對多關係。如果你發佈了一個很好的答案,我也可以授予獎勵! –

0

請參閱屬於許多mixin :「與連接表的多對多關聯」。

例子:

UserProject = sequelize.define('user_project', { 
    role: Sequelize.STRING 
}); 
User.belongsToMany(Project, { through: UserProject }); 
Project.belongsToMany(User, { through: UserProject });  

請參閱該文檔:

你做
+0

謝謝你的答案被創建!我修改了,現在,在我的event_model.js上,我有: User.belongsToMany(Event,{through:EventUser}); 而在我的event_model上,js爲: Event.belongsToMany(User,{through:EventUser}); 我的中間模型: var EventUser = sequelize.define('eventuser',{...}); 但是,不幸的是,它不工作。 –

+0

你是什麼意思,它不工作?發生了一些錯誤或發生了什麼? – piotrbienias

+0

是的,我得到以下錯誤:event.belongsToMany調用的東西不是Sequelize.Model的實例 –

相關問題