提问人:sayinmehmet47 提问时间:11/16/2023 更新时间:11/16/2023 访问量:25
为什么typeorm不完全删除表格?
Why typeorm does not delete the table completely?
问:
我有一个项目和projectMember实体。当我删除项目成员时,我希望它完全删除该成员。但似乎我的projectMember没有正确删除。相反,某些列值更改为 NULL。但是我希望在删除表格行后将其删除。这就是删除逻辑的实现方式
projecEntity
@Entity({ name: 'project' })
@Unique('project-organisationId_slug', ['organisation', 'slug'])
export class ProjectEntity
extends AbstractEntity<ProjectId>
implements ProjectEntityModel
{
@Column({ nullable: true })
address?: string;
removeProjectMember(memberId: string) {
if (!this.members) {
this.members = [];
}
if (!this.hasOtherAdministrators(memberId)) {
throw new MemberNotRemovableException(memberId);
}
const projectMemberIndex = this.getProjectMemberIndex(memberId);
this.members.splice(projectMemberIndex, 1);
}
}
projectMemberEntity 项目成员实体
import { ProjectEntity } from './project.entity';
import { UserEntity } from './user.entity';
@Entity({ name: 'project_member' })
@Unique(['project', 'user'])
export class ProjectMemberEntity
extends AbstractEntity
implements ProjectMemberModel
{
@ManyToOne(() => ProjectEntity, (project) => project.members, {
onDelete: 'CASCADE',
})
@Index(['projectId'])
project!: Relation<ProjectEntity>;
@ManyToOne(() => UserEntity, { eager: true })
@JoinColumn()
@Index(['userId'])
user!: Relation<UserEntity>;
@Column({
type: 'enum',
enum: ProjectRole,
default: ProjectRole.Member,
})
role!: ProjectRole;
@Column({ nullable: true })
projectJobTitle?: string;
}
export type ProjectMemberCreate = Omit<ProjectMemberEntity, 'id'>;
这是我从项目中删除成员的函数
async removeProjectMember(
projectIdentifier: ProjectIdentifier,
memberId: string,
organisationIdentifier: OrganisationIdentifier
): Promise<void> {
const member = project.getProjectMember(memberId);
project.removeProjectMember(memberId);
await this.projectRepository.save(project);
await this.projectMemberRepository.delete(memberId);
await this.activityLogWriterService.createFromProject({
project,
currentUserSub: user?.legacyAuth0Id ?? '',
activityUser: member.user,
activity: ProjectActivityLogWriterType.PROJECT_MEMBER_REMOVED,
});
this.eventEmitter?.emit('project.updated', project.id);
}
答: 暂无答案
评论