Repository: oguimbal/pg-mem

Issue #38: Self-Alias not found  (subquery using alias of its outer query)
Created: 2021-01-18T10:32:15Z
https://oguimbal.github.io/pg-mem-playground/
CREATE TABLE my_table (id text NOT NULL PRIMARY KEY, name text NOT NULL, parent_id text);
CREATE INDEX my_table_idx_name ON my_table (name);
CREATE INDEX my_table_idx_id_parent_id ON my_table (id,parent_id);

SELECT * FROM my_table as t1 WHERE t1.name = 'test';
SELECT * FROM my_table as t1 WHERE t1.name = 'test' AND NOT EXISTS (SELECT * FROM my_table as t2 WHERE t2.parent_id = t1.id);

Alias 't1' not found

🐜 This seems to be an execution error, which means that your request syntax seems okay,
but the resulting statement cannot be executed → Probably not a pg-mem error.

*️⃣ Reconsituted failed SQL statement: SELECT *  FROM "my_table" AS "t1"  WHERE (("t1"."name" = ('test')) AND (NOT ("exists"((SELECT *  FROM "my_table" AS "t2"  WHERE ("t2"."parent_id" = "t1"."id") )))))

👉 You can file an issue at https://github.com/oguimbal/pg-mem along with a way to reproduce this error (if you can), and  the stacktrace:

Duplicate issue #114: Subquery execution error
Created: 2021-05-07T22:34:54Z
I'm building queries with Objection ORM and subqueries that work when executed against postgres seem to fail with pg-mem. Here's an example:
 select "achievement".* from "achievement" where exists (select "users".* from "user" as "users" inner join "user_achievement" on "users"."id" = "user_achievement"."user_id" where "user_achievement"."achievement_id" = "achievement"."id") - column "achievement.id" does not exist

    🐜 This seems to be an execution error, which means that your request syntax seems okay,
    but the resulting statement cannot be executed → Probably not a pg-mem error.

    *️⃣ Failed SQL statement: select "achievement".* from "achievement" where exists (select "users".* from "user" as "users" inner join "user_achievement" on "users"."id" = "user_achievement"."user_id" where "user_achievement"."achievement_id" = "achievement"."id");

    👉 You can file an issue at https://github.com/oguimbal/pg-mem along with a way to reproduce this error (if you can), and  the stacktrace:

    > 87 |   const storedAchievements = await Achievement.query(trx).whereExists(
         |                              ^
      88 |     Achievement.relatedQuery('users')
      89 |   )
      90 |   //.where({                      ^
      at JoinSelection.getColumn (node_modules/pg-mem/src/transforms/join.ts:264:19)
      at _buildValueReal (node_modules/pg-mem/src/expression-builder.ts:74:25)
      at _buildValue (node_modules/pg-mem/src/expression-builder.ts:48:11)
      at buildBinary (node_modules/pg-mem/src/expression-builder.ts:204:22)
      at _buildValueReal (node_modules/pg-mem/src/expression-builder.ts:70:20)
      at _buildValue (node_modules/pg-mem/src/expression-builder.ts:48:11)
      at Object.buildValue (node_modules/pg-mem/src/expression-builder.ts:18:17)
      at _buildFilter (node_modules/pg-mem/src/transforms/build-filter.ts:24:19)
      at Object.buildFilter (node_modules/pg-mem/src/transforms/build-filter.ts:19:12)
      at JoinSelection.filter (node_modules/pg-mem/src/transforms/transform-base.ts:81:26)
      at DbSchema.buildSelect (node_modules/pg-mem/src/schema.ts:1141:19)
      at Alias.subquery (node_modules/pg-mem/src/transforms/transform-base.ts:101:33)
      at buildSelectAsArray (node_modules/pg-mem/src/expression-builder.ts:586:25)
      at _buildValueReal (node_modules/pg-mem/src/expression-builder.ts:118:20)
      at _buildValue (node_modules/pg-mem/src/expression-builder.ts:48:11)
      at node_modules/pg-mem/src/expression-builder.ts:98:44
          at Array.map (<anonymous>)
      at _buildValueReal (node_modules/pg-mem/src/expression-builder.ts:98:35)
      at _buildValue (node_modules/pg-mem/src/expression-builder.ts:48:11)
      at Object.buildValue (node_modules/pg-mem/src/expression-builder.ts:18:17)
      at _buildFilter (node_modules/pg-mem/src/transforms/build-filter.ts:24:19)
      at Object.buildFilter (node_modules/pg-mem/src/transforms/build-filter.ts:19:12)
      at Alias.filter (node_modules/pg-mem/src/transforms/transform-base.ts:81:26)
      at DbSchema.buildSelect (node_modules/pg-mem/src/schema.ts:1141:19)
      at DbSchema.prepareWithable (node_modules/pg-mem/src/schema.ts:440:47)
      at DbSchema.executeWithable (node_modules/pg-mem/src/schema.ts:453:25)
      at DbSchema._execOne (node_modules/pg-mem/src/schema.ts:165:33)
      at node_modules/pg-mem/src/schema.ts:118:31
      at Object.pushContext (node_modules/pg-mem/src/utils.ts:382:16)
      at DbSchema.queries (node_modules/pg-mem/src/schema.ts:115:47)
          at queries.next (<anonymous>)
      at DbSchema.query (node_modules/pg-mem/src/schema.ts:84:20)
      at MemPg.query (node_modules/pg-mem/src/adapters.ts:113:76)
      at node_modules/knex/lib/dialects/postgres/index.js:237:18
      at Client_PG._query (node_modules/knex/lib/dialects/postgres/index.js:236:12)
      at Client_PG.query (node_modules/knex/lib/client.js:168:17)
      at Runner.query (node_modules/knex/lib/runner.js:151:36)
      at node_modules/knex/lib/runner.js:40:23
      at node_modules/knex/lib/runner.js:277:24
      at QueryBuilder.execute (node_modules/objection/lib/queryBuilder/QueryBuilder.js:667:22)

Closing comment (2021-05-08T09:20:10Z): Hi !
This is a duplicate of #38
This bug has been botheriing me for a while now, and requires an ongoing non-trivial refactoring to be fixed.
I'm lacking time to address these days, but I hope to resolve it soon.
Thanks for the report