typeorm icon indicating copy to clipboard operation
typeorm copied to clipboard

Bug with @VirtualColumn not being returned correctly

Open macleysousa opened this issue 1 year ago • 36 comments

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)

  • [ ] aurora-mysql
  • [ ] aurora-postgres
  • [ ] better-sqlite3
  • [ ] cockroachdb
  • [ ] cordova
  • [ ] expo
  • [ ] mongodb
  • [ ] mysql
  • [ ] nativescript
  • [ ] oracle
  • [X] postgres
  • [ ] react-native
  • [ ] sap
  • [ ] spanner
  • [ ] sqlite
  • [ ] sqlite-abstract
  • [ ] sqljs
  • [ ] sqlserver

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

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

macleysousa avatar Jan 17 '24 13:01 macleysousa

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 avatar Jan 18 '24 08:01 nodegin

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

nodegin avatar Jan 18 '24 08:01 nodegin

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

jorenvandeweyer avatar Jan 18 '24 09:01 jorenvandeweyer

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.

kevinwilson541 avatar Jan 18 '24 23:01 kevinwilson541

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

NarHakobyan avatar Jan 19 '24 15:01 NarHakobyan

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

jorenvandeweyer avatar Jan 19 '24 15:01 jorenvandeweyer

@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

macleysousa avatar Jan 19 '24 18:01 macleysousa

Same here ->

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

fatlirmorina avatar Jan 23 '24 10:01 fatlirmorina

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

EliezerMafra avatar Jan 23 '24 11:01 EliezerMafra

I have the Same issue.

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

PatrickWe avatar Jan 28 '24 19:01 PatrickWe

not working with us too after update from 0.3.17

AmrAnwar avatar Jan 29 '24 14:01 AmrAnwar

still broken in 0.3.20

martin-scholz-emnify avatar Jan 30 '24 08:01 martin-scholz-emnify

Any news on open MR ?

gwen1230 avatar Feb 27 '24 15:02 gwen1230

@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.

nodegin avatar Feb 27 '24 16:02 nodegin

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.

strongpauly avatar Feb 29 '24 15:02 strongpauly

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

Dawar avatar Mar 08 '24 16:03 Dawar

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

d3tr1tus avatar Mar 21 '24 18:03 d3tr1tus

any updates on this?

ernes128 avatar Mar 25 '24 20:03 ernes128

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!

tristin-terry avatar May 03 '24 21:05 tristin-terry

Any news on this, please?

Many production environments are affected by this, please.

fatlirmorina avatar May 13 '24 07:05 fatlirmorina

@fatlirmorina The solution is to install 0.3.17

nodegin avatar May 13 '24 15:05 nodegin

Any news on this

jsnazaroff avatar Jun 06 '24 11:06 jsnazaroff

Any news on this

PatrickWe avatar Jun 07 '24 09:06 PatrickWe

Any news on this

ReneeDress avatar Jun 08 '24 08:06 ReneeDress

Any news on this

Unfortunately not, still awaiting PR approval.

macleysousa avatar Jun 10 '24 14:06 macleysousa

Have you got any update on this yet?

krishnabrq avatar Jul 05 '24 17:07 krishnabrq

Have you got any update on this yet?

Unfortunately still no response. I'm already thinking about creating a fork of the project.

macleysousa avatar Jul 06 '24 12:07 macleysousa

+1

Pyakz avatar Aug 05 '24 06:08 Pyakz

I'd also like to offer my own fork as a solution. I took e7649d2746f907ff36b1efb600402dedd5f5a499 and applied the fix of #10698, and it is published as @helveg/typeorm. I won't maintain the fork any further, but people looking for typeorm 0.3.20 with working VirtualColumns can install it with the following npm-alias:

npm install typeorm@npm:@helveg/[email protected]

which will fix this issue.

Helveg avatar Aug 05 '24 16:08 Helveg

+1

luscala avatar Aug 19 '24 13:08 luscala