Skip to content

Validation issue for schemas with compound foreign keys #2387

@sdnts

Description

@sdnts

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions