Fix Drizzle "SqliteError: no such table" error — how to create tables
I tried Drizzle ORM with SQLite but got stuck on "SqliteError: no such table". Here's how I solved the error and created SQLite tables from a Drizzle schema.
I was trying to build something with Drizzle but I couldn’t figure out how to actually create tables in my SQLite database. Here are the docs I wish I’d had that would have saved me an hour of Googling things.
Create a new Node project
If you don’t already have one, create a new Node project.
Update package.json
In package.json, make the highlighted changes. More on the command prefixed with db: later in this tutorial.
Add tsconfig.json
You can use any TypeScript setup you prefer, but here’s the one I tested with.
Create a Drizzle config file
Create a SQLite database schema
Your schema can define any tables you need for your app. Here’s what mine looks like:
Create the SQLite database tables from your Drizzle schema
Right now, the schema is defined but the tables haven’t been created in the SQLite database yet.
This is where I got stuck. I tried to run my app, but I kept getting a SqliteError: no such table: users error. I couldn’t find any reference to how to actually create the database tables in the Drizzle docs.
After searching around, I figured out two ways to get the SQLite tables created using Drizzle:
Generate a migration (the SQL query to create the tables as defined in your schema), which can be applied manually, using Drizzle’s migrate helper, or with third-party tools — this is a production-friendly approach
Push the schema changes directly, which is exactly what I was looking for to set up my local database and is what I’ll cover in this tutorial.
Both generating migrations and pushing schemas can be done using Drizzle’s CLI helper, Drizzle Kit.
Push the SQLite database changes using Drizzle Kit
For prototyping, local dev, or initializing a new database, the push command will use the defined schema to update the database — which, in our case, will create the missing tables.
View the SQLite database in Drizzle Studio
To verify that the tables were created, you can use Drizzle Studio, which gives you a browser-based interface for viewing and working with your SQLite data.
This runs drizzle-kit studio under the hood and will start up Drizzle Studio at https://local.drizzle.studio/.
Test CRUD in SQLite using Drizzle ORM
Now the tables exist, you can actually insert and select data from the database.
To try this out, create index.ts at the root of the project and add the following:
After saving this, start the dev server by running npm run dev and you’ll see output similar to the following:
This creates both a user and an idea, then does a partial select to create a combined view of the idea and user.
If you’re like me and this is your first time using Drizzle and SQLite: congratulations! You just created and interacted with a database in a Node and TypeScript project, and you’re ready to build your data-powered app.
Happy building!
Learn With Jason is made possible by our sponsors: