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

"called Option::unwrap() on a None value" on mismatch between Prisma and database schema #21219

Open
jlkravitz opened this issue Sep 25, 2023 · 7 comments
Assignees
Labels
bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. kind/bug A reported bug. team/client Issue for team Client. tech/engines Issue for tech Engines. topic: relations topic: rust panic topic: sql server Microsoft SQL Server topic: (@)@unique topic: unwrap error topic: views

Comments

@jlkravitz
Copy link

jlkravitz commented Sep 25, 2023

Hi Prisma Team! My Prisma Client just crashed. Perhaps related to #10586?

This is coming up repeatedly and really making it difficult to work with prisma...

This is the report:

Versions

Name Version
Node v18.15.0
OS darwin-arm64
Prisma Client 4.16.2
Query Engine 4bc8b6e1b66cb932731fb1bdbbc550d1e010de81
Database sqlserver

Logs

prisma:tryLoadEnv Environment variables loaded from .env
prisma:client checkPlatformCaching:postinstall false
prisma:client checkPlatformCaching:ciName 
prisma:tryLoadEnv Environment variables loaded from .env
prisma:client dirname node_modules/.prisma/client
prisma:client relativePath ../../../prisma
prisma:client cwd prisma
prisma:client protocol graphql
prisma:client clientVersion 4.16.2
prisma:client clientEngineType library
prisma:client:libraryEngine internalSetup
prisma:client:engines:resolveEnginePath enginePath node_modules/.prisma/client/libquery_engine-darwin-arm64.dylib.node
prisma:client:libraryEngine library starting
prisma:client:libraryEngine library started
prisma:client:libraryEngine sending request, this.libraryStarted: true
prisma:client:libraryEngine sending request, this.libraryStarted: true
prisma:client:libraryEngine sending request, this.libraryStarted: true
prisma:client:request_handler {"name":"PrismaClientRustPanicError","clientVersion":"4.16.2"}
prisma:client:libraryEngine sending request, this.libraryStarted: true
pri

Client Snippet

getData: authProcedure.query(async () => {
    return prisma.actions.findMany({
      select: {
        request: { select: { request_id: true } },
      },
    });
  })

Schema

model actions {
  request_id       Int
  request          all_request? @relation("ActionFirstRequest", fields: [request_id], references: [request_id], onDelete: NoAction, onUpdate: NoAction)
  
  // ... more fields

  @@index([request_id])
}

view all_request {
  request_id                          Int       @unique
// ... more fields
}
@aqrln aqrln added bug/0-unknown Bug is new, does not have information for reproduction or reproduction could not be confirmed. kind/bug A reported bug. tech/engines Issue for tech Engines. topic: sql server Microsoft SQL Server team/client Issue for team Client. topic: rust panic topic: unwrap error labels Oct 2, 2023
@aqrln
Copy link
Member

aqrln commented Oct 2, 2023

@jlkravitz thanks for the report, could you please provide the missing information so we could reproduce the issue (the query that is failing and the schema)?

@jlkravitz
Copy link
Author

jlkravitz commented Oct 2, 2023

@jlkravitz thanks for the report, could you please provide the missing information so we could reproduce the issue (the query that is failing and the schema)?

@aqrln Just added a minimal example based on my code that produces the error.

(Note that the error persists even if I copy the data from the all_request view into a table.)

@jlkravitz
Copy link
Author

Hi @aqrln I did some more digging. The issue is related to #13340.

I marked the column request_id as unique on the all_request model, but it is in fact not unique. So when I fetch related records of actions, Prisma expects a unique ID on its request field. My bad, but this should probably show a clearer error message?

@Jolg42 Jolg42 added bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. and removed bug/0-unknown Bug is new, does not have information for reproduction or reproduction could not be confirmed. labels Oct 10, 2023
@janpio janpio changed the title called Option::unwrap() on a None value "called Option::unwrap() on a None value" on mismatch between Prisma and database schema May 24, 2024
@janpio
Copy link
Member

janpio commented May 24, 2024

@jlkravitz That makes a lot of sense. Optimally Prisma would of course not crash here, but return some useful error message to the user what went wrong - according to your description it queried for 1 entry (as indicated by the relation and @unique) but got back multiple ones. Seems we currently just do not have any code to handle that case.


But I have trouble to reproduce the situation you are describing. When I try to manually create a schema similar to the one above, 2 tables with a relation to a non-unique field, with this SQL:

-- CreateTable
CREATE TABLE [dbo].[actions] (
    [id] INT NOT NULL IDENTITY(1,1),
    [request_id] INT NOT NULL,
    CONSTRAINT [actions_pkey] PRIMARY KEY CLUSTERED ([id])
);

-- CreateTable
CREATE TABLE [dbo].[request2] (
    [id] INT NOT NULL IDENTITY(1,1),
    [request_id] INT NOT NULL,
    CONSTRAINT [request2_pkey] PRIMARY KEY CLUSTERED ([id]),
    -- CONSTRAINT [request2_request_id_key] UNIQUE NONCLUSTERED ([request_id])
);

-- AddForeignKey
ALTER TABLE [dbo].[actions] ADD CONSTRAINT [actions_request_id_fkey] FOREIGN KEY ([request_id]) REFERENCES [dbo].[request2]([request_id]) ON DELETE NO ACTION ON UPDATE CASCADE;

(Note the commented out UNIQUE constraint)

I get an error message from SQL server on creation:

There are no primary or candidate keys in the referenced table 'dbo.request2' that match the referencing column list in the foreign key 'actions_request_id_fkey'.

Seems SQL Server does not allow you to create a schema with a foreign key (to represent the relation) that points to a non unique column?

Is this maybe where the views come in?
Or does your table not actually have a foreign key, but you only added that to the Prisma schema?

@jlkravitz
Copy link
Author

jlkravitz commented May 24, 2024

@janpio I admit it's been a while since I encountered this issue, but if I recall - yes, this is where views come in. In theory, the view could be some complicated SQL query under the hood. There's a world in which I think one of the columns is unique (or at least I expect it to be), but am wrong perhaps because I made an incorrect assumption about the query.

In this case, I marked the field as unique in the Prisma schema to improve ergonomics when querying with Prisma. But that was incorrect, so I got an error downstream that didn't make much sense at the time.

@janpio
Copy link
Member

janpio commented May 24, 2024

Does that mean the @relation was also not really backed by a foreign key, but that it only existed in the Prisma schema so that the API is generated to use it like one? Then we could indeed end up in a situation that could work like yours.

@jlkravitz
Copy link
Author

jlkravitz commented May 25, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. kind/bug A reported bug. team/client Issue for team Client. tech/engines Issue for tech Engines. topic: relations topic: rust panic topic: sql server Microsoft SQL Server topic: (@)@unique topic: unwrap error topic: views
Projects
None yet
Development

No branches or pull requests

4 participants