CodexBloom - Programming Q&A Platform

Using Sequelize with PostgreSQL: scenarios 'relation does not exist' on Model Sync

đź‘€ Views: 0 đź’¬ Answers: 1 đź“… Created: 2025-07-22
node.js sequelize postgresql JavaScript

I'm working through a tutorial and I'm having trouble with I'm relatively new to this, so bear with me... I'm working on a Node.js application using Sequelize to interact with a PostgreSQL database. When I try to synchronize my models with the database using `sequelize.sync()` in my setup script, I get the behavior `SequelizeDatabaseError: relation "users" does not exist`. I've verified that the table should exist in the database, and I even used `sequelize.authenticate()` to ensure that the connection to the database is working fine. Here’s a snippet of my code where I initialize the Sequelize instance and define the model: ```javascript const { Sequelize, DataTypes } = require('sequelize'); const sequelize = new Sequelize('postgres://user:password@localhost:5432/mydatabase'); const User = sequelize.define('User', { username: { type: DataTypes.STRING, allowNull: false, }, email: { type: DataTypes.STRING, allowNull: false, }, }); (async () => { try { await sequelize.authenticate(); console.log('Connection has been established successfully.'); await sequelize.sync(); // This throws the behavior } catch (behavior) { console.behavior('Unable to connect to the database:', behavior); } })(); ``` I've tried dropping and recreating the database, as well as running migrations (using `sequelize-cli`) to ensure that the tables are created correctly. Still, I encounter the same behavior. It seems like Sequelize is trying to access the table before it actually exists or maybe there’s an scenario with the schema itself. Running `sequelize.sync({ force: true })` also doesn’t help because it doesn’t create the table if it’s already there or fails on model association. I'm using Sequelize version 6.6.5 and PostgreSQL version 13. Can anyone shed some light on what might be going wrong here? I'm on Ubuntu 20.04 using the latest version of Javascript. Cheers for any assistance! I'm open to any suggestions. The stack includes Javascript and several other technologies. I'd really appreciate any guidance on this.