Sqlite client: ConversionError(cannot parse integer from empty string)
See original GitHub issueBug 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:
- Created 3 years ago
- Comments:8 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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:
Now looking into the schema, it says the following (unimportant parts removed):
There are two empty fields that might cause this, so let’s see what they actually are:
This tells us that district is null, which maps fine to our type of
Int?
.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 beInt?
, 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:
And the data model (the interesting part):
This would mean we can have values of
0
(false),1
(true) andnull
stored to the column, and loosening up the requirements, values ofTRUE
andFALSE
should also work (if the column subtype is a boolean). If you’d query the row with a value ofNA
, we would crash again due to not being able to convertNA
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.
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