Migrate: ERROR: there is no unique constraint matching given keys for referenced table "User"

Created on 11 Apr 2020  路  3Comments  路  Source: prisma/migrate

report id 3380

bu2-confirmed kinbug statuneeds-action tecengines

Most helpful comment

I can reproduce the problem.

Steps to reproduce:

  1. Have the following datamodel:
model Post {
  id        Int     @default(autoincrement()) @id
  title     String
  content   String?
  published Boolean @default(false)
  author    User?   @relation(fields: [authorId], references: [id])
  authorId  Int?
}

model User {
  id       Int     @default(autoincrement()) @id
  email    String  @unique
  password String
  name     String?
  posts    Post[]
}
  1. Migrate the database using prisma migrate

  2. Change the datamodel to the following:

model Post {
  id        Int     @default(autoincrement()) @id
  title     String
  content   String?
  published Boolean @default(false)
  author    User?   @relation(fields: [authorId], references: [uid])
  authorId  Int?
}

model User {
  uid   Int    @id
  posts Post[]
}
  1. Try migrating the database

  2. See the following error:
    image

Version used:

@prisma/cli          : 2.0.0-beta.3
Current platform     : darwin
Query Engine         : query-engine 2fb8f444d9cdf7c0beee7b041194b42d7a9ce1e6 (at /Users/harshit/.nvm/versions/node/v12.16.2/lib/node_modules/@prisma/cli/query-engine-darwin)
Migration Engine     : migration-engine-cli 2fb8f444d9cdf7c0beee7b041194b42d7a9ce1e6 (at /Users/harshit/.nvm/versions/node/v12.16.2/lib/node_modules/@prisma/cli/migration-engine-darwin)
Introspection Engine : introspection-core 2fb8f444d9cdf7c0beee7b041194b42d7a9ce1e6 (at /Users/harshit/.nvm/versions/node/v12.16.2/lib/node_modules/@prisma/cli/introspection-engine-darwin)

All 3 comments

I can reproduce the problem.

Steps to reproduce:

  1. Have the following datamodel:
model Post {
  id        Int     @default(autoincrement()) @id
  title     String
  content   String?
  published Boolean @default(false)
  author    User?   @relation(fields: [authorId], references: [id])
  authorId  Int?
}

model User {
  id       Int     @default(autoincrement()) @id
  email    String  @unique
  password String
  name     String?
  posts    Post[]
}
  1. Migrate the database using prisma migrate

  2. Change the datamodel to the following:

model Post {
  id        Int     @default(autoincrement()) @id
  title     String
  content   String?
  published Boolean @default(false)
  author    User?   @relation(fields: [authorId], references: [uid])
  authorId  Int?
}

model User {
  uid   Int    @id
  posts Post[]
}
  1. Try migrating the database

  2. See the following error:
    image

Version used:

@prisma/cli          : 2.0.0-beta.3
Current platform     : darwin
Query Engine         : query-engine 2fb8f444d9cdf7c0beee7b041194b42d7a9ce1e6 (at /Users/harshit/.nvm/versions/node/v12.16.2/lib/node_modules/@prisma/cli/query-engine-darwin)
Migration Engine     : migration-engine-cli 2fb8f444d9cdf7c0beee7b041194b42d7a9ce1e6 (at /Users/harshit/.nvm/versions/node/v12.16.2/lib/node_modules/@prisma/cli/migration-engine-darwin)
Introspection Engine : introspection-core 2fb8f444d9cdf7c0beee7b041194b42d7a9ce1e6 (at /Users/harshit/.nvm/versions/node/v12.16.2/lib/node_modules/@prisma/cli/introspection-engine-darwin)

@do4gr to reproduce to see if it's still an issue.

Still an issue: The migration changes the primary key, but a foreign key still depends on it. We'll need to adjust the order of operations.

Was this page helpful?
0 / 5 - 0 ratings