question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Foreign key value is null

See original GitHub issue

Hi! I have a problem: When I create ManyToOne relationship by @ManyToOne decotator, I want to select foreign key value.

But on field select result is null. How can i get a foreign key value? I`m tryng to use something like this:

@Field(() => User) @ManyToOne(() => User, { nullable: true }) user?: User;

@Field() @Property({ hidden: false, fieldName: “user_id”, unsigned: true }) userId: number; // result is NULL but entity exist (in db FK has value) and other fields has values

Thanks for help!

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

4reactions
B4nancommented, Feb 25, 2020

Actually there is quite easy way to achieve this, not sure why it did not pop up on my mind before. You can define the userId property as getter that will read the value from user.id:

@ManyToOne(() => User, { nullable: true })
user?: User;

@Property({ persist: false })
get userId(): number {
  return this.user?.id;
}

This is called virtual property in MikroORM: https://mikro-orm.io/docs/defining-entities#virtual-properties

If you want to be able to set to that property, it is a bit more tricky, as you need to set instance of the target entity to the user variable. It can be done via internal entity manager reference if the parent entity is loaded from database (and therefore managed).

@ManyToOne(() => User, { nullable: true })
user?: User;

@Property({ persist: false })
get userId(): number {
  return this.user?.id;
}

set userId(value: number): void {
  this.user = wrap(this).__em!.getReference(User, value); // TODO __em can be undefined if entity not managed
}
2reactions
B4nancommented, Feb 25, 2020

Yes, this is by design.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can a foreign key be NULL and/or duplicate? - Stack Overflow
Short answer: Yes, it can be NULL or duplicate. I want to explain why a foreign key might need to be null or...
Read more >
Can a foreign key be null? If yes, what is the situation? - Quora
Yes, a foreign key can be null. When a foreign key's value is not known at the time of record generation then it's...
Read more >
Can I Insert Null values in Foreign Key Constraint? - STechies
Yes. Foreign key can take Null . e.g in Emoloyee table we have employee_id and manager_id where manager_id is foregin key and CEO...
Read more >
5 Maintaining Data Integrity in Database Applications
Foreign keys allow key values that are all NULL , even if there are no matching PRIMARY or UNIQUE keys. By default (without...
Read more >
Can a foreign key contain null or duplicate value?
Yes, Foreign key contains null value or duplicate value. A foreign key containing null values cannot match the values of a parent key, ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found