nodejs和mysql数据库关联序列化错误


User.hasMany(Assignment, { as: 'assignments', foreignKey: 'tutorId' });
Assignment.belongsTo(User, { as: 'tutor', foreignKey: 'tutorId' });
Assignment.belongsToMany(User, {
as: 'students',
through: 'studentAssignment',
foreignKey: 'assignmentId',
});
User.belongsToMany(Assignment, {
as: 'assignments',
through: 'studentAssignment',
foreignKey: 'studentId',
});
Submission.belongsTo(User, { as: 'student', foreignKey: 'studentId' });
Submission.belongsTo(Assignment, {
as: 'assignment',
foreignKey: 'assignmentId',
});

throw new AssociationError(You have used the alias ${options.as} in two separate associations. Aliased associations must have unique aliases.);^

AssociationError [SequelizeAssociationError]:您已经使用了别名在两个独立的关联中的赋值。别名关联必须拥有独特的别名。(C:UsersUserOneDriveDesktopToddle appnode_modulessequelizelibassociationsbase.js:13:13)(C:UsersUserOneDriveDesktopToddle appnode_modulessequelizelibassociations belong_to -many.js:33:5)在函数。belongsToMany (C:UsersUserOneDriveDesktopToddle appnode_modulessequelizelibassociationsmixin.js:43:25)在对象。用户(C: 用户桌面 OneDrive app toddleapp.js散步:76:8)在模块。_compile(节点:内部/模块/cj/装载机:1126:14)在Object.Module._extensions. js (node:internal/modules/cjs/loader:1180:10)在模块。负载(节点:内部/模块/cj/装载机:1004:32)在Function.Module。_load(节点:内部/模块/cj/装载机:839:12)在函数。executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)主要节点:内部//run_main_module: 17:47

我正在尝试关联,然后我得到这个错误

您使用相同的assignments别名命名了两个关联:

User.hasMany(Assignment, { as: 'assignments', foreignKey: 'tutorId' });
User.belongsToMany(Assignment, {
as: 'assignments',
through: 'studentAssignment',
foreignKey: 'studentId',
});

只需重命名其中一个:

User.hasMany(Assignment, { as: 'tutorAssignments', foreignKey: 'tutorId' });
User.belongsToMany(Assignment, {
as: 'assignments',
through: 'studentAssignment',
foreignKey: 'studentId',
});

最新更新