Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reusing subqueries inside of laterals with the shortcut functions results in incorrect parent tables #167

Open
rosettaroberts-impact opened this issue Apr 3, 2024 · 1 comment · May be fixed by #168

Comments

@rosettaroberts-impact
Copy link

Let me know if you want me to make a minimal reproduce-able example!

Here is an example of a query with the problem:

const foo = db.selectOne('foo', { id: parent('foo_id') });

const result = await db.select('bar', db.all, {
  lateral: {
    foo,
    baz: db.selectOne('baz', { id: parent('baz_id') }, {
      lateral: { foo }
    })
  }
);

In this example, you will get a database error somewhat like the following:

      "type": "DatabaseError",
      "message": "missing FROM-clause entry for table \"baz\"",

This is because foo's parent table is set first to bar, but then overridden with baz.

A solution to this is to make the parentTable property on SQLExpression readonly, and to require making a copy of the SQLExpression to set the parentTable.

@rosettaroberts-impact
Copy link
Author

I'll be able to make a PR to fix this probably next week if someone else doesn't get to it by then!

@rosettaroberts-impact rosettaroberts-impact linked a pull request Apr 9, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant