fix(backend): Foreign constraint probleem bij het verwijderen van leerpaden opgelost

This commit is contained in:
Gerald Schmittinger 2025-05-13 22:51:16 +02:00
parent 63cf60409f
commit 9400b7f33c
7 changed files with 20 additions and 16 deletions

View file

@ -9,6 +9,7 @@ export class Attachment {
@ManyToOne({
entity: () => LearningObject,
primary: true,
deleteRule: 'cascade'
})
learningObject!: LearningObject;

View file

@ -1,6 +1,5 @@
import {
ArrayType,
Cascade,
Collection,
Embedded,
Entity,
@ -93,8 +92,7 @@ export class LearningObject {
@OneToMany({
entity: () => Attachment,
mappedBy: 'learningObject',
cascade: [Cascade.ALL]
mappedBy: 'learningObject'
})
attachments: Collection<Attachment> = new Collection<Attachment>(this);

View file

@ -72,8 +72,6 @@ const learningObjectService = {
learningObject.hruid = getEnvVar(envVars.UserContentPrefix) + learningObject.hruid;
}
await learningObjectRepository.getEntityManager().flush();
// Lookup the admin teachers based on their usernames and add them to the admins of the learning object.
const teacherRepo = getTeacherRepository();
const adminTeachers = await Promise.all(
@ -85,7 +83,13 @@ const learningObjectService = {
}
});
await learningObjectRepository.save(learningObject, {preventOverwrite: true});
try {
await learningObjectRepository.save(learningObject, {preventOverwrite: true});
} catch (e: unknown) {
learningObjectRepository.getEntityManager().clear();
throw e;
}
return learningObject;
},