当用户被删除时,我尝试删除用户的个人资料。但它并没有删除个人资料上的任何内容。
用户实体
@Entity()
export class User {
@PrimaryGeneratedColumn()
id: number;
@Column()
name: string;
@OneToOne(type => Profile, profile => profile.user,
{ eager: true, cascade: true, onDelete: 'CASCADE' })
@JoinColumn()
profile: Profile;
}
配置文件实体
@Entity()
export class Profile {
@PrimaryGeneratedColumn()
id: number;
@Column()
gender: string;
@Column()
photo: string;
@OneToOne(type => User, user => user.profile)
user: User;
}
我如何删除用户。
const userRepository = connection.getRepository(User);
const user = await userRepository.findOne({ where: {id: 'someuserId'}, relations: ["profile"] });
await user.remove()
用户已被删除,但用户的个人资料上没有任何反应。
谁能解释如何删除一对一的关系?
User
@Entity()
export class User {
@PrimaryGeneratedColumn()
id: number;
@Column()
name: string;
@OneToOne(type => Profile, profile => profile.user,
{ eager: true, onDelete: 'CASCADE' })
@JoinColumn()
profile: Profile;
}
Profile
@Entity()
export class Profile {
@PrimaryGeneratedColumn()
id: number;
@Column()
gender: string;
@Column()
photo: string;
@OneToOne(type => User, user => user.profile, { onDelete: "CASCADE" })
user: User;
}
我运行了这段代码来从两个实体中删除条目。
const userRepository = connection.getRepository(User);
const user = await userRepository.findOne({ where: {id: 'someuserId'} });
const profileRepository = connection.getRepository(Profile);
const profile = await profileRepository.findOne({ where: {id: user.profile.id} });
await profileRepository.remove(profile)
这是一对一关系的预期行为。您必须删除引用端才能使级联删除生效。讨论link https://github.com/typeorm/typeorm/issues/1460#issuecomment-439996651
edit:提出问题github https://github.com/typeorm/typeorm/issues/6764
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)