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

Not set Name Constraint > Unique #8494

Open
SebaBoler opened this issue Dec 31, 2021 · 6 comments
Open

Not set Name Constraint > Unique #8494

SebaBoler opened this issue Dec 31, 2021 · 6 comments

Comments

@SebaBoler
Copy link

Issue Description

I have @unique with name but in database name constraint have random value
Database : Postgresql 13

Expected Behavior

New Constraint with properly name from entity

Actual Behavior

Random name contraint

@ObjectType()
@Entity({ name: 'Event', schema: 'public' })
@Unique('UQ_EVENTSHORTNAME', ['eventShortName'])
@Index('idx_eventId', ['eventId'])
export class Event extends CoreEntity {
  @HideField()
  @PrimaryGeneratedColumn('increment')
  @Field(() => Int, { nullable: false })
  @IsInt()
  readonly eventId: number;

  @Field({ nullable: false })
  @Generated('uuid')
  @Column({ nullable: false })
  @IsUUID()
  readonly eventUUID: string;

  @Field(() => String, { nullable: false })
  @Column({ nullable: false })
  @MinLength(3, { message: 'Your eventShortName must be greater than 3' })
  @IsString()
  eventShortName: string;

Zrzut ekranu 2021-12-31 o 13 19 03

Dependency Version
Node.js version 16.9.1
Typescript version 4.0.5
TypeORM version 0.2.41

Relevant Database Driver(s)

Postgresql 13

@zoton2
Copy link

zoton2 commented Mar 9, 2022

I also have this issue, only difference is I'm on PostgreSQL 12.4, but I imagine this is an issue in the library code and not the database side anyway.

Another note is, for me at least, this only happens if you're targeting one property, like in the example above, if you target multiple properties, it works fine.

There were older issues for this that were closed because it was assumed to be fixed by PR #7561, but that OP says...

Fixed @Unique constraint name behavior in SQLite. Looks like other drivers was already fixed because test is successfully passing.

...although 2/3 of the issues they linked were PostgreSQL, so I guess there were either issues with the tests, or something changed between then and current version (should try the older version myself and see). One of the issues mentions that dropping the table fully would fix it, but didn't test, and obviously a bit destructive. There's also a "workaround" posted here which can be used to find the constraint name so you can at least use it programatically.

@SebaBoler
Copy link
Author

@zoton2 thank you for your reply. I used @Index

@Mordorreal
Copy link

@zoton2 This issue still exist for PostgreSQL 14.4

@neverovski
Copy link

neverovski commented May 12, 2023

@zoton2 thank you for your reply. I used @Index

@Index doesn't work for me. I need a constraint, not an index.

If you specify only one value in columnNames, typeOrm generates a random name. But you have more one values in columnNames, @unique works correctly

For example

Case 1:

export const UQ_CITY: UniqueOptions = {
  name: 'UQ_CITY',
  columnNames: ['name'],
};

typeorm generate sql command:

ALTER TABLE "cities" ADD CONSTRAINT "UQ_a0ae8d83b7d32359578c486e7f6" UNIQUE ("name");

Case 2:

export const UQ_CITY: UniqueOptions = {
  name: 'UQ_CITY',
  columnNames: ['name', 'countryId'],
};

typeorm generate sql command:

ALTER TABLE "cities" ADD CONSTRAINT "UQ_CITY" UNIQUE ("name", "countryId");

@VeniciusMarafon
Copy link

@zoton2
Any update? i have the same issue.

pg: 8.11.2
typeorm: 0.3.17

@TechGeorgii
Copy link

Same error, PostgreSQL 15.3, typeorm: 0.3.17

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

6 participants