SQLite
The SQLite data source connector connects Prisma ORM to a SQLite database file. These files always have the file ending .db
(e.g.: dev.db
).
By default, the SQLite connector contains a database driver responsible for connecting to your database. You can use a driver adapter (Preview) to connect to your database using a JavaScript database driver from Prisma Client.
Example
To connect to a SQLite database file, you need to configure a datasource
block in your Prisma schema:
datasource db {
provider = "sqlite"
url = "file:./dev.db"
}
The fields passed to the datasource
block are:
provider
: Specifies thesqlite
data source connector.url
: Specifies the connection URL for the SQLite database. The connection URL always starts with the prefixfile:
and then contains a file path pointing to the SQLite database file. In this case, the file is located in the same directory and calleddev.db
.
Type mapping between SQLite to Prisma schema
The SQLite connector maps the scalar types from the data model to native column types as follows:
Alternatively, see Prisma schema reference for type mappings organized by Prisma ORM type.
Native type mapping from Prisma ORM to SQLite
Prisma ORM | SQLite |
---|---|
String | TEXT |
Boolean | BOOLEAN |
Int | INTEGER |
BigInt | INTEGER |
Float | REAL |
Decimal | DECIMAL |
DateTime | NUMERIC |
Json | Not supported |
Bytes | BLOB |
Rounding errors on big numbers
SQLite is a loosely-typed database. If your Schema has a field of type Int
, then Prisma ORM prevents you from inserting a value larger than an integer. However, nothing prevents the database from directly accepting a bigger number. These manually-inserted big numbers cause rounding errors when queried.
To avoid this problem, Prisma ORM 4.0.0 and later checks numbers on the way out of the database to verify that they fit within the boundaries of an integer. If a number does not fit, then Prisma ORM throws a P2023 error, such as:
Inconsistent column data: Conversion failed:
Value 9223372036854775807 does not fit in an INT column,
try migrating the 'int' column type to BIGINT
Connection details
Connection URL
The connection URL of a SQLite connector points to a file on your file system. For example, the following two paths are equivalent because the .db
is in the same directory:
datasource db {
provider = "sqlite"
url = "file:./dev.db"
}
is the same as:
datasource db {
provider = "sqlite"
url = "file:dev.db"
}
You can also target files from the root or any other place in your file system:
datasource db {
provider = "sqlite"
url = "file:/Users/janedoe/dev.db"
}