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

Column name aliasing broke in v 0.2.29 #7030

Closed
1 of 17 tasks
ddehghan opened this issue Nov 7, 2020 · 14 comments · Fixed by #7042, mattwelke/typeorm-postgres-example#165 or newerton/gobarber-2-backend#17
Closed
1 of 17 tasks

Comments

@ddehghan
Copy link

ddehghan commented Nov 7, 2020

~~It looks like bug fix #6870 caused this regression. ~~ Edit from @imnotjames: It seems to be #4760

If you have a table with Primary key and another column with the same name, then the Primary ID name gets precedence. 0.2.28 was working correctly.

Issue Description

We have a table for example called Book which had a DB primary key in the db. But we don't want to expose this to the users so we have created another column Book_id which has a UUID which we use in our API

@Entity({name: 'website_book'})
export class Book {
    @PrimaryGeneratedColumn({
        type: 'integer',
        name: 'id',     <========= 0.2.29 behavior 
    })
    oldBookId: number;

    @Column('int', {
        nullable: false,
        name: 'user_id',
    })
    user_id: number;

    @Column('character varying', {
        nullable: false,
        unique: true,
        length: 38,
        name: 'book_id',
    })
    id: string;    <========= 0.2.28 behavior 
}

Expected Behavior

" Book.id " should be mapped to (book_id column in the database)

Actual Behavior

" Book.id " is mapped to (id column in the database)

My Environment

Not os or env dependent issue

| TypeORM version : 0.2.29

Relevant Database Driver(s)

  • aurora-data-api
  • aurora-data-api-pg
  • better-sqlite3
  • cockroachdb
  • cordova
  • expo
  • mongodb
  • mysql
  • nativescript
  • oracle
  • postgres
  • react-native
  • sap
  • sqlite
  • sqlite-abstract
  • sqljs
  • sqlserver

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

I can help test the resolution

@nebkat
Copy link
Contributor

nebkat commented Nov 7, 2020

#6870 appears to be related to count expressions only so I don't think that is the cause.

Could you create a minimal sample program or test to recreate this and/or post a query log along with the input/output entity values?

@ddehghan
Copy link
Author

ddehghan commented Nov 7, 2020

Sure, here is a simple repro. https://github.com/ddehghan/typeorm-bug

the insert works correctly but the findOne() fails. It is falling back to the db column name "id" instead of the alias "id" which points to "new_id" column

typeorm: 0.2.29 output:

$ npx ts-node src/app.ts

Post has been saved:  Post {
  text: 'Hello how are you?',
  title: 'hello',
  id: 'df027828-6b7b-4a52-992a-d087872fb6a4',
  likesCount: 100,
  oldId: 10
}
(node:71466) UnhandledPromiseRejectionWarning: QueryFailedError: invalid input syntax for type integer: "df027828-6b7b-4a52-992a-d087872fb6a4"
    at new QueryFailedError (/Users/david/Downloads/test/src/error/QueryFailedError.ts:9:9)
    at Query.callback (/Users/david/Downloads/test/src/driver/postgres/PostgresQueryRunner.ts:220:30)
    at Query.handleError (/Users/david/Downloads/test/node_modules/pg/lib/query.js:128:19)
    at Client._handleErrorMessage (/Users/david/Downloads/test/node_modules/pg/lib/client.js:335:17)
    at Connection.emit (events.js:314:20)
    at Connection.EventEmitter.emit (domain.js:486:12)
    at /Users/david/Downloads/test/node_modules/pg/lib/connection.js:109:12
    at Parser.parse (/Users/david/Downloads/test/node_modules/pg-protocol/src/parser.ts:102:9)
    at Socket.<anonymous> (/Users/david/Downloads/test/node_modules/pg-protocol/src/index.ts:7:48)
    at Socket.emit (events.js:314:20)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:71466) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:71466) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

typeorm: 0.2.28 output:

 $ npx ts-node src/app.ts

Post has been saved:  Post {
  text: 'Hello how are you?',
  title: 'hello',
  id: '37431979-77e2-44ae-97e1-1817c91aa811',
  likesCount: 100,
  oldId: 11
}
Post {
  oldId: 11,
  id: '37431979-77e2-44ae-97e1-1817c91aa811',
  title: 'hello',
  text: 'Hello how are you?',
  likesCount: 100
}

@imnotjames imnotjames self-assigned this Nov 8, 2020
@imnotjames
Copy link
Contributor

imnotjames commented Nov 8, 2020

