question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

breaking change on uuid column

See original GitHub issue

Issue Description

update to 0.2.35, the uuid column force to char(36) #7853 , even if set type to varchar in options

Expected Behavior

if set type to varchar, it should be varchar

Actual Behavior

always char, no matter what type is set

export const IDColumnOpts = { type: 'varchar', charset: 'ascii', length: 36 } as const;

export abstract class BaseEntity extends TypeORMBaseEntity {
  @Generated('uuid')
  @PrimaryColumn({ comment: 'ID', ...IDColumnOpts })
  id: string;
}

Steps to Reproduce

// insert code here

My Environment

Dependency Version
Operating System
Node.js version x.y.zzz
Typescript version x.y.zzz
TypeORM version x.y.zzz

Additional Context

Relevant Database Driver(s)

DB Type Reproducible
aurora-data-api no
aurora-data-api-pg no
better-sqlite3 no
cockroachdb no
cordova no
expo no
mongodb no
mysql no
nativescript no
oracle no
postgres no
react-native no
sap 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.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:7
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
maitrungduc1410commented, Sep 24, 2021

I’m having same issue, after syncing, our production database columns get cleared because of this. Thanks for new release !

1reaction
ph1pcommented, Nov 9, 2021

Hi! My colleague @jsproede and I had the same problem and since we didn’t find any other solution, we wrote a migration. Maybe this helps one or the other.

The migration reads all your entities, finds their primary/foreign keys and modifies the column type. First we need to disable the foreign key checks, after everything has gone through we need to enable them again.

The good thing about this solution is that no data is lost!

Caution: Use at your own risk!

More: #8167 #7853 Docs: https://github.com/typeorm/typeorm/blob/master/docs/migrations.md

import { MigrationInterface, QueryRunner } from 'typeorm';
import { ColumnMetadata } from 'typeorm/metadata/ColumnMetadata';

export class UUIDMigration implements MigrationInterface {
  name = 'UUIDMigration';

  public async up(queryRunner: QueryRunner): Promise<void> {
    await this.runMigration(queryRunner, 'CHAR');
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    await this.runMigration(queryRunner, 'VARCHAR');
  }

  private async runMigration(queryRunner: QueryRunner, type: 'VARCHAR' | 'CHAR') {
    await this.disableForeignKeyChecks(queryRunner);

    for (const { tableName, primaryColumns, foreignKeys } of queryRunner.connection.entityMetadatas) {
      for (const columnName of this.getRelevantPrimaryColumnNames(primaryColumns)) {
        await queryRunner.query(`ALTER TABLE ${tableName} MODIFY ${columnName} ${type}(36);`);
      }

      for (const foreignKey of foreignKeys) {
        for (const columnName of this.getRelevantForeignKeyColumnNames(foreignKey.columns)) {
          await queryRunner.query(`ALTER TABLE ${tableName} MODIFY ${columnName} ${type}(36);`);
        }
      }
    }

    await this.enableForeignKeyChecks(queryRunner);
  }

  private getRelevantForeignKeyColumnNames(foreignKeyColumns: ColumnMetadata[]) {
    return foreignKeyColumns.filter(column => column.type === 'uuid').map(column => column.databaseName);
  }

  private getRelevantPrimaryColumnNames(primaryColumns: ColumnMetadata[]) {
    return primaryColumns
      .filter(({ type, length }) => type === 'uuid' || (type === 'varchar' && length === '36'))
      .map(column => column.databaseName);
  }

  private async disableForeignKeyChecks(queryRunner: QueryRunner) {
    await queryRunner.query('SET foreign_key_checks = 0;');
  }

  private async enableForeignKeyChecks(queryRunner: QueryRunner) {
    await queryRunner.query('SET foreign_key_checks = 1;');
  }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

UUID field backed by String column in Postgres breaks `filter`
Describe the bug I'm using UUIDs for primary keys in my Fluent model representation, like this: @ID public var id: UUID?
Read more >
What is a UUID, and why should you care? - Cockroach Labs
Here's how it breaks down visually: how a version 1 UUID breaks down. Generating UUIDs in this way makes having identical UUIDs almost ......
Read more >
Recalculate UUIDs for Vulnerabilities::Findings using UUIDv5
The existing column is holding UUID V4 but the semantics are the same for V5 so we can utilize the same column and...
Read more >
Unable to store UUIDs in H2 2.0.202 with Hibernate
Setting @Column(columnDefinition = "uuid") actually resolved an issue I was having with the embedded H2 Spring Boot Test DB not returning any ...
Read more >
Should I call my UUID primary key column ID or not?
What if you have to change the data type of your foo_id column from integer to bigint ? You wouldn't want to change...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found