我在连接表上设置属性时遇到问题。
我通过自定义表 UserModel
在两个模型 HangModel
和 HangUsers
之间定义了多对多关联。
const HangModel = rootRequire('/models/Hang');
const UserModel = rootRequire('/models/User');
const HangUsers = database.define('HangUsers', {
id: {
type: Sequelize.INTEGER(10).UNSIGNED,
primaryKey: true,
autoIncrement: true,
},
hangId: {
type: Sequelize.INTEGER(10).UNSIGNED,
references: {
model: HangModel,
key: 'id',
},
},
userId: {
type: Sequelize.INTEGER(10).UNSIGNED,
references: {
model: UserModel,
key: 'id',
},
},
rsvp: {
type: Sequelize.STRING,
allowNull: false,
validate: {
isIn: {
args: [ 'pending', 'joined' ],
msg: 'The rsvp provided is invalid',
},
},
},
});
UserModel.hasMany(HangUsers, { as: 'invitations' });
HangModel.hasMany(HangUsers, { as: 'invites' });
UserModel.belongsToMany(HangModel, { through: HangUsers });
HangModel.belongsToMany(UserModel, { through: HangUsers });
通过表有一个列 rsvp
,当我将用户添加到挂起时,我试图填充它:const hang = await HangModel.create();
await hang.addUser(user, { through: { rvsp: 'joined' } });
但是,我收到一个错误:AggregateError
at recursiveBulkCreate (/Users/sgarza62/ditto-app/api/node_modules/sequelize/lib/model.js:2600:17)
at processTicksAndRejections (internal/process/task_queues.js:97:5)
at async Function.bulkCreate (/Users/sgarza62/ditto-app/api/node_modules/sequelize/lib/model.js:2824:12)
at async Promise.all (index 0)
at async BelongsToMany.add (/Users/sgarza62/ditto-app/api/node_modules/sequelize/lib/associations/belongs-to-many.js:740:30)
at async /Users/sgarza62/ditto-app/api/routes/hangs.js:121:3 {
name: 'AggregateError',
errors: [
BulkRecordError [SequelizeBulkRecordError]: notNull Violation: HangUsers.rsvp cannot be null
at /Users/sgarza62/ditto-app/api/node_modules/sequelize/lib/model.js:2594:25
at processTicksAndRejections (internal/process/task_queues.js:97:5) {
name: 'SequelizeBulkRecordError',
errors: [ValidationError],
record: [HangUsers]
}
]
}
当我在 rsvp 列上允许 null 时,会创建 HangUsers
行,但 rsvp 值为 NULL
。{ through: { rsvp: 'joined' } }
参数似乎被忽略了。我已经根据 BelongsToMany docs 和 Advanced M:N Associations docs 完成了这一切,它说:
However, defining the model by ourselves has several advantages. We can, for example, define more columns on our through table:
const User_Profile = sequelize.define('User_Profile', { selfGranted: DataTypes.BOOLEAN }, { timestamps: false }); User.belongsToMany(Profile, { through: User_Profile }); Profile.belongsToMany(User, { through: User_Profile });
With this, we can now track an extra information at the through table, namely the selfGranted boolean. For example, when calling the user.addProfile() we can pass values for the extra columns using the through option.
Example:
const amidala = await User.create({ username: 'p4dm3', points: 1000 }); const queen = await Profile.create({ name: 'Queen' }); await amidala.addProfile(queen, { through: { selfGranted: false } }); const result = await User.findOne({ where: { username: 'p4dm3' }, include: Profile }); console.log(result);
最佳答案
只是属性名称上的一个错字:“rvsp”应该是“rsvp”。
关于node.js - 在 Sequelize 中的连接表上添加属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64722869/