I'm very unsure how the count expression PR caused this issue but I'll take a look. What points you at that specific PR?

It sounds like you're doing a findOne, not a count - so the code in question in that PR shouldn't be executing.

@imnotjames
Copy link
Contributor

Test to reproduce - imnotjames@438f577

@imnotjames
Copy link
Contributor

Code confirmed failing in 0.2.29 but also seems to be failing in earlier versions. I tested 0.2.28 and saw the same failures.

@imnotjames
Copy link
Contributor

imnotjames commented Nov 8, 2020

Also tested with 0.2.26 - same deal. Failures ): So a bigger issue than something in the latest release?

@ddehghan
Copy link
Author

ddehghan commented Nov 8, 2020

I am not sure about the exact PR that caused this. I just looked at the change list of .29 and guessed.

Also I cant repro this on 0.2.26. I tried my example on .24-2.9 and only it fails on .29.

@imnotjames
Copy link
Contributor

Ok - steps I followed to verify with that code with the test I had created:

git checkout 0.2.26
rm -rf node_modules
npm install
npm run test

@imnotjames
Copy link
Contributor

Oh :) I see my issue - I mixed up your PK and the id field, oops. I'll start bisecting shortly..

@imnotjames
Copy link
Contributor

imnotjames commented Nov 9, 2020

According to git-bisect it's d86671c

#4760

@imnotjames
Copy link
Contributor

imnotjames commented Nov 9, 2020

if (!(column.databaseName in replacements))
replacements[column.databaseName] = column.databaseName;

These lines weren't in the original implementation. We added it to fix some implementation weirdness.

This would be fine except.. We also are running in an undefined sort because JS objects aren't sorted EDIT: That wouldn't impact this, because it's exact matches because of the regex.

So we probably want to do propertyPath first for all classes, then propertyName, then databaseName

imnotjames added a commit to imnotjames/typeorm that referenced this issue Nov 9, 2020
we should always prioritize replacement of any property path first,
then property names, then database names, then relation values
whenever there's ambiguous values

fixes typeorm#7030
@imnotjames
Copy link
Contributor

@ddehghan If you do have a moment and can verify the change that'd be helpful in getting this fixed in the next release!

@ddehghan
Copy link
Author

ddehghan commented Nov 9, 2020

Great. It worked. :-) Thanks a lot @imnotjames

pleerock pushed a commit that referenced this issue Nov 10, 2020
)

we should always prioritize replacement of any property path first,
then property names, then database names, then relation values
whenever there's ambiguous values

fixes #7030
edcolvin pushed a commit to edcolvin/typeorm that referenced this issue Nov 12, 2020
…peorm#7042)

we should always prioritize replacement of any property path first,
then property names, then database names, then relation values
whenever there's ambiguous values

fixes typeorm#7030
nebkat pushed a commit to nebkat/typeorm that referenced this issue Jan 9, 2021
…peorm#7042)

we should always prioritize replacement of any property path first,
then property names, then database names, then relation values
whenever there's ambiguous values

fixes typeorm#7030
nebkat pushed a commit to nebkat/typeorm that referenced this issue Jan 9, 2021
…peorm#7042)

we should always prioritize replacement of any property path first,
then property names, then database names, then relation values
whenever there's ambiguous values

fixes typeorm#7030
zaro pushed a commit to zaro/typeorm that referenced this issue Jan 12, 2021
…peorm#7042)

we should always prioritize replacement of any property path first,
then property names, then database names, then relation values
whenever there's ambiguous values

fixes typeorm#7030
@midoelhawy
Copy link

Hi everyone
unfortunately, I have the same problem

TypeOrm use Class-model as table name in the final query

Code :

const banners = await AppDataSource.manager.find(Banner, {
                where: {
                    active: true
                }
            })

TYPE ORM QUERY RESULT :

SELECT 'Banner'.'id' AS 'Banner_id', 'Banner'.'image' AS 'Banner_image', 'Banner'.'description' AS 'Banner_description', 'Banner'.'type' AS 'Banner_type', 'Banner'.'price' AS 'Banner_price', 'Banner'.'link' AS 'Banner_link', 'Banner'.'active' AS 'Banner_active', 'Banner'.'customar_id' AS 'Banner_customar_id', 'Banner'.'updated_at' AS 'Banner_updated_at', 'Banner'.'created_at' AS 'Banner_created_at' FROM 'banners' 'Banner' WHERE ('Banner'.'active' = ?)'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment