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.

Sqlite client: ConversionError(cannot parse integer from empty string)

See original GitHub issue

Bug description

I’m seeing the following exception when running a query:

PrismaClientUnknownRequestError3 [PrismaClientUnknownRequestError]: 
Invalid `prisma.elections.findMany()` invocation:


  Error occurred during query execution:
ConnectorError(ConnectorError { user_facing_error: None, kind: ConversionError(cannot parse integer from empty string) })
    at PrismaClientFetcher.request (/Users/bsouthga/projects/elections/node_modules/@prisma/client/runtime/index.js:78125:15)
    at processTicksAndRejections (node:internal/process/task_queues:93:5)
    at async getResults (webpack-internal:///./pages/elections/[year]/[office]/index.tsx:111:10)
    at async getStaticProps (webpack-internal:///./pages/elections/[year]/[office]/index.tsx:151:19)
    at async renderToHTML (/Users/bsouthga/projects/elections/node_modules/next/dist/next-server/server/render.js:28:109)
    at async /Users/bsouthga/projects/elections/node_modules/next/dist/next-server/server/next-server.js:98:97
    at async __wrapper (/Users/bsouthga/projects/elections/node_modules/next/dist/lib/coalesced-function.js:1:330)
    at async DevServer.renderToHTMLWithComponents (/Users/bsouthga/projects/elections/node_modules/next/dist/next-server/server/next-server.js:123:387)
    at async DevServer.renderToHTML (/Users/bsouthga/projects/elections/node_modules/next/dist/next-server/server/next-server.js:124:874)
    at async DevServer.renderToHTML (/Users/bsouthga/projects/elections/node_modules/next/dist/server/next-dev-server.js:34:578) {
  clientVersion: '2.13.1'
}

How to reproduce

  • Run query (in Prisma Information section).
  • Can provide a copy of the sqlite db for debugging to a team member if that helps.

Expected behavior

No exception, query is successfully executed.

Prisma information

Query:

const client = new PrismaClient();
return await client.elections.findMany({
  select: {
    locations: {
      select: {
        fips: true,
        name: true,
        district: true,
        state: true,
      },
    },
    results: {
      select: {
        votes: true,
        parties: {
          select: {
            name: true,
          },
        },
        candidates: {
          select: {
            name: true,
          },
        },
      },
    },
  },
  where: {
    year: 2016,
    offices: {
      name: 'president'
    },
  },
});

Schema:

generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "sqlite"
  url      = "file:../data/elections.db"
}

model candidates {
  id      Int       @id @default(autoincrement())
  name    String    @unique
  results results[]
}

model election_totals {
  election_id Int       @unique
  total_votes Int
  elections   elections @relation(fields: [election_id], references: [id])
}

model elections {
  id              Int              @id @default(autoincrement())
  location_id     Int
  office_id       Int
  year            Int
  special         Boolean?         @default(false)
  runoff          Boolean?         @default(false)
  locations       locations        @relation(fields: [location_id], references: [id])
  offices         offices          @relation(fields: [office_id], references: [id])
  election_totals election_totals?
  results         results[]

  @@unique([year, office_id, location_id, special, runoff], name: "elections_id")
  @@index([location_id], name: "location_id_idx")
  @@index([office_id], name: "office_id_idx")
  @@index([year], name: "year_idx")
}

model locations {
  id        Int         @id @default(autoincrement())
  state     String
  LEVEL     String
  district  Int?
  fips      Int?
  name      String?
  elections elections[]

  @@unique([state, LEVEL, district, fips], name: "location_id")
  @@index([fips], name: "fips_idx")
  @@index([district], name: "district_idx")
  @@index([LEVEL], name: "level_idx")
  @@index([state], name: "state_idx")
}

model offices {
  id        Int         @id @default(autoincrement())
  name      String      @unique
  elections elections[]
}

model parties {
  id      Int       @id @default(autoincrement())
  name    String    @unique
  results results[]

  @@index([name], name: "name_idx")
}

model results {
  election_id  Int
  candidate_id Int
  party_id     Int
  votes        Int
  candidates   candidates @relation(fields: [candidate_id], references: [id])
  elections    elections  @relation(fields: [election_id], references: [id])
  parties      parties    @relation(fields: [party_id], references: [id])

  @@unique([election_id, candidate_id, party_id], name: "sqlite_autoindex_results_1")
}

Environment & setup

  • OS: MacOS 11.1 (20C69)
  • Database: SQLite
  • Node.js version: v15.3.0
  • Prisma version:
@prisma/cli          : 2.13.1
@prisma/client       : 2.13.1
Current platform     : darwin
Query Engine         : query-engine fcbc4bb2d306c86c28014f596b1e8c7980af8bd4 (at node_modules/@prisma/engines/query-engine-darwin)
Migration Engine     : migration-engine-cli fcbc4bb2d306c86c28014f596b1e8c7980af8bd4 (at node_modules/@prisma/engines/migration-engine-darwin)
Introspection Engine : introspection-core fcbc4bb2d306c86c28014f596b1e8c7980af8bd4 (at node_modules/@prisma/engines/introspection-engine-darwin)
Format Binary        : prisma-fmt fcbc4bb2d306c86c28014f596b1e8c7980af8bd4 (at node_modules/@prisma/engines/prisma-fmt-darwin)
Studio               : 0.329.0

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
pimeyscommented, Jan 7, 2021

Hey, I’ve been investigating this issue today and here are my findings.

TLDR: inconsistencies in the data.

The query in the question is not the only one that fails, but let’s look into what’s going on with it:

We get a conversion error trying to parse an integer from a string. This is unfortunate with SQLite due to it not having almost any typing at all. The row that breaks is this:

id    fips  name               district  state
----  ----  -----------------  --------  -----
1955        Statewide writein            NA

Now looking into the schema, it says the following (unimportant parts removed):

model locations {
  id        Int         @id @default(autoincrement())
  fips      Int?
  name      String?
  district  Int?
  state     String
}

There are two empty fields that might cause this, so let’s see what they actually are:

sqlite> select id, fips, name, district, state from locations where id = 1955 and district is null;
id    fips  name               district  state
----  ----  -----------------  --------  -----
1955        Statewide writein            NA

This tells us that district is null, which maps fine to our type of Int?.

select id, fips, name, district, state from locations where id = 1955 and fips is null;

And this one then returns nothing, meaning the value of fips is not null, but an empty string. If we define in the schema the field to be Int?, we can only parse integers or null values from the database, which is the case happened here.

I dug a bit deeper, and found some other inconsistencies too:

sqlite> select distinct(special) from elections;
special
-------
0
FALSE
TRUE
NA

And the data model (the interesting part):

model elections {
  special         Boolean?         @default(false)
}

This would mean we can have values of 0 (false), 1 (true) and null stored to the column, and loosening up the requirements, values of TRUE and FALSE should also work (if the column subtype is a boolean). If you’d query the row with a value of NA, we would crash again due to not being able to convert NA to a boolean.

My suggestion is to go through all the data carefully, iron out the inconsistencies to work together with the schema. It’s also quite tricky to introspect the SQLite database, and sometimes we should be looking into the data too instead of the schema only. This is a case that our introspection should catch, giving an error describing problems with the existing data, or at least warnings.

1reaction
pantharshit00commented, Dec 31, 2020

Thanks for information. I can reproduce this now. We will need fix the conversion issue.

Consolidated reproduction: https://github.com/harshit-test-org/prisma-issue-4778

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I cast a string to integer and have 0 in case of error in ...
And the question asks to convert empty strings ( '' ) to 0 , but not ... This function will always return 0...
Read more >
Casting - DuckDB
For example, trying trying to cast the string 'hello' to an INTEGER will result in an error being thrown. TRY_CAST can be used...
Read more >
sqlite3 wasm docs: Check-in [bd2d5a287f]
As a special case, if it's passed only a single integer argument, the string form of that argument is the result of sqlite3.capi.sqlite3_js_rc_str()...
Read more >
C-style API - SQLite
The function sqlite3_js_rc_str() accepts an integer argument and returns the JS string form of its corresponding symbolic name, or returns undefined if no...
Read more >
All Reports with description - SQLite CVSTrac
1. please add a new format in printf (like %k) to convert a string to a ... If a MATCH query using OR...
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