romeerez / orchid-orm

Orchid ORM
https://orchid-orm.netlify.app/
MIT License
488 stars 14 forks source link

Can no longer select computed columns #404

Open mordechaim opened 2 days ago

mordechaim commented 2 days ago

A new bug introduced in orchid-orm@1.35.17, when trying to select a computed column, the ORM thinks it's a regular column and the query fails:

{
  message: 'column "name" does not exist',
  length: 105,
  name: 'error',
  severity: 'ERROR',
  code: '42703',
  position: '2522',
  file: 'parse_relation.c',
  line: '3722',
  routine: 'errorMissingColumn',
  cause: {}
}
mordechaim commented 2 days ago

The issue number is very appropriate lol...

romeerez commented 2 days ago

I won't fix it soon, hope it's alright for you to rollback for awhile.

Tests are passing, so I can't see what's wrong, please share code of your table class and the query, there has to be something.

mordechaim commented 2 days ago

Interestingly, I try to isolate it to create a reproduction, but it does seem to work.

mordechaim commented 2 days ago

Yes! I finally reproduced it.

So, the following requirements are needed to reproduce it:

The following table:

export class ATable extends BaseTable {
  readonly table = 'a';

  columns = this.setColumns((t) => ({
    id: t.identity().primaryKey(),
    snakeCase: t.text(),
  }));

  computed = this.setComputed((q) => ({
    name: sql`'name'`.type((t) => t.string()),
  }));
}

Try selecting:


const query = db.a.select('*', 'name');

console.log(query.toSQL().text);
const result = await query;
console.log(result);

The output is

SELECT "id", "snake_case" AS "snakeCase", "name", 'name' "name" FROM "a"
[redacted]\node_modules\pqb\src\queryMethods\then.ts:411
      error = new (q.error as unknown as new () => QueryError)();
              ^

error: column "name" does not exist
    at then ([redacted]\node_modules\pqb\src\queryMethods\then.ts:411:15)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
  length: 104,
  severity: 'ERROR',
  code: '42703',
  detail: undefined,
  hint: undefined,
  position: '43',
  internalPosition: undefined,
  internalQuery: undefined,
  where: undefined,
  schema: undefined,
  table: undefined,
  column: undefined,
  dataType: undefined,
  constraint: undefined,
  file: 'parse_relation.c',
  line: '3722',
  routine: 'errorMissingColumn',
  cause: Error
      at Db.getThen ([redacted]\node_modules\pqb\src\queryMethods\then.ts:84:18)
      at <anonymous> ([redacted]\index.ts:6:16)
      at ModuleJob.run (node:internal/modules/esm/module_job:192:25)
      at async CustomizedModuleLoader.import (node:internal/modules/esm/loader:228:24)
      at async loadESM (node:internal/process/esm_loader:40:7)
      at async handleMainPromise (node:internal/modules/run_main:66:12)
}

Node.js v20.5.0

As you see, the name column is included as a regular column in the query.