nestjsx / crud

NestJs CRUD for RESTful APIs
https://github.com/nestjsx/crud/wiki
MIT License
4.04k stars 533 forks source link

Using composite primary keys creates many independent single-column indexes instead of one unique multi-column index #787

Closed nikelborm closed 2 years ago

nikelborm commented 2 years ago

Bug Report

Current behavior

I create a table. That table has composite primary key consist from 2 columns. On both columns, I use @Column({ primary: true }). It creates a table with sufficiently formed primary key.

@Entity({ name: 'available_for_launch_testing' })
export class AvailableForLaunchTesting {
  @Column({
    name: 'abstract_testing_id',
    primary: true,
    nullable: false,
  })
  abstractTestingId!: number;

  @Column({
    name: 'educational_space_id',
    primary: true,
    nullable: false,
  })
  educationalSpaceId!: number;

...other columns
}

It produces migration like that:

CREATE TABLE "available_for_launch_testing" (
        "abstract_testing_id" integer NOT NULL,
        "educational_space_id" integer NOT NULL,
        "created_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),
        CONSTRAINT "PK_54a12fbdc9989bd921c8cbb4372" PRIMARY KEY ("abstract_testing_id", "educational_space_id")
      )
CREATE INDEX "IDX_f7d26e320ec68f35ccf8ce14be" ON "available_for_launch_testing" ("educational_space_id")
CREATE INDEX "IDX_3d00e8bc90bc12c19a824ade7b" ON "available_for_launch_testing" ("abstract_testing_id")

The problem is that it produces two indexes instead of one.

I also tried to add multicolumn index manually by

@Index(['abstractTestingId', 'educationalSpaceId'], { unique: true })

and it added this index

CREATE UNIQUE INDEX "IDX_54a12fbdc9989bd921c8cbb437" ON "available_for_launch_testing" ("abstract_testing_id", "educational_space_id")

It is exactly what I want!

But those 2 indexes are still here. And I didn't find the way to remove them. If I remove them, they appear in the next migration. I tried to blacklist them by

@Index('IDX_f7d26e320ec68f35ccf8ce14be', { synchronize: false })
@Index('IDX_3d00e8bc90bc12c19a824ade7b', { synchronize: false })

But it looks like it has no effect for that case.

Expected behavior

Using composite primary key causes multicolumn index creation. And because it is primary key as a consequence index should be unique.

Environment


Package version: 0.3.6


For Tooling issues:
- Node version: 16.15.0
- Platform:  Linux
- Database:  Postgres