Unnecessary join: foreign key same as property name
See original GitHub issueDescribe the bug
I am migrating a project where the foreign key in the database is not suffixed with Id
. For example, the database table looks like this:
CREATE TABLE "Profile" (
"id" TEXT NOT NULL ,
"user" TEXT REFERENCES "User"(id) ON DELETE SET NULL,
PRIMARY KEY ("id")
)
The problem is that when you query this class like so:
em.find(Profile, { user: { $in: [userId] }, })
The code below in ObjectCriteriaNode
will trigger. It will see that user
is a non-scalar field and join in the User
table, even though I’m really just trying to query on an indexed foreign key that happens to also be called user
in the database.
export class ObjectCriteriaNode extends CriteriaNode {
static create(metadata: MetadataStorage, entityName: string, payload: Dictionary, parent?: CriteriaNode, key?: string): ObjectCriteriaNode {
const node = new ObjectCriteriaNode(metadata, entityName, parent, key);
const meta = metadata.get(entityName, false, false);
node.payload = Object.keys(payload).reduce((o, item) => {
// prop is defined because "user" is a defined relationship
// as well as the name of the database field that holds the foreign key
const prop = meta && meta.properties[item];
const childEntity = prop && prop.reference !== ReferenceType.SCALAR ? prop.type : entityName;
o[item] = CriteriaNode.create(metadata, childEntity, payload[item], node, item);
return o;
}, {});
return node;
}
Issue Analytics
- State:
- Created 4 years ago
- Comments:9 (6 by maintainers)
Top Results From Across the Web
3 common foreign key mistakes (and how to avoid them)
Dangling foreign keys 3. Not creating foreign key indexes Bonus: Not using foreign keys Key takeaway: think before you CREATE TABLE.
Read more >Hibernate - Avoiding unnecessary join when using foreign key ...
For a similar reason, it's generally optimal to add any query hints that you can to joins. For instance (rewriting your query in...
Read more >novice-design: Are FOREIGN KEYs always necessary? Joins ...
FKs are to enforce referential integrity, not to allow joins per se. MyISAM had no FKs and still you were expected to join...
Read more >Changing Foreign Keys and Navigations - EF Core
This dependent/child entity is associated with a given principal/parent entity when the values of the foreign key properties on the dependent/ ...
Read more >Hidden secrets of SQL Server Foreign Keys - SQLShack
When we look at the properties of the nested loops operator, we see that it performs a left semi join operation. At the...
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
Just wanted to thank you (for the other issues too), I have everything working the way I want now!
Fixed in
3.0.0-rc.1
.