【发布时间】:2020-01-01 16:58:02
【问题描述】:
给定以下关系:
@Entity({name: 'accounts'})
export class Account {
@PrimaryGeneratedColumn('uuid')
id: string;
@OneToOne(type => Address, address => address.id)
@JoinColumn({name: 'address_id'})
address: Address;
@Column()
name: string;
}
和地址关系:
@Entity({name: 'addresses'})
export class Address {
@PrimaryGeneratedColumn('uuid')
id: string;
@Column({length: 45})
country: string;
}
当我通过这个获得account 实体时:
/**
* Gets account by haccount ID with ALL relations
* @param accountId The account ID
*/
public async getAccountByAccountIdWithRelations(accountId: string): Promise<Account> {
return await this.findOneOrFail({id: accountId}, {relations: ['address']});
}
我得到了完整的 Account 实体,其中包含 Address 关系。
然后当我执行以下操作时:
account.address.country = 'newcountry';
然后在accountRepository 中执行this.save(account),地址根本不会更新!
当我在保存前做控制台日志时,我看到 account 实体的地址已更新,所以这真的很奇怪!
为什么会这样?
注意:所有查询都在一个事务中完成;我不知道这是否重要
【问题讨论】:
标签: postgresql typescript nestjs typeorm