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

TypeORM VirtualColumn cause error in table creation and querying #390

Open
XihuaYang opened this issue Mar 22, 2024 · 2 comments
Open

TypeORM VirtualColumn cause error in table creation and querying #390

XihuaYang opened this issue Mar 22, 2024 · 2 comments

Comments

@XihuaYang
Copy link

XihuaYang commented Mar 22, 2024

Describe the bug

Use VirtualColumn feature in TypeORM (https://orkhan.gitbook.io/typeorm/docs/decorator-reference#virtualcolumn) cause issues with table creation, table would either fail to create the table and later on run into relation "Foo" does not exist error, or the table could be mis-structured and run into column "Foo.bar" does not exist, even though bar is defined in the entity.
Removing the field annotated with @VirtualColumn would allow pg-mem to behave correctly.

To Reproduce

@Entity('test')
class TestEntity {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  num: number;

  @VirtualColumn({
    query: (alias) => ` SELECT SUM(num) FROM ${alias}`,
  })
  test_sum: number;
}

The generated SQL from TypeORM by calling dataSource.getRepository(TestEntity).find() looks like below:

SELECT "TestEntity"."id" AS "TestEntity_id", "TestEntity"."num" AS "TestEntity_num", ( SELECT SUM(num) FROM "TestEntity") AS "TestEntity_test_sum" FROM "test" "TestEntity"

With the entity defined above, the SQL generated above would fail with error relation "TestEntity" does not exist. However, if simply remove the @VirtualColumn field test_sum, the .find() call would succeed without any problem. It also would not work if we copy the generated SQL and call dataSource.getRepository(TestEntity).query(sql), the failure is the same.
If we change the query in virtual column to use direct table name instead of alias: SELECT SUM(num) FROM "test", the query would run without error, but the returned result only contains result of the subquery for virtual column, the id and num fields are missing in the returned object.
We have verified the query can run successfully in an actual Postgres v16 DB.

pg-mem version

2.8.1

@ayoubbouguettaya
Copy link

are you been able to fix this ?

@XihuaYang
Copy link
Author

are you been able to fix this ?

No I have not, I suspect this is related to the parser of pg-mem, and I've give up using virtual column, instead I'll just use a raw query and select whatever calculated column I need, then write code to fill the calculated result into a non-column field in the entity

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

No branches or pull requests

2 participants