我有两个模型:文件和课程。文件属于 Course 并且有一个 CourseId 字段。
在某些时候,我需要在插入它们之前使用事务来验证它们,但是,我的文件模型验证规则之一验证是否存在通知的 CourseId(这在我不使用事务的其他情况下很有用)和这个规则在交易期间被标记,因为在交易期间没有 CouseId,正如我们所预料的那样。
models.sequelize.transaction(function (t) {
// if we want to insert a new course
if (formData.courseId == 0) {
return models.course.create({
name: formData.courseName,
fieldOfStudy: formData.fieldOfStudyId
}, {transaction: t}).then(function(course) {
return models.file.create({
name: formData.name,
universityId: formData.universityId,
status: 1,
type: formData.typeId,
createdBy: userId,
file_raw: files,
courseId: course.id // here is the problem!
}, {transaction: t});
});
// if we want to use a existing one
} else {
return models.file.create({
name: formData.name,
universityId: formData.universityId,
courseId: formData.courseId, …
Run Code Online (Sandbox Code Playgroud)