Skip to content

generate not identifying a unique criteria in introspected schema #2142

Open

Description

@MattGson

Bug description

Updating to 2.0.0-beta.2 from 2.0.0-beta.1 causes generate to break on an introspected schema.

Error received:

 Error validating: The argument `references` must refer to a unique criteria in the related model `metrics`. But it is referencing the following fields that are not a unique criteria: `metric_id`, `group_id`

How to reproduce

The schema is fairly complicated and I have not included related tables that are not referenced in the error. I have also renamed things with unrelated joins identified as other. It looks as follows:

model values {
  metric_id       Int
  score           Float
  group_id         Int
  other1_id      Int
  other2_id         Int
  metrics         metrics         @relation(fields: [metric_id, group_id], references: [metric_id, group_id])
  other2_groups other2_groups @relation(fields: [other1_id, other2_id, group_id], references: [other1_id, other2_id, group_id])

  @@id([other1_id, other2_id, metric_id])
  @@index([metric_id, group_id], name: "fk_group_metric")
  @@index([metric_id], name: "metric_id")
  @@index([other1_id, other2_id, group_id], name: "other1_id")
}

model metrics {
  metric_id   Int           @default(autoincrement()) @id
  name        String
  group_id     Int
  groups       groups         @relation(fields: [group_id], references: [group_id])

  @@index([metric_id, group_id], name: "metric_group")
  @@index([group_id], name: "group_id")
}

As you can see there is an index on [metric_id, group_id]. Regardless of this, any combination of a primary key (metric_id) with any other column will always be unique and should be allowed as a relation.

Expected behavior

Should generate properly. Was working in beta-1 without issue.

Activity

  1. changed the title [-]beta.2 breaks generate functionality. Not identifying a unique criteria.[/-] [+]beta.2 breaks generate functionality. Not identifying a unique criteria in introspected schema.[/+] on Apr 9, 2020
  2. added
    bug/2-confirmed
    kind/bug
    on Apr 14, 2020
  3. pantharshit00 commented on Apr 14, 2020

    @pantharshit00
    Contributor

    I can confirm this behaviour. The schema used for reproduction:

    model values {
      metric_id       Int
      score           Float
      group_id         Int
      other1_id      Int
      other2_id         Int
      metrics         metrics         @relation(fields: [metric_id, group_id], references: [metric_id, group_id])
    
      @@id([other1_id, other2_id, metric_id])
      @@index([metric_id, group_id], name: "fk_group_metric")
      @@index([metric_id], name: "metric_id")
      @@index([other1_id, other2_id, group_id], name: "other1_id")
    }
    
    model metrics {
      metric_id   Int           @default(autoincrement()) @id
      name        String
      group_id     Int
    
    
      @@index([metric_id, group_id], name: "metric_group")
      @@index([group_id], name: "group_id")
    }
  4. added
    tech/engines/query engine
    tech/engines
    on Aug 5, 2020
  5. added this to the Backlog 2.5.0 milestone on Aug 5, 2020
  6. added
    tech/engines/datamodel
    and removed
    tech/engines/query engine
    on Aug 6, 2020
  7. 13 remaining items

  8. added this to the Backlog 2.6.0 milestone on Aug 18, 2020
  9. self-assigned this
    on Aug 19, 2020
  10. changed the title [-]beta.2 breaks generate functionality. Not identifying a unique criteria in introspected schema.[/-] [+]generate not identifying a unique criteria in introspected schema[/+] on Sep 1, 2020
  11. changed the title [-]generate not identifying a unique criteria in introspected schema[/-] [+]`generate` not identifying a unique criteria in introspected schema[/+] on Sep 1, 2020
  12. sn0wst0rm commented on Nov 22, 2022

    @sn0wst0rm

    Hello, has this issue been fixed? I need to mirror a status column shared by two tables, and at this moment I am forced to first create a new item in the first table and then update the status in the second one with two separate operations.

    I would really like to be able to use this native MySQL feature so that I can also assure that the operation is completed correctly without using any explicit transaction.

    I have also found the issue #2182 which says that the problem has been fixed, but the parser still does not let me use non-unique indexes with the references keyword.

    I hope you can let me know!
    Thank you

  13. Akxe commented on Apr 24, 2023

    @Akxe
    Contributor

    @janpio This is supposed to be fixed, yet I, too, am getting the error. Do I need to set some option in prisma.schema file?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

status/needs-actiontech/enginestech/engines/datamodeltopic: schema

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions