How to Fix SequelizeDatabaseError: Relation 'users' Does Not Exist – A Beginner's Guide
If you’re new to Sequelize (a popular ORM for Node.js) and working with databases like PostgreSQL, MySQL, or SQLite, you might have encountered the frustrating error: SequelizeDatabaseError: Relation 'users' does not exist. This error occurs when Sequelize tries to interact with a database table (in this case, users), but the table doesn’t actually exist in your database.
Don’t worry—this is a common issue, especially for beginners, and it’s usually fixable with a few simple checks. In this guide, we’ll break down what causes this error, walk through step-by-step solutions, and share tips to prevent it in the future. By the end, you’ll understand how Sequelize models, migrations, and database tables work together.
"Relation": In database terms (especially PostgreSQL), a "relation" refers to a table, view, or other database object. So "relation 'users' does not exist" simply means the users table is missing.
Why Sequelize Throws This: Sequelize acts as a bridge between your Node.js code and the database. When you define a model (e.g., a User model) and try to query it (e.g., User.findAll()), Sequelize expects a corresponding table (users) to exist in the database. If it doesn’t, you get this error.
Migrations are scripts that create or modify database tables. If you defined a User model but never ran the migration to create the users table, the table won’t exist.
Sequelize models often map to database tables, but if the model’s expected table name doesn’t match the actual table name in the database, Sequelize will look for a non-existent table.
If Sequelize is connected to the wrong database (e.g., a staging database instead of your local development database), the users table might exist in one DB but not the one you’re connected to.
4. model.sync() Was Not Called (or Misconfigured)#
In development, model.sync() can auto-create tables from models. If you forgot to call sync() or used the wrong options (e.g., force: false), the table might not be created.
Some databases (e.g., PostgreSQL on Linux) are case-sensitive. If your model references Users (capital "U") but the table is named users (lowercase), Sequelize will fail to find it.
Locate your Sequelize configuration file (usually config/config.json or config/database.js).
Check the development (or relevant environment) settings to ensure the database name, username, and password are correct.
Example config.json:
{ "development": { "username": "your_db_username", "password": "your_db_password", "database": "your_dev_database_name", // Ensure this is the DB you intend to use "host": "localhost", "dialect": "postgres" // or "mysql", "sqlite", etc. }}
Test the connection with Sequelize’s authenticate() method:
// In your database setup file (e.g., db.js)const { Sequelize } = require('sequelize');const sequelize = new Sequelize( 'your_dev_database_name', // from config 'your_db_username', 'your_db_password', { host: 'localhost', dialect: 'postgres' });// Test the connectionasync function checkConnection() { try { await sequelize.authenticate(); console.log('✅ Connected to the correct database!'); } catch (error) { console.error('❌ Failed to connect:', error); }}checkConnection();
If the connection fails, fix your config.json credentials. If it succeeds but the error persists, move to the next step.
Migrations are the recommended way to create tables (avoid manual SQL!). If the users table is missing, you likely forgot to create or run the migration.
Sequelize models default to pluralizing the model name for the table. For example:
Model name: User → Default table name: users (pluralized).
But if your migration uses a different table name (e.g., user instead of users), or your model explicitly sets a different tableName, you’ll get a mismatch.
In development, you can use model.sync() to auto-create tables from models (instead of migrations). This is faster for prototyping but not recommended for production (it can drop data!).
Call sequelize.sync() (to sync all models) or User.sync() (to sync only the User model) after defining your models:
// In your app setup (e.g., app.js)const User = require('./models/user');// Sync all models (creates tables if they don't exist)async function syncModels() { await sequelize.sync({ force: true }); // "force: true" drops tables first (use with caution!) console.log('✅ Tables synced!');}syncModels();
force: true: Drops existing tables and recreates them (use in dev to reset schema).
alter: true: Updates tables to match models (safer than force but still not for production).
Databases like PostgreSQL (on Linux) or SQL Server are case-sensitive for table names. For example:
If your model uses tableName: 'Users' (capital "U"), but the migration creates users (lowercase), Sequelize will throw "relation 'Users' does not exist".
The SequelizeDatabaseError: Relation 'users' does not exist error is a common hurdle for beginners, but it’s easily fixed by checking migrations, model-table mappings, and database connections. Remember: migrations are your best friend for managing database schema changes, and sync() is only for development!
By following the steps in this guide, you’ll not only fix the error but also gain a better understanding of how Sequelize, models, and databases work together.