gpt4 book ai didi

node.js - Sequelize.js如何在表之间建立多对多关系?

转载 作者:搜寻专家 更新时间:2023-11-01 00:38:37 26 4
gpt4 key购买 nike

我想通过使用 Sequelize.js 在 Events 表和 Users 表之间建立多对多关系。一个用户可以参加更多的事件,一个事件有更多的参与者。因此,我带来了一个 event_user 的中间表并创建了它的模型。

以下是所有 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: 'whatever@gmail.com',
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 实例。您需要创建一个文件,在其中创建 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 } );

关于node.js - Sequelize.js如何在表之间建立多对多关系?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42790772/

26 4 0
文章推荐: swift - swift 抛出可选错误最干净的方法是什么?
文章推荐: php - 从 php 中的
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com