typeorm / typeorm

ORM for TypeScript and JavaScript. Supports MySQL, PostgreSQL, MariaDB, SQLite, MS SQL Server, Oracle, SAP Hana, WebSQL databases. Works in NodeJS, Browser, Ionic, Cordova and Electron platforms.
http://typeorm.io
MIT License
33.46k stars 6.21k forks source link

Naming single column unique constraint with decorator not working as expected #2376

Closed hoonoh closed 3 years ago

hoonoh commented 5 years ago

Issue type:

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

Database system/driver:

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

TypeORM version:

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

Steps to reproduce or a small repository showing the problem:

Naming unique constraints with @Unique decorator works for constraints with multiple columns but for constraints with single column it generates random name.

@Entity("users")
@Unique("unique-email", ["email"])
@Unique("unique-email-nickname", ["email", "nickname"])
export class User {
...

generates queries:

ALTER TABLE "users" ADD CONSTRAINT "UQ_97672ac88f789774dd47f7c8be3" UNIQUE ("email")
ALTER TABLE "users" ADD CONSTRAINT "unique-email-nickname" UNIQUE ("email", "nickname")
y4nnick commented 5 years ago

It also occurs in mysql.

anodynos commented 4 years ago

It would be great if we could manually name the constraint on foreign keys, unique etc on the decorator level, for example @PrimaryGeneratedColumn({constraint: 'User_primary_key'}

Also it would be more amazing to have a default strategy, better than FK_xxxx etc, to mimic the above, for example ${entity}_${column$}_reason, like 'user_id_primaryKey' oruser_name_unique` or `` 'user_id_foreignKey_references_user_id' etc.

This will allow typeorm to properly document the DB schema and interoperability with other tools that use or generate such schema details.

PedramMarandi commented 4 years ago

Is there any workaround for this issue?

PedramMarandi commented 4 years ago

Instead, I could use @Index:

 @Index(`email_uniq_index`, ['email'], { unique: true })

But it does not generate a constraint :(

philefstat commented 3 years ago

One workaround, if you need to get the name of the constraint:

const tableName = connection.getMetadata(Entity).tableName
const constraintName = connection.namingStrategy.uniqueConstraintName(
  tableName,
  ['columnName']
)
demarchenac commented 3 years ago

Is this issue still happening?

TvojTatk0 commented 3 years ago

Yes it's still there. Typeorm ver 0.2.25, pg ver. 8.3.0

closquet commented 3 years ago

this issue is still happening :(

@Unique("unique-email", ["email"])

generates queries:

ALTER TABLE "users" ADD CONSTRAINT "UQ_97672ac88f789774dd47f7c8be3" UNIQUE ("email")

clintonb commented 2 weeks ago

This issue still exists with TypeORM 0.3.17 and Postgres 15.x.

I encounter the exact same issue as detailed in https://github.com/typeorm/typeorm/issues/8494#issuecomment-1545606498.