I need to store 2 records which relates to each other. Table1 has relation to Table2 and at the same time Table2 has relation to Table1
The code of creation and storing records:
const account = this.accountRepository.create({
// ...
});
const user = this.authUserRepository.create({
account,
// ...
});
account.primary_user = user;
await Promise.all([
this.accountRepository.save(account),
this.authUserRepository.save(user),
]);
Thats how entities look like:
// auth-user.entity.ts
@Entity({ name: 'auth_user' })
export class AuthUser {
@PrimaryGeneratedColumn('increment')
public id: number;
@OneToOne(() => Account, { nullable: true })
@JoinColumn({ name: 'account_id', referencedColumnName: 'id' })
public account: Account;
}
// account.entity.ts
@Entity({ name: 'account' })
export class Account {
@PrimaryGeneratedColumn('increment')
public id: number;
@OneToOne(() => AuthUser, {
nullable: true,
onDelete: 'SET NULL',
onUpdate: 'CASCADE',
})
@JoinColumn({ name: 'primary_user_id', referencedColumnName: 'id' })
public primary_user: AuthUser;
}
In DB I see that no any of related ids are filled: account.primary_user_id is null and auth_user.account_id is null
PS: I am not the person who designed this DB structure I must follow it