question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Inconsistent query result: Field {field name} is required to return data, got `null` instead.

See original GitHub issue

Bug description

I’m performing a huge query in my database that uses many conditional filters (that may or may not exist in the API call/query parameters), and after that I’m trying to select a handful of fields to return. The issue is that one of the fields I’m trying to select is a relation that is not present in all my records. I wanted to select a field from this relation in all records that do have it, but not for the ones which don’t. I’m not sure if there’s a way to conditionally retrieve a relation’s field inside a select block, but if I just try to select that field the app crashes with the following message:

Error: 
Invalid `prisma.metadataPub.findMany()` invocation:
Inconsistent query result: Field geneIDToTaxInfoAccNumb is required to return data, got `null` instead.

How to reproduce

Perform a query to my backend without setting the conditionals that guarantee that all returned records will have a geneIDToPMID relation, and then select the taxID field within geneIDToTaxInfoAccNumb within geneIDToPMID.

Expected behavior

Return an object containing the query results with all selected fields, including records that do and records that don’t have a relation to geneIDToPMID, and selecting the taxID field on all records that do have that relation, but not on those that don’t.

Prisma information

Relevant query bit:

const papers = await prisma.metadataPub.findMany({
  where: {
    // ... all the required and optional query parameters
    // Forcing the existence of the relation is an optional parameter
    ...(options.filters?.forceGeneIDs && {geneIDToPMID: {some: {}}}),
    // ... all the required and optional query parameters
  },
  select: {
    // all fields and relations that are always present
    // this is the one that bugs the call:
    geneIDToPMID: { 
        select: {
          geneIDToTaxInfoAccNumb: {
            select: {
              taxID: true,
            },
          },
        },
      },
  }
})

Prisma schema:

model Classification2ndLay {
  pmid        Int         @unique(map: "PMID") @map("PMID")
  score       Decimal     @db.Decimal(8, 7)
  probability Decimal     @db.Decimal(8, 7)
  other__     Int
  otherTerms  String?     @db.VarChar(2000)
  known__     Int
  knownTerms  String?     @db.VarChar(2000)
  coverage    String      @db.VarChar(10)
  metadataPub MetadataPub @relation(fields: [pmid], references: [pmid], onDelete: Cascade, map: "fkMetadataPubPMID2ndLay")

  @@index([probability], map: "probability2nd")
  @@map("classification_2ndLay")
}

model Classification1stLay {
  pmid        Int         @unique(map: "PMID") @map("PMID")
  score       Decimal     @db.Decimal(8, 7)
  probability Decimal     @db.Decimal(8, 7)
  other__     Int
  otherTerms  String?     @db.VarChar(2000)
  known__     Int
  knownTerms  String?     @db.VarChar(2000)
  coverage    String      @db.VarChar(10)
  metadataPub MetadataPub @relation(fields: [pmid], references: [pmid], onDelete: Cascade, map: "fkMetadataPubPMID1stLay")

  @@index([probability], map: "probability")
  @@map("classifications_1stLay")
}

model GeneIDToPMID {
  pmid                    Int @map("PMID")
  geneID                  Int @map("geneIDs")
  metadataPub             MetadataPub             @relation(fields: [pmid], references: [pmid], onDelete: Cascade, map: "fkMetadataPubPMIDgeneIDs")
  geneIDToTaxInfoAccNumb  GeneIDToTaxInfoAccNumb  @relation(fields: [geneID], references: [geneID], onDelete: Cascade, map: "geneIDs_PMIDs_ibfk_2")

  @@id([pmid, geneID])
  @@index([geneID], map: "geneIDs_PMIDs_ibfk_2")
  @@map("geneIDs_PMIDs")
}

model GeneIDToTaxInfoAccNumb {
  geneID        Int             @id @map("geneIDs")
  orgTaxName    String?         @db.VarChar(200) @map("OrgTaxName")
  taxID         Int?            @map("TaxID")
  accNumb       String?         @db.VarChar(2000) @map("AccNumb")
  taxPath       TaxPath?        @relation(fields: [taxID], references: [taxID], onDelete: Cascade, map: "taxIDsAccNumb_ibfk_1")
  geneIDToPMID  GeneIDToPMID[]

  @@index([taxID], map: "taxIDsAccNumb_ibfk_1")
  @@map("geneIDs_taxInfo_AccNumb")
}

model MetadataPub {
  pmid                   Int                     @id @map("PMID")
  title                  String                  @db.VarChar(2000) @map("Titles")
  abstract               String?                 @db.VarChar(10000) @map("Abstract")
  pubDate                String?                 @db.VarChar(15) @map("PubDate")
  yearPub                Int?                    @map("YearPub")
  lastAuthor             String?                 @db.VarChar(30) @map("LastAuthor")
  journalAbbrev          String?                 @db.VarChar(70) @map("Journal_Abbrev")
  journal                String?                 @db.VarChar(100) @map("Journal")
  volume                 String?                 @db.VarChar(30) @map("Volume")
  issue                  String?                 @db.VarChar(30) @map("Issue")
  pages                  String?                 @db.VarChar(30) @map("Pages")
  languagePub            String?                 @db.VarChar(30) @map("LanguagePub")
  citations              Int?                    @map("Citations")
  classification2ndLay   Classification2ndLay?
  classification1stLay   Classification1stLay?
  geneIDToPMID           GeneIDToPMID[]

  @@index([citations], map: "citations")
  @@index([yearPub], map: "yearPub")
  @@map("metadataPub")
}

model TaxPath {
  taxID                   Int                       @id @map("TaxID")
  orgLineage              String?                   @db.VarChar(3000) @map("OrgLineage")
  lineagePath             String?                   @db.VarChar(3000) @map("LineagePath")
  geneIDToTaxInfoAccNumb  GeneIDToTaxInfoAccNumb[]

  @@map("taxPath")
}

