Skip to content

fix(postgres): length limit for alias names #3118 - #3514

Merged
pleerock merged 1 commit into
typeorm:masterfrom
VinceOPS:3118-shorten-postgres-name-of-alias-columns
Mar 1, 2019
Merged

pleerock merged 1 commit into
typeorm:masterfrom
VinceOPS:3118-shorten-postgres-name-of-alias-columns

Conversation

@VinceOPS

@VinceOPS VinceOPS commented Jan 25, 2019 •

Copy link
Copy Markdown
Contributor

Hi there!

Context

I just made a small Pull request to address #3118. Postgres has a (built-in) limit of 63 characters for the name of columns and aliases used in SQL queries.

This prevent TypeORM from being able to properly transform raw results to Entities when querying "deeply nested" relations, as it can generate aliases as long as "CategoryWithVeryLongName__postsWithVeryLongName__authorWithVeryLongName_firstName" (dummy example, but still 81 characters long).

Thus, the "populated" (joined) authorWithVeryLongName is never converted from a raw result to an entity (and stays undefined).

Solution

A similar issue has already been addressed for Oracle here by abbreviating the name of the selected column.
My concern with this code is that when alias is "CategoryWithVeryLongName__postsWithVeryLongName__authorWithVeryLongName" and columnName is "firstName", abbreviating "firstName" to 2 characters might create collisions with other columns starting with "fi".
That's why I decided to write shorten, which is focused on shortening aliases.

I also added a test which guarantees that authorWithVeryLongName is correctly loaded/instantiated as an entity.

Why

Some E2E tests started failing in our application when this fix was introduced. Using __ instead of _ (as a separator for the path of tables) lengthened the selection aliases.

Any feedback is most welcome, this is only my second PR in the project. Thanks.

@VinceOPS
VinceOPS force-pushed the 3118-shorten-postgres-name-of-alias-columns branch 2 times, most recently from 233dc5b to 04e838b Compare January 25, 2019 13:16
@pleerock

Copy link
Copy Markdown
Member

Maybe I'm asking for too much, but can't we make an ideal and proper solution from the beginning and cover all exist drivers and database and resolve this issue forever?

@VinceOPS

VinceOPS commented Jan 25, 2019 •

Copy link
Copy Markdown
Contributor Author

@pleerock That would be much better, indeed. I didn't want to change the codebase too much in order to avoid making the review time too painful.

What would you recommend? Defining a dedicated method into Driver and overriding it in specific driver classes?

@pleerock

Copy link
Copy Markdown
Member

If you are ready to start this refactoring for all drivers then see comment above ^

@VinceOPS

Copy link
Copy Markdown
Contributor Author

@pleerock Alright, I made a new commit (for the sake of an easier review) with a more generic approach. Let me know. Please also have a look at t4nz's comment about "alias collisions".

Thanks

@t4nz

t4nz commented Feb 6, 2019

Copy link
Copy Markdown

Any news? I don't want to be rude but the ORM with Oracle is quite unusable right now

@vlapo vlapo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really love these complex PRs for all drivers. We have to start do things right and not just fixing issues. Thnx a lot for this @VinceOPS.

I have just few comments about formating but I would love to see more tests not only for postgres but for all drivers and maybe more scenarios if it is possible. Could you please add tests e.g. in test/functional/query-builder/ test folder?

Comment thread src/util/StringUtils.ts Resolved
Comment thread src/driver/mongodb/MongoDriver.ts Resolved
@pleerock
pleerock requested a review from AlexMesser February 9, 2019 14:20
Comment thread CHANGELOG.md Resolved
@ajxs

ajxs commented Feb 15, 2019 •

Copy link
Copy Markdown

Any news? I don't want to be rude but the ORM with Oracle is quite unusable right now

I'm in the same boat, our application is coming unstuck because of this issue.

@ajxs

ajxs commented Feb 16, 2019 •

Copy link
Copy Markdown

What is actually even more troubling, is that the relations which silently failed to be populated will overwrite values with null during a cascading save, which has the capability to destroy all kinds of valuable data. We're just lucky we caught this in testing.
I've already heard all about the creators views on cascading, but this is pretty standard ORM functionality, and if used can have pretty dreadful consequences because of this bug.
The very unfortunate thing for us, is that now we have to go through our app and ensure that this condition cannot happen. It's very difficult for us to predict when the ORM is going to generate an invalid query, fail silently then just clobber data during a save.
The 'persistence ignorance' that I mentioned in that thread regarding the future of TypeORM has become basically impossible, we now budget to program defensively around the ORM.

@pleerock

Copy link
Copy Markdown
Member

We almost ready to merge this PR, just few small things left to finish, hope @VinceOPS will find a time to take a look on this PR.

@ajxs sorry, but how is your last comment related to this PR?

@ajxs

ajxs commented Feb 16, 2019 •

Copy link
Copy Markdown

We almost ready to merge this PR, just few small things left to finish, hope @VinceOPS will find a time to take a look on this PR.

@ajxs sorry, but how is your last comment related to this PR?

This bug is causing data clobbering in our product. This is something you should be aware of, that under certain conditions this bug can lead to serious data corruption.

Take for instance this scenario:

@Entity("item")
export class Item {
	@PrimaryGeneratedColumn("uuid", {
		name: "item_id"
	})
	public itemId:string;

	@OneToOne(type => OtherThing, {
		cascade: true,
		eager: true
	})
	@JoinColumn({
		name: "super_long_other_thing_identifier",
		referencedColumnName: "otherThingId"
	})
	public superLongOtherThing:OtherThing;
});

