期望的行为
使用 Epilogue JS 和 Sequelize,我有两个资源,Student
并且Course
,它们具有 m2m 关系。我想创建一个新Student
的学生实例并将其与现有课程相关联。
问题
当我POST
是一名新学生并在有效负载中包含课程信息时,Epilogue/Sequelize 将尝试创建新学生并基于嵌套有效负载创建新课程。这将导致验证错误,因为这些课程已经存在。
代码示例
const Student = db.define('student', {
id: Sequelize.UUID,
name: Sequelize.STRING
});
const Course = db.define('course', {
id: Sequelize.UUID,
name: Sequelize.STRING
});
Student.belongsToMany(Course, {through: 'student_courses'});
Course.belongsToMany(Student, {through: 'student_courses'});
const StudentResource = epilogue.resource({
model: Student,
endpoints: ['/student', '/student/:id'],
include: [{
model: Course
}];
});
我想发布一个新学生并与现有课程相关联,例如:
// POST /users/
{
name: "Sally",
courses: [
{id: 123, name: "math"},
{id: 456, name: "science"}
]
}
但是,Epilogue/Sequelize 将失败,因为这些课程已经存在:
{
"message": "Validation error",
"errors": [
{
"field": "id",
"message": "id must be unique"
}
]
}