Full query object:

const papers = await prisma.metadataPub.findMany({
    where: {
      classification1stLay: {
        probability: {
          gte: options.firstLayerRange.min / 100,
          lte: options.firstLayerRange.max / 100,
        },
      },
      classification2ndLay: {
        probability: {
          gte: options.secondLayerRange.min / 100,
          lte: options.secondLayerRange.max / 100,
        },
      },
      ...(options.taxonID && {
        geneIDToPMID: {
          some: {geneIDToTaxInfoAccNumb: {taxID: options.taxonID}},
        },
      }),
      ...(options.geneIDs &&
          (Array.isArray(options.geneIDs) ?
               {
                 OR: options.geneIDs.map((geneID) => ({
                                           geneIDToPMID: {some: {geneID}},
                                         })),
               } :
               {geneIDToPMID: {some: {geneID: options.geneIDs}}})),
      ...(options.filters?.excludeHosts && {
        geneIDToPMID: {
          none: {
            geneIDToTaxInfoAccNumb: {
              taxPath: {
                OR: [
                  {lineagePath: {contains: '7742'}},
                  {orgLineage: {contains: 'Vertebrata'}},
                  {taxID: 7742},
                ],
              },
            },
          },
        },
      }),
      ...(options.filters?.forceGeneIDs && {geneIDToPMID: {some: {}}}),
      ...(options.terms &&
          (Array.isArray(options.terms) ?
               {
                 OR: options.terms.map((term) => ({
                                         OR: [
                                           {title: {contains: term}},
                                           {abstract: {contains: term}},
                                         ],
                                       })),
               } :
               {
                 OR: [
                   {title: {contains: options.terms}},
                   {abstract: {contains: options.terms}},
                 ],
               })),
      ...(options.lastAuthor &&
          (Array.isArray(options.lastAuthor) ?
               {
                 OR: options.lastAuthor.map((author) => ({
                                              lastAuthor: {contains: author},
                                            })),
               } :
               {lastAuthor: {contains: options.lastAuthor}})),
      ...(options.language && {languagePub: {contains: options.language}}),
      ...(options.journal &&
          {journal: options.journal}),  // Check this for ci collation
      ...(!options.allDates && options.dateRange && options.dateRange.min &&
          options.dateRange.max && {
            AND: [
              {yearPub: {gte: options.dateRange.min}},
              {yearPub: {lte: options.dateRange.max}},
            ],
          }),
      ...(options.citations && {
        OR: options.citations.map((citationRange) => ({
                                    AND: [
                                      {citations: {gte: citationRange[0]}},
                                      {citations: {lte: citationRange[1]}},
                                    ],
                                  })),
      }),
    },
    select: {
      pmid: true,
      title: true,
      yearPub: true,
      lastAuthor: true,
      citations: true,
      classification1stLay: {
        select: {
          probability: true,
        },
      },
      classification2ndLay: {
        select: {
          probability: true,
        },
      },
      geneIDToPMID: {
        select: {
          geneIDToTaxInfoAccNumb: {
            select: {
              taxID: true,
            },
          },
        },
      },
    },
  });

Environment & setup

  • OS: Ubuntu
  • Database: MySQL
  • Node.js version: v16.15.0

Prisma Version

prisma                  : 3.15.1
@prisma/client          : 3.15.1
Current platform        : debian-openssl-1.1.x
Query Engine (Node-API) : libquery-engine 461d6a05159055555eb7dfb337c9fb271cbd4d7e (at node_modules/@prisma/engines/libquery_engine-debian-openssl-1.1.x.so.node)
Migration Engine        : migration-engine-cli 461d6a05159055555eb7dfb337c9fb271cbd4d7e (at node_modules/@prisma/engines/migration-engine-debian-openssl-1.1.x)
Introspection Engine    : introspection-core 461d6a05159055555eb7dfb337c9fb271cbd4d7e (at node_modules/@prisma/engines/introspection-engine-debian-openssl-1.1.x)
Format Binary           : prisma-fmt 461d6a05159055555eb7dfb337c9fb271cbd4d7e (at node_modules/@prisma/engines/prisma-fmt-debian-openssl-1.1.x)
Default Engines Hash    : 461d6a05159055555eb7dfb337c9fb271cbd4d7e
Studio                  : 0.462.0

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
ricsands2801commented, Oct 12, 2022

I don’t think this should prevent the studio (and prisma cloud) from loading the rows as you cannot fix it using those tools and instead have to connect directly to the DB to resolve.

0reactions
riezkyfirdauzcommented, Jul 14, 2022

hi, i have same issue like this

Inconsistent query result: Field standart is required to return data, got nullinstead. at RequestHandler.handleRequestError

then i found because some foreign key is uppercase, its bug? or i must check every create data this foreign key must be lowercase image

Read more comments on GitHub >

github_iconTop Results From Across the Web

findUnique query returns null for array fields - prisma
I read the Prisma Relations documentation and it fixed my findMany query which is able to return valid data but ...
Read more >
Inconsistent query results from az cli, returning null values ...
Right now, when I run it, I get back the name:name field, but the rest return null. Why does the middle query not...
Read more >
Null and undefined (Reference) - Prisma
In the following example, if emailInput is null , the query sets email (a mandatory field) to undefined - which means ✓ do...
Read more >
Trailing spaces give inconsistent query results - Allen Browne
Use Trim() when parsing or importing. Use TEXT rather than CHAR when creating fields in DDL, and specify the size. Use the equal...
Read more >
Why Is My Field Null? - Relay
The simplest reason a field might be null is that the server explicitly returned null. This can happen in two cases: The server's...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found