If you deserialised this entity, and populating the superLongOtherThing relation failed, resulting in a null value being present for the relation, if you used a repository save method on the entity, the ORM will think that I've changed the value of super_long_other_thing_identifier to be NULL. If the relation was nullable, there would be error thrown and the save would succeed. We have sufficiently large and complex entities, it was only in our automated testing that we caught the fact that values were being overwritten with NULL in the database.

@pleerock

Copy link
Copy Markdown
Member

Please DON'T CALL IT BUG. Because its not a BUG. Its how it is designed to work. Your statements can mislead other people. And I didn't get the actual answer to my question - how is your problem related to this PR?

@ajxs

ajxs commented Feb 16, 2019

Copy link
Copy Markdown

Please DON'T CALL IT BUG. Because its not a BUG. Its how it is designed to work. Your statements can mislead other people. And I didn't get the actual answer to my question - how is your problem related to this PR?

Pardon me, I thought I was commenting on the 'issue'. I think I ended up here following what I thought was a 'related issue' link. I understand what you mean now.
And yes, this is a bug. If the ORM generates an invalid query, which does not populate the entity in the prescribed manner, which in turn leads to unintended, undefined behavior then it is a bug.

@pleerock

Copy link
Copy Markdown
Member

@ajxs please create a new issue if you haven't yet and link all our conversations on this topic and lets continue discussion in there

@adrianhara

adrianhara commented Feb 18, 2019 •

Copy link
Copy Markdown

Great work guys! I stumbled onto this issue by hitting the postgres limitation. One question: if I understand the code correctly the shorten function takes some options but it's never called with any, therefore always splitting on double-underscore __. In our case the segments are separated by a simple underscore _ and won't be split/shortened correctly. One option I see is to define this separator at the driver level, same as max length and then send it down to the function when calling. What do you think, would that work or do you see a better option?

Also, will this be merged only to master or also to next? (we're using next)

@VinceOPS

VinceOPS commented Feb 20, 2019 •

Copy link
Copy Markdown
Contributor Author

Hi guys & sorry for the delay. I'm actually open for change requests and improvements, but I cannot test this feature for Oracle. At least I don't really have an interest/time in trying to make my workstation able to run Oracle and installing the drivers required to let the Node app connect to it... 🙄

EDIT - Don't want to look indelicate/rude so I'd rather be accurate: any help/commit/code change is most welcome regarding Oracle, if someone can/wants to do this part of the job.

However I'd be glad to do whatever is required for MySQL & other DB systems "directly" available through the docker-compose. I can't reinvent the wheel or refactor too much code, though. I think I'd better fix only this issue 😅 😁 ... And we could create different PR's for pure-refactoring.

@pleerock

Copy link
Copy Markdown
Member

Hi guys & sorry for the delay. I'm actually open for change requests and improvements, but I cannot test this feature for Oracle. At least I don't really have an interest/time in trying to make my workstation able to run Oracle and installing the drivers required to let the Node app connect to it... 🙄
EDIT - Don't want to look indelicate/rude so I'd rather be accurate: any help/commit/code change is most welcome regarding Oracle, if someone can/wants to do this part of the job.

Thats absolutely fine! Thank you for all the great job you already did. We all hate Oracle too.

Please review all comments people did, see if you won't break anything, uncomment enabledDrivers: ["postgres"] (since all drivers should pass, right? ignore oracle in your local ormconfig by setting skip: true in ormconfig)

@VinceOPS
VinceOPS force-pushed the 3118-shorten-postgres-name-of-alias-columns branch 5 times, most recently from dbd6d7b to 9589bc4 Compare February 25, 2019 14:23
@VinceOPS

Copy link
Copy Markdown
Contributor Author

Hi @pleerock

I solved the branch conflicts and handled most of the comments. Two discussions are pending:
#3514 (comment)
#3514 (comment)

The integration test has been left to "default drivers" and it helped me to notice an other issue: MySQL is throwing an error when trying to create a table whose name is longer than 64 characters (this was the case of the many-yo-many join-column, in my example). Thus, I had to apply the shortening logic to the table names too. I also added a test to check this behavior. All other tests passed too.

Any comments are most welcome.

Comment thread tsconfig.json Resolved
Comment thread CHANGELOG.md Resolved
@VinceOPS
VinceOPS force-pushed the 3118-shorten-postgres-name-of-alias-columns branch 5 times, most recently from 0724743 to b9d120e Compare February 28, 2019 15:30
@VinceOPS

Copy link
Copy Markdown
Contributor Author

@pleerock Should be fine now. Please let me know!

@pleerock

Copy link
Copy Markdown
Member

there are some compiler errors ci fails because of them

@VinceOPS
VinceOPS force-pushed the 3118-shorten-postgres-name-of-alias-columns branch from b9d120e to 6c9af3d Compare March 1, 2019 09:25
@VinceOPS

VinceOPS commented Mar 1, 2019

Copy link
Copy Markdown
Contributor Author

@pleerock

there are some compiler errors ci fails because of them

👌

@pleerock
pleerock merged commit 47780e4 into typeorm:master Mar 1, 2019
@pleerock

pleerock commented Mar 1, 2019

Copy link
Copy Markdown
Member

Finally merged! Thank you very much for your hard work!

@adrianhara

Copy link
Copy Markdown

Could this be merged also into next?

@hohler

hohler commented Mar 4, 2019

Copy link
Copy Markdown

I noticed that it's also a problem when getting relations. Specifically in the query-builder/RelationIdLoader.ts we don't use this "buildColumnAlias" at all. But it can also happen, that the alias column names are longer than e.g. 63 characters there.

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

Labels

None yet

Development

Successfully merging this pull request may close these issues.

7 participants