Search code examples
node.jssqlitesequelize.jssequelize-auto

Sequelize 'no such table:' despite table and database clearly existing and correctly configured


Good evening. I'm building an Express REST API using SQLite, with Sequelize being my ORM of choice. I was working with an existing database, so I used a package called sequelize-auto in order to generate my models.

Problem is, Sequelize for some reason won't see my database, a .db file, or won't detect the table, despite being clearly defined. The problem occurs in this block of code:

const Sequelize = require('sequelize');
const sequelize = new Sequelize('expenserdb', 'user', 'pass', {
    dialect: 'sqlite',
    host: '../database/expenserdb.db'
});
const initModels = require('../models/init-models');
const models = initModels(sequelize);

module.exports = class dbService {
    editUserName(newName) {
        models.User.update({Name: newName}, {
            where: {
                UserId: 1
            }
        })
        .catch((err) => {console.log(err)});               
    }
}

This is my dbService.js file, and as you can see, the database is clearly defined, with the name, location and everything being correct. Despite all of this, I'm still getting:

Error: SQLITE_ERROR: no such table: User {errno: 1, code: 'SQLITE_ERROR', sql: 'UPDATE `User` SET `Name`=$1 WHERE `UserId` = $2'

This service is being injected into my controller via awilix, and the method is called inside the controller:

const container = require("../containerConfig");

const dbService = container.resolve("dbService");

exports.runNameChange = async (req) => {
    const newName = JSON.stringify(req.body.name);
    const result = await dbService.editUserName(newName);
    return result;
};

And in turn, the controller method is ran when this endpoint is hit:

app.post('/updateuser', async function(req, res) {
    const result = await userController.runNameChange(req);
    res.status(200).send(String(result));
});

Also, the dboptions.json file:

{
    "storage":"./database/expenserdb.db"
}

I'm new to Node, Express, and Sequelize in general. I might have missed something crucial, but I can't seem to realize what. Help is greatly appreciated.


Solution

  • Update: just figured it out, you need the FULL path and not the relative path in the verbose constructor, as such:

    const sequelize = new Sequelize('expenserdb', 'user', 'pass', {
        dialect: 'sqlite',
        storage: 'E:/desktopshortcuts/yahalom/expenser/expenser-server/database/expenserdb.db'
    });