Skip to content

Synchronize issue with UUID (MySQL) #3374

Closed
@drjamesj

Description

Issue type:

[ ] question
[x] bug report
[ ] feature request
[ ] documentation issue

Database system/driver:

[ ] cordova
[ ] mongodb
[ ] mssql
[x] mysql / mariadb
[ ] oracle
[ ] postgres
[ ] sqlite
[ ] sqljs
[ ] react-native
[ ] expo

TypeORM version:

[x] latest
[ ] @next
[ ] 0.x.x (or put your version here)

Steps to reproduce or a small repository showing the problem:

I am running into issues with synchronize setting on TypeORM with UUID PrimaryGeneratedColumns on MySQL driver. It seems each time application loads it tries to re-index, so it drops the id column and tries to add it again. Issue is that since it's UUID, for MySQL the column type is varchar(36) NOT NULL PRIMARY KEY. So we run into duplicate entry '' for key 'PRIMARY' because UUID is not set.

To recreate issue, you can do it like this:

typeorm init --name MyProject --database mysql

Edit your User.ts entity to change primary key to UUID.

import { Entity, PrimaryGeneratedColumn, Column } from "typeorm";

@Entity()
export class User {

    @PrimaryGeneratedColumn('uuid')
    id: string;

    @Column()
    firstName: string;

    @Column()
    lastName: string;

    @Column()
    age: number;

}

Ensure synchronize is set to true in ormconfig.json. Start the app and it will insert sample User. Restart app and synchronize will run and try and drop and re-add the column and give error.

  message: 'ER_DUP_ENTRY: Duplicate entry \'\' for key \'PRIMARY\'',
  code: 'ER_DUP_ENTRY',
  errno: 1062,
  sqlMessage: 'Duplicate entry \'\' for key \'PRIMARY\'',
  sqlState: '23000',
  index: 0,
  sql:
   'ALTER TABLE `user` ADD `id` varchar(36) NOT NULL PRIMARY KEY',
  name: 'QueryFailedError',
  query:
   'ALTER TABLE `user` ADD `id` varchar(36) NOT NULL PRIMARY KEY',
  parameters: []

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