typeorm / typeorm

ORM for TypeScript and JavaScript. Supports MySQL, PostgreSQL, MariaDB, SQLite, MS SQL Server, Oracle, SAP Hana, WebSQL databases. Works in NodeJS, Browser, Ionic, Cordova and Electron platforms.
http://typeorm.io
MIT License
33.46k stars 6.21k forks source link

Bug with @VirtualColumn not being returned correctly #10638

Open macleysousa opened 4 months ago

macleysousa commented 4 months ago

Issue description

@VirtualColumn not being returned correctly

Expected Behavior

@Entity({ name: 'integrations' })
export class IntegrationEntity {
  @Exclude()
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @ApiProperty()
  @PrimaryColumn({ name: 'client_id', type: 'uuid' })
  client_id: string;

  @ApiProperty()
  @VirtualColumn({ query: (alias) => `select "DMS" from config_client where "client_id"=${alias}.client_id` })
  dms: string;
}

It was expected that @VirtualColumn would return the correct values according to the logic defined in the decorator.

{
  "client_id": "1b97a2d2-91b8-4dab-9593-906da6d95d36",
  "dms": "NBS"
}

Actual Behavior

@Entity({ name: 'integrations' })
export class IntegrationEntity {
  @Exclude()
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @ApiProperty()
  @PrimaryColumn({ name: 'client_id', type: 'uuid' })
  client_id: string;

  @ApiProperty()
  @VirtualColumn({ query: (alias) => `select "DMS" from config_client where "client_id"=${alias}.client_id` })
  dms: string;
}

DNS column is not being returned

{
  "client_id": "1b97a2d2-91b8-4dab-9593-906da6d95d36"
}

Steps to reproduce

  1. Add the @VirtualColumn decorator to an entity or class.
  2. Define the expected logic for the virtual value.
  3. Observe the obtained result.

My Environment

Dependency Version
Operating System windows 10
Node.js version 18.18.0
Typescript version 5.3.3
TypeORM version 0.3.19

Additional Context

No response

Relevant Database Driver(s)

Are you willing to resolve this issue by submitting a Pull Request?

Yes, I have the time, and I know how to start.

nodegin commented 4 months ago

Same here, with latest 0.3.19. Locking the version to 0.3.17 fixed it. Not sure if breaking changes introduced in 0.3.18 or 0.3.19.

nodegin commented 4 months ago

https://github.com/typeorm/typeorm/pull/10432/files

jorenvandeweyer commented 4 months ago

How do you select the virtual column, can you give an example of the implementation?

kevinwilson541 commented 4 months ago

As an example, usage of the repository findOne or findOneOrFail functions, without the select option, will return virtual columns by default in version 0.3.17. Version 0.3.19 omits virtual columns with the same usage pattern.

NarHakobyan commented 4 months ago

Yeah, we are experiencing the same issue, virtual column stopped working. this is urgent‼️

jorenvandeweyer commented 4 months ago

Can you create a PR to fix the issue introduced in #10432 but also keep #10431 fixed? If not, I think we should create a PR reverting the changes from #10432

macleysousa commented 4 months ago

@jorenvandeweyer I think we should create a PR reverting the changes from #10432, because analyzing the code from PR #10432 it just made all virtual columns return null, in his case I think he should use a transformation to convert empty to null

fatlirmorina commented 3 months ago

Same here ->

Upgraded from 0.3.17 to 0.3.19 (latest) and everything related to VirtualColumn breaks.

EliezerMafra commented 3 months ago

Same here -> the @VirtualColumn decorator does not work on 0.3.19 version

PatrickWe commented 3 months ago

I have the Same issue.

Upgraded from 0.3.17 to 0.3.19 and everything related to VirtualColumn breaks.

AmrAnwar commented 3 months ago

not working with us too after update from 0.3.17

martin-scholz-emnify commented 3 months ago

still broken in 0.3.20

gwen1230 commented 2 months ago

Any news on open MR ?

nodegin commented 2 months ago

@gwen1230 I think they are not going to fix this because not much people are using virtual column, so for stability either downgrade or switch to Prisma instead.

strongpauly commented 2 months ago

I've seen this too after upgrading. A possible workaround is to pass a select which includes the virtual column, but this isn't great as you then have to pass all the fields that you want to be returned.

Dawar commented 2 months ago

I've seen this too after upgrading. A possible workaround is to pass a select which includes the virtual column, but this isn't great as you then have to pass all the fields that you want to be returned.

Tried this but didn't seem to work, TypeORM is trying to load the column from the actual table. Did you have any success with it?

If the general direction is to manually select virtualcolumns that would also be fine (there should probably be an eager option on the decorator in that case). The previous virtualcolumn functionality was ideal imo. Curious why these breaking changes were implemented from a benefit stand-point.

Will try and compare prev and curr implementation for possible ideas

d3tr1tus commented 1 month ago

Any updates? I would appreciate some fix here it will be really helpful 🙏🏼

ernes128 commented 1 month ago

any updates on this?

tristin-terry commented 2 weeks ago

A fix for this would be helpful, we were just affected by this in production and are going to have to downgrade or patch it internally until it is fixed!

hopefully either https://github.com/typeorm/typeorm/pull/10777 or https://github.com/typeorm/typeorm/pull/10698 can be reviewed soon!

fatlirmorina commented 6 days ago

Any news on this, please?

Many production environments are affected by this, please.

nodegin commented 6 days ago

@fatlirmorina The solution is to install 0.3.17