Gabriel_costa
Gabriel_costa

Reputation: 31

SequelizeDatabaseError: SQLITE_ERROR: no such table: Users

I'm using Sequelize with sqlite and when i try to insert data to the table gives me the error

Executing (default): INSERT INTO `Users` (`id`,`username`,`password`,`createdAt`,`updatedAt`) 
VALUES (NULL,$1,$2,$3,$4);
{ SequelizeDatabaseError: SQLITE_ERROR: no such table: Users

I'm using sequelize-cli to do migrations.

This is my file structure img

file structure

globalConfigs.js

const dotenv = require('dotenv');

dotenv.config({
    path: './config.env'
});

module.exports = {
    port: process.env.PORT || 9000,
    database: {
        dialect: 'sqlite',
        sotrage: './MVC/db/dbFiles/portfolioDB.sqlite'
    }

};

dbConnectorConfig.js

const globalConfigs = require('../globalConfigs/globalconfigs');
const Sequelize = require('sequelize');


const sequelize = new Sequelize(
    globalConfigs.database
);

module.exports = sequelize;

userModel.js

const Sequelize = require("sequelize");
const sequelize = require("../db/dbConnectorConfigs");

module.exports = sequelize.define("User", {
    id: {
        type: Sequelize.INTEGER(11),
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
    },
    username: {
        type: Sequelize.STRING(35),
        allowNull: false,
        unique: true
    },
    password: {
        type: Sequelize.STRING(20),
        allowNull: false
    }
});

running : sequelize migration:create --name user_table

I get the file

'use strict';

module.exports = {
  up: (queryInterface, Sequelize) => {
    return queryInterface.createTable('users', {
      id: {
        type: Sequelize.INTEGER(11),
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
      },
      username: {
        type: Sequelize.STRING(35),
        allowNull: false,
        unique: true
      },
      password: {
        type: Sequelize.STRING(20),
        allowNull: false
      },
      createdAt: Sequelize.DATE,
      updatedAt: Sequelize.DATE

    });
  },

  down: (queryInterface, Sequelize) => {
    return queryInterface.dropTable('users');

  }
};

This is the config.json file generated by sequelize-cli

{
  "development": {
    "username": "root",
    "password": null,
    "host": "127.0.0.1",
    "dialect": "sqlite",
    "storage": "./db/dbFiles/portfolio.sqlite"

  },
  "test": {
    "username": "root",
    "password": null,
    "database": "database_test",
    "host": "127.0.0.1",
    "dialect": "mysql",
    "operatorsAliases": false
  },
  "production": {
    "username": "root",
    "password": null,
    "database": "database_production",
    "host": "127.0.0.1",
    "dialect": "mysql",
    "operatorsAliases": false
  }
}

this is the file where user model create the user

authenticationController.js

const User = require("../models/userModel");

exports.createUser = async (req, res, next) => {
  
  const user_created = await User.create({
       username: "userteste",
        password: "password"
        });
  
};

Upvotes: 0

Views: 5506

Answers (1)

Ninad Bondre
Ninad Bondre

Reputation: 57

its probably late to answer but i just found solution

this problem occurred to me because I have made changes to database.

so you have to drop the table first and recreate it.

sequelize
  .sync()
  .then(() => {
     //something here
  })
  .catch((e) => console.log(e));


we use above code to create the database you just have to drop table as below


sequelize
  .sync()
  .then(() => {
     //something here
return sequelize.drop();
  })
  .catch((e) => console.log(e));

Upvotes: 2

Related Questions