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

Postgres primary key of type uuid: default value migration/sync not working #5898

Closed
joel31415 opened this issue Apr 18, 2020 · 2 comments · Fixed by #8274
Closed

Postgres primary key of type uuid: default value migration/sync not working #5898

joel31415 opened this issue Apr 18, 2020 · 2 comments · Fixed by #8274

Comments

@joel31415
Copy link

Issue type:

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

Database system/driver:
[x] postgres

TypeORM version:

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

Steps to reproduce or a small repository showing the problem:
When converting @PrimaryColumn('uuid') to @PrimaryGeneratedColumn('uuid') the migrations/synchronization does not actually update the default value to uuid_generate_v4(), it just drops and recreates all foreign keys. So it will never automatically sync/migrate completely.
Given these entities:

import {  Entity, PrimaryGeneratedColumn,  ManyToOne,  OneToMany,  JoinColumn,  PrimaryColumn } from 'typeorm'

@Entity()
export class User {
  @PrimaryColumn('uuid')
  // @PrimaryGeneratedColumn('uuid')
  id: string

  @OneToMany(() => Document, (doc) => doc.owner, { onDelete: 'CASCADE' })
  docs: Document[]
}

@Entity()
export class Document {
  @PrimaryColumn('uuid')
  id: string
  @ManyToOne(() => User, (user) => user.docs, { onDelete: 'CASCADE' })
  @JoinColumn({ name: 'ownerId' })
  owner
}

When swapping User's id to be @PrimaryGeneratedColumn('uuid') the resulting up migration looks like:

await queryRunner.query(`ALTER TABLE "document" DROP CONSTRAINT "FK_2d617266bd4cbb6ebcfdb5f67e2"`, undefined);
await queryRunner.query(`ALTER TABLE "document" ADD CONSTRAINT "FK_2d617266bd4cbb6ebcfdb5f67e2" FOREIGN KEY ("ownerId") REFERENCES "user"("id") ON DELETE CASCADE ON UPDATE NO ACTION`, undefined);

As you can see, it does not actually update the default value for the id column in the user table. It just recreates all foreign keys.

I believe there's another bug when going the other way too, the migration tries to remove a sequence that does not exist, the up migration looks like:

await queryRunner.query(`ALTER TABLE "document" DROP CONSTRAINT "FK_2d617266bd4cbb6ebcfdb5f67e2"`, undefined);
await queryRunner.query(`ALTER TABLE "user" ALTER COLUMN "id" DROP DEFAULT`, undefined);
await queryRunner.query(`DROP SEQUENCE "user_id_seq"`, undefined);
await queryRunner.query(`ALTER TABLE "document" ADD CONSTRAINT "FK_2d617266bd4cbb6ebcfdb5f67e2" FOREIGN KEY ("ownerId") REFERENCES "user"("id") ON DELETE CASCADE ON UPDATE NO ACTION`, undefined);

When using uuid there are no sequences. But when the sequence is removed from the migration, it does update the default value for the id column and future sync/migrations will be empty.

With this small case it's easy to see the issue, in my real-world case I'm migrating a code base to TypeORM and wanted to set a generated primary key uuid on an existing table.

Also, thanks for a great library!

@Hesowcharov
Copy link

I met the same problem - when switching from PrimaryKey('uuid') to PrimaryGeneratedColumn('uuid'), typeorm generates a migration which re-creates all foreign key constraints related with this table.

"typeorm": {
      "version": "0.2.25",
      ...

@OmgImAlexis
Copy link

PrimaryGeneratedColumn is working fine for me. I only got this issue when using PrimaryColumn with a custom default set.

AlexMesser added a commit to edeesis/typeorm that referenced this issue Feb 12, 2022
AlexMesser pushed a commit that referenced this issue Feb 12, 2022
…d/remove the DEFAULT value (#8274)

* fix(8273) Adding @generated('uuid') will now generate a migration to add the DEFAULT value

* implemented fix for "increment" generation type;
implemented fix for generation removal;
improved tests;

* added test for #5898

Co-authored-by: AlexMesser
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants