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

Typeorm creates migration that creates already existing unique constraint #8158

Closed
styczynski opened this issue Sep 6, 2021 · 0 comments · Fixed by #8364
Closed

Typeorm creates migration that creates already existing unique constraint #8158

styczynski opened this issue Sep 6, 2021 · 0 comments · Fixed by #8364

Comments

@styczynski
Copy link

styczynski commented Sep 6, 2021

Issue Description

Expected Behavior

Typeorm should detect the unique constraint for column was already created and skip its creation.

Actual Behavior

Typeorm does not detect the constraint is present and tries to create it again:

error: error: relation "UQ_7acfebc1c4dab180b06c83c66b2" already exists

Steps to Reproduce

  1. Clone the repo https://github.com/styczynski/express-typeorm-starter/tree/reproduce-contraints-problem
  2. Run npm install
  3. Run docker-compose -f ./docker-compose.yml up --abort-on-container-exit
  4. Run npm run migrate:up
  5. Run npm run migrate:generate NewMigration
  6. Now npm run migrate:up will fail (on the new migration).

The failure message will be:

error: error: relation "UQ_7acfebc1c4dab180b06c83c66b2" already exists

The problem is that if you change the order of constraints when setting up the table Typeorm will fail to correctly detect uniqueness of columns.
The problematic part is this (part of the User.ts model):

@Unique(["id", "userMetaId"])
@Unique(["userMetaId"])
/* ... */
@OneToOne(() => UserMeta)

The OneToOne relation requires unique constraint on the userMetaId column.
However if we create ["id", "userMetaId"] constraint and then ["userMetaId"], Typeorm will catch only the first one
and decide that the constraint is composite so the column is not unique.

This means that if we edit the initial migration and change order of constraints so that ("userMetaId") precede the other one, everything will works fine.

My Environment

Dependency Version
Operating System MacOS Catalina 10.15.7
Node.js version 12.7.0
Typescript version 3.9.7
TypeORM version 0.2.37

Additional Context

Relevant Database Driver(s)

DB Type Reproducible
aurora-data-api no
aurora-data-api-pg no
better-sqlite3 no
cockroachdb no
cordova no
expo no
mongodb no
mysql no
nativescript no
oracle no
postgres yes
react-native no
sap no
sqlite no
sqlite-abstract no
sqljs no
sqlserver no

Are you willing to resolve this issue by submitting a Pull Request?

  • ✅ Yes, I have the time, and I know how to start.
  • ✖️ Yes, I have the time, but I don't know how to start. I would need guidance.
  • ✖️ No, I don’t have the time, but I can support (using donations) development.
  • ✖️ No, I don’t have the time and I’m okay to wait for the community / maintainers to resolve this issue.

PR for this issue: #8157

AlexMesser added a commit that referenced this issue Nov 13, 2021
AlexMesser added a commit that referenced this issue Nov 14, 2021
* added fix and test for #8158

* fixed for other drivers

* fixed column length for Oracle
HeartPattern pushed a commit to HeartPattern/typeorm that referenced this issue Nov 29, 2021
* added fix and test for typeorm#8158

* fixed for other drivers

* fixed column length for Oracle
HeartPattern pushed a commit to HeartPattern/typeorm that referenced this issue Nov 29, 2021
* added fix and test for typeorm#8158

* fixed for other drivers

* fixed column length for Oracle
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
1 participant