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.

Unnecessary join: foreign key same as property name

See original GitHub issue

Describe 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:closed
  • Created 4 years ago
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
emrosenfcommented, Dec 4, 2019

Just wanted to thank you (for the other issues too), I have everything working the way I want now!

0reactions
B4nancommented, Dec 1, 2019

Fixed in 3.0.0-rc.1.

Read more comments on GitHub >

github_iconTop 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 >

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