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

Validation issue for schemas with compound foreign keys #2387

Closed
sdnts opened this issue May 6, 2020 · 1 comment
Closed

Validation issue for schemas with compound foreign keys #2387

sdnts opened this issue May 6, 2020 · 1 comment
Assignees
Labels
bug/2-confirmed Bug has been reproduced and confirmed. kind/bug A reported bug. tech/engines Issue for tech Engines. topic: schema
Milestone

Comments

@sdnts
Copy link
Contributor

sdnts commented May 6, 2020

Bug description

The following schema is invalid:

model User {
  id           Int  @id
  // 
  favoriteGame Game @relation("user_game")
}

model Game {
  id     Int    @id
  name   String
  // 
  userId Int
  userName String
  user   User   @relation("user_game", fields: [userId, userName], references: [id])
}

because:

  1. The fields in the @relation of model Game has two values, so references must have two values as well
  2. In order for references to have two values, the User model must have a unique contraint on two fields.

The correct schema should be:

model User {
  id           Int
  name String
  // 
  favoriteGame Game @relation("user_game")

  @@id(fields: [id, name])
}

model Game {
  id       Int    @id
  name     String
  // 
  userId   Int
  userName String
  user     User   @relation("user_game", fields: [userId, userName], references: [id, name])
}

How to reproduce

Type the above schema into a VSCode window.

Expected behavior

Red squiggly lines in the VSCode extension, and a validation error when generating Prisma Client or running Migrate commands

Prisma information

prisma: 2.0.0-alpha.1187

Environment & setup

Irrelevant

@mavilein mavilein added this to the Beta 5 milestone May 6, 2020
@mavilein mavilein added the tech/engines Issue for tech Engines. label May 6, 2020
@mavilein mavilein self-assigned this May 6, 2020
@janpio janpio modified the milestones: Beta 5, Beta 6 May 12, 2020
@pantharshit00 pantharshit00 added bug/2-confirmed Bug has been reproduced and confirmed. kind/bug A reported bug. topic: schema labels May 16, 2020
@pantharshit00
Copy link
Contributor

I can confirm this behavior

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug/2-confirmed Bug has been reproduced and confirmed. kind/bug A reported bug. tech/engines Issue for tech Engines. topic: schema
Projects
None yet
Development

No branches or pull requests

4 participants