Skip to content

StringUtils shorten() function can result in collisions #9544

Closed

Description

@michaelbromley

Issue Description

The shorten function is used to ensure that an alias in a generated query does not exceed the maximum allowed by the DB.

It does this by truncating words such that UserShoppingCart__order__market__director becomes UsShCa__orde__mark__dire.

Users have reported the following error:

ER_NONUNIQ_TABLE: Not unique table/alias: 'SuStInTr__suSt__prVaFiFrAs'"

This has been traced down to the shorten() method, which will generate an identical alias for these two non-identical inputs.

Expected Behavior

Unique inputs should always result in unique outputs.

Actual Behavior

console.log(shorten('SupplierStockInTransit__supplierStock__productVariant_customFields_tryonFrameAsset'));
// -> 'SuStInTr__suSt__prVaFiFrAs'
console.log(shorten('SupplierStockInTransit__supplierStock__productVariant_customFields_visualFrameAsset'));
// -> 'SuStInTr__suSt__prVaFiFrAs'

Steps to Reproduce

  1. Copy the code from the shorten source into a file.
  2. Give it the two inputs I specify above
  3. Observe identical outputs
  export function shorten(input: string, options: IShortenOptions = {}): string {
    const { segmentLength = 4, separator = "__", termLength = 2 } = options;


    const segments = input.split(separator);
    const shortSegments = segments.reduce((acc: string[], val: string) => {
        // split the given segment into many terms based on an eventual camel cased name
        const segmentTerms = val.replace(/([a-z\xE0-\xFF])([A-Z\xC0-\xDF])/g, "$1 $2").split(" ");
        // "OrderItemList" becomes "OrItLi", while "company" becomes "comp"
        const length = segmentTerms.length > 1 ? termLength : segmentLength;
        const shortSegment = segmentTerms.map(term => term.substr(0, length)).join("");


        acc.push(shortSegment);
        return acc;
    }, []);


    return shortSegments.join(separator);
  }

console.log(shorten('SupplierStockInTransit__supplierStock__productVariant_customFields_tryonFrameAsset'));
console.log(shorten('SupplierStockInTransit__supplierStock__productVariant_customFields_visualFrameAsset'));

Reproduction in the context of a running TypeORM app can be found here: https://github.com/tianyingchun/vendure-issue and further described in:

My Environment

Dependency Version
Operating System Windows
Node.js version 16.15.1
Typescript version 4.3.5
TypeORM version 0.2.45

Relevant Database Driver(s)

I'll put yes on the ones I tested on, but I'm pretty sure it affects more or all.

DB Type Reproducible
aurora-mysql no
aurora-postgres no
better-sqlite3 no
cockroachdb no
cordova no
expo no
mongodb no
mysql yes
nativescript no
oracle no
postgres no
react-native no
sap no
spanner no
sqlite no
sqlite-abstract no
sqljs no
sqlserver no

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

  • ✅ Yes, I have the time, and I know how to start.
  • ✖️ Yes, I have the time, but I don't know how to start. I would need guidance.
  • ✖️ No, I don’t have the time, but I can support (using donations) development.
  • ✖️ No, I don’t have the time and I’m okay to wait for the community / maintainers to resolve this issue.

Activity

  1. github-actions commented on Sep 18, 2025

    @github-actions

    TypeORM has returned to active development after recently transitioning to new leadership (see related announcement).

    As part of the new team's efforts to prioritise work on the project moving forward, it is necessary to clean up the backlog of stale issues.

    🧹 This issue is being automatically closed because it has had no activity in the last 6 months.
    If you believe this issue is still relevant, feel free to comment below and the maintainers may reopen it. Thank you for your contribution.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions