Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Synchronize issue with UUID (MySQL) #3374

Closed
drjamesj opened this issue Jan 7, 2019 · 16 comments
Closed

Synchronize issue with UUID (MySQL) #3374

drjamesj opened this issue Jan 7, 2019 · 16 comments

Comments

@drjamesj
Copy link

drjamesj commented Jan 7, 2019

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: []
@pleerock
Copy link
Member

pleerock commented Jan 7, 2019

please provide some reproduction

@drjamesj
Copy link
Author

drjamesj commented Jan 7, 2019

Hi @pleerock , I wrote some steps above to reproduce the issue, did I miss something?

@chudnyi
Copy link

chudnyi commented Jan 7, 2019

Same issue

@pleerock
Copy link
Member

pleerock commented Jan 7, 2019

do you have records in your database before you change it to uuid?

@drjamesj
Copy link
Author

drjamesj commented Jan 7, 2019

No records before changing it. But once set to UUID, I then insert sample data which all works fine. Upon restarting app it tries to synchronize by dropping the column and adding it again, which fails because it's not generating a UUID at that point.

@pleerock
Copy link
Member

pleerock commented Jan 7, 2019

Sounds like uuid isn't being generated

@drjamesj
Copy link
Author

drjamesj commented Jan 7, 2019

Thanks, how to fix?

@pleerock
Copy link
Member

pleerock commented Jan 7, 2019

first we need to write a test that will with this issue, then we need to find a place where its being generated and see why its not generating (if its an issue) then fix it.

@vladimir-kanev
Copy link

Hitting the same problem.

Do you mean it is expected to drop the primary key column? What if its values are referenced and used somewhere else?

@sangheestyle
Copy link

We have a similar problem.

By the way, I have a question. Bipolar said 'Upon restarting app it tries to synchronize by dropping the column and adding it again'. I saw the same log with mysql 5.7. Can anybody tell me why columns are dropped?

@sangheestyle
Copy link

@pleerock Hi! Could you tell me where do I start to fix this problem? I want to spend time to solve this issue.

pleerock added a commit that referenced this issue Jan 13, 2019
@pleerock
Copy link
Member

pleerock commented Jan 13, 2019

fix in #3234 caused this bug. I pushed fix and released 0.2.12-rc.2 with this fix.

@tlimp
Copy link

tlimp commented Jun 15, 2021

The issue is closed, but ist fixed? I am experiencing the exact same issue with the aurora-data-api driver.
Is this a general issue, or some implementation details in the driver?

@Cobular
Copy link

Cobular commented Aug 2, 2021

Yep experienced the same in the postgres driver, it doesn't seem to be dropping everything in the synchronize.

@pleerock
Copy link
Member

pleerock commented Aug 3, 2021

Please create a new issue with the new details if you face problems.

@N-Andronopoulos
Copy link

Regression 0.3.16 related to #10090

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

No branches or pull requests

8 participants