Foreign key value is null
See original GitHub issueHi! 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:
- Created 4 years ago
- Comments:9 (5 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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 fromuser.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).Yes, this is by design.