Skip to content

PostgreSQL sequence identifier length error #7106

@matthewberryman

Description

@matthewberryman

Issue Description

Expected Behaviour

Generated sequence name should be < NAMEDATALEN bytes, where NAMEDATALEN=64 by default.

Actual Behaviour

There is no check in

return disableEscape ? `${schema}.${tableName}_${columnName}_seq` : `"${schema}"."${tableName}_${columnName}_seq"`;
or
return disableEscape ? `${tableName}_${columnName}_seq` : `"${tableName}_${columnName}_seq"`;
that the generated sequence length fits in the default limit for identifiers. A second schema sync will throw an unhandled error, e.g.:

Error during schema synchronization:
QueryFailedError: relation "soil_classification_order_sub_orders_soilclassificationordersub" does not exist
    at new QueryFailedError (/Users/matthew/code/CBB/Nepal/API/src/error/QueryFailedError.ts:9:9)
    at Query.callback (/Users/matthew/code/CBB/Nepal/API/src/driver/postgres/PostgresQueryRunner.ts:220:30)
    at Query.handleError (/Users/matthew/code/CBB/Nepal/API/node_modules/pg/lib/query.js:139:19)
    at Client._handleErrorMessage (/Users/matthew/code/CBB/Nepal/API/node_modules/pg/lib/client.js:326:17)
    at Connection.emit (node:events:329:20)
    at Connection.EventEmitter.emit (node:domain:467:12)
    at /Users/matthew/code/CBB/Nepal/API/node_modules/pg/lib/connection.js:109:12
    at Parser.parse (/Users/matthew/code/CBB/Nepal/API/node_modules/pg-protocol/src/parser.ts:102:9)
    at TLSSocket.<anonymous> (/Users/matthew/code/CBB/Nepal/API/node_modules/pg-protocol/src/index.ts:7:48)
    at TLSSocket.emit (node:events:329:20) {
  length: 161,
  severity: 'ERROR',
  code: '42P01',
  detail: undefined,
  hint: undefined,
  position: undefined,
  internalPosition: undefined,
  internalQuery: undefined,
  where: undefined,
  schema: undefined,
  table: undefined,
  column: undefined,
  dataType: undefined,
  constraint: undefined,
  file: 'namespace.c',
  line: '423',
  routine: 'RangeVarGetRelidExtended',
  query: `ALTER TABLE "soil_classification_order_sub_orders" ALTER COLUMN "SoilClassificationOrderSubOrdersID" SET DEFAULT nextval('soil_classification_order_sub_orders_SoilClassificationOrderSubOrdersID_seq')`,
  parameters: []
}
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! nepal@1.0.0 schema:sync: `ts-node ./node_modules/typeorm/cli.js schema:sync`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the nepal@1.0.0 schema:sync script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/matthew/.npm/_logs/2020-11-24T22_40_17_327Z-debug.log

This is similar to
#3118
but for sequence names, not column aliases.

Steps to Reproduce

  1. Define an entity with a PrimaryGeneratedColumn where ${tableName}_${columnName}_seq is longer than 63 bytes.
  2. Schema sync twice.
@Entity()
export class SoilClassificationOrderSubOrders {
    @PrimaryGeneratedColumn() 
    SoilClassificationOrderSubOrdersID: number;
    @OneToOne(type => SoilClassificationOrders) @JoinColumn({name: 'SoilClassificationOrderID'})
    SoilClassificationOrderID: SoilClassificationOrders;

    @OneToOne(type => SoilClassificationSubOrders) @JoinColumn({name: 'SoilClassificationSubOrderID'})
    SoilClassificationSubOrderID: SoilClassificationSubOrders;

    @Column()
    ValidFrom: Date;
}

My Environment

Dependency Version
macOS 11.1 beta
Node.js version v15.2.1
Typescript version v4.0.2
TypeORM version v0.2.29

Additional Context

https://www.postgresql.org/docs/13/limits.html

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?

  • 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, although I believe I could do it if I had the time...
  • No, I don't have the time and I wouldn't even know how to start.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions