Skip to content

How to get id from an entity before em.flush() ? #6529

Answered by B4nan
sonicgamer0721 asked this question in Q&A
Discussion options

You must be logged in to vote

oh right, sorry, now i get it. the PK of your Employee is the user entity itself, they share the same PK value:

  orm.em.create(EmployeeRecord, {
    id: 1,
    employee: orm.em.getReference(Employee, 5),
  });

you also need to use the PrimaryKeyProp symbol on the Employee to state on type level that your PK is called user:

@Entity()
class Employee {
  @ManyToOne(() => User, { primary: true })
  user!: User;

  @Property()
  phone!: string;

  [PrimaryKeyProp]?: 'user';
}

Replies: 1 comment 11 replies

Comment options

You must be logged in to vote
11 replies
@B4nan
Comment options

@B4nan
Comment options

@sonicgamer0721
Comment options

@B4nan
Comment options

Answer selected by sonicgamer0721
@sonicgamer0721
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
enhancement New feature or request
2 participants
Converted from issue

This discussion was converted from issue #6527 on March 19, 2025 07:55.