Skip to content

@Column({ type: "uuid" }) should be varchar(36) on MySQL instead of varchar(255) #7228

Open

Description

@dickfickling

Feature Description

In MySQL, we use varchar(36) by default for UUID primary key columns (see here:

).

The Problem

When I create a uuid column with @Column({ type: "uuid" }) it's using varchar(255)

The Solution

I've dealt with this for now by using @Column({ type: "uuid", length: 36 }) but it seems like a simple change to make 36 the default length for uuid columns. If I threw up a PR for that, would anybody object?

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.

Activity

  1. yf-hk commented on Jan 14, 2021

    @yf-hk

    What about char(36)? varchar means variable lenth character.

  2. dickfickling commented on Jan 14, 2021

    @dickfickling
    Author

    good point, char(36) makes more sense

  3. arimus commented on Jul 31, 2021

    @arimus

    This is a serious breaking change and is creating serious issues with migrations. Now that the default for UUID is char(32), previously existing varchar(32) columns are being dropped and replaced in migration generation scripts, with all the corresponding effects (really bad).

    At the very least, detection of an existing varchar field of the same length should be detected and left alone. Alternatively and probably better would be the ability to add an option to choose between char / varchar rather than it being forced. Migrating all primary keys in tables between types is less than ideal and a show stopper for many reasons.

    I can create a new ticket, but it seems this one should be opened back up and fixed?

  4. imnotjames commented on Jul 31, 2021

    @imnotjames
    Contributor

    Migrating all primary keys in tables between types

    This doesn't affect primary keys. Just UUIDs.

    My suggestion would be to write a migration that you would prefer instead of using the automatically generated one.
    The automatically generated ones err on the side of EXACTNESS which may or may not be correct.

    Please feel free to open another issue if you believe something needs to be done differently.

  5. arimus commented on Jul 31, 2021

    @arimus
  6. imnotjames commented on Jul 31, 2021

    @imnotjames
    Contributor

    Got it - it switches it from a 36 character varchar to a 36 character char.

    You're right that it's a baseline for hand written changes. However, not all changes are easy to write generations for.

    In particular, what you're describing sounds less like a problem with this change exactly and more with a problem in how we do changes to columns. In this case an ALTER COLUMN to set the type would be more appropriate than dropping & creating a new column.

  7. arimus commented on Aug 1, 2021

    @arimus

    Yeah, an alter column would have been a preferable and less destructive change.

    Still, it's also a lot of noise during each migration generation, particularly if one were to want to leave the varchar(32) and not change the column type. Seems like both changes would be good to have. If you are amenable to a flag to allow varchar (old behavior), I can help with a PR. The advantage here is that even if folks are only using schema generation for something like testing, they can get still things to match up without having to migrate to the new column types. This is nice for folks that are trying to make minimal changes in production setups.

  8. pleerock commented on Aug 2, 2021

    @pleerock
    Member

    @imnotjames we should try to avoid these kind of changes, because they are considered as breaking. If user don't have a data backup and accidentally run it on production - it's a big problem. I would recommend to publish a new version with revert of this fix.

    Before 0.2.0 we had ALTER COLUMN approach instead of drop/create for the columns with "changed type", but we had so many issues and complexity because of them, that we decided with @AlexMesser to keep it simple - if column type has completely changed - drop and re-create the column. I don't think we should go back to ALTER COLUMN, at least for now.

    We can figure out a new mechanism (for example, cli command "typeorm upgrade") - that can solve these kind of issues.

  9. maitrungduc1410 commented on Sep 24, 2021

    @maitrungduc1410

    this is serious breaking change, it directly affects our production database after syncing. Previously default for uuid is varchar(36) now you changed to char(36) without any backward compatibility, we synced, and all the columns with uuid are cleared

  10. yf-hk commented on Sep 24, 2021

    @yf-hk

    Well, it's a bad practice to sync on a production database... But it's surely an issue. Maybe we need to tag it to a different major version

  11. devshorts commented on Sep 28, 2021

    @devshorts

    I mean we sync on our sandbox database and it dropped a bunch of primary keys and then blew up errors. Thankfully we were able to restore data from backups, but this a terrible side effect of what should really be called out and labeled as a major breaking change

  12. 4 remaining items

  13. ArielPrevu3D commented on Jan 17, 2023

    @ArielPrevu3D

    Here's a workaround to bring back char, for the brave among us. Don't use this outside of migration code.

    export default config.then((c) => {
      const dataSource = new DataSource(c);
      const driver = dataSource.driver as unknown as
        | typeof dataSource.driver
        & { oldNormalizeType: typeof dataSource.driver.normalizeType };
      driver.oldNormalizeType = driver.normalizeType;
      driver.normalizeType = (column: {
        type: ColumnType;
        length?: number | string;
        precision?: number | null;
        scale?: number;
      }): string => {
        if (column.type === 'uuid') {
          column.length = 36;
          return 'char';
        }
        return driver.oldNormalizeType(column);
      };
    
      return dataSource;
    });
  14. gnat commented on May 8, 2023

    @gnat

    A little heads up for people in here: VARCHAR() stores the length as 1 byte, so there's no storage or performance difference between VARCHAR(36) and VARCHAR(255).

    Also, VARCHAR(255) may actually save space because CHAR(36) will always allocate a length of 36, because no length is stored.. mysql will always allocate 36. (Real life example: an unused foreign key will have 36 allocated anyway).

    Also, lol @ everyone getting primary key migrations. Yeah.. that's bad.

  15. ArielPrevu3D commented on May 8, 2023

    @ArielPrevu3D

    A little heads up for people in here: VARCHAR() stores the length as 1 byte, so there's no storage or performance difference between VARCHAR(36) and VARCHAR(255).

    Also, VARCHAR(255) may actually save space because CHAR(36) will always allocate a length of 36, because no length is stored.. mysql will always allocate 36. (Real life example: an unused foreign key will have 36 allocated anyway).

    Also, lol @ everyone getting primary key migrations. Yeah.. that's bad.

    VARCHAR(255) will never save space compared to CHAR(36) for this usecase.

  16. gnat commented on May 8, 2023

    @gnat

    The devil is in the details. @ArielPrevu3D See: https://dev.mysql.com/doc/refman/8.0/en/char.html

    image

    Your unused reference / relational fields are going to take 36 bytes if CHAR(36). Only 1 byte if VARCHAR(255). 👀

  17. ArielPrevu3D commented on May 18, 2023

    @ArielPrevu3D

    The devil is in the details. @ArielPrevu3D See: https://dev.mysql.com/doc/refman/8.0/en/char.html

    image

    Your unused reference / relational fields are going to take 36 bytes if CHAR(36). Only 1 byte if VARCHAR(255). 👀

    The unused relation fields are going to be NULL.

  18. ArielPrevu3D commented on Oct 6, 2023

    @ArielPrevu3D

    Any plans on fixing this? Is there an upcoming major release where the CHAR(36) type for UUIDs would make sense? Maybe this could be configurable to avoid breaking people's migration generation.

  19. Rayyan98 commented on Nov 29, 2023

    @Rayyan98

    Shouldn't we just go to binary 16 now, in a configurable manner of course in the decorator

  20. 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.

  21. Rayyan98 commented on Sep 19, 2025

    @Rayyan98

    Would love to have a binary 16 column option behind this so i can off load some logic from my application to it

  22. reopened this on Sep 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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