SequelizeDatabaseError:SQLITE_ERROR:没有这样的表:Users

时间:2019-11-05 11:55:04

标签: node.js sqlite sequelize.js sequelize-cli

我正在将Sequelize与sqlite一起使用,当我尝试向表中插入数据时出现错误

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

我正在使用sequelize-cli进行迁移。

这是我的文件结构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
    }
});

运行:顺序迁移:创建--name用户表

我得到了文件

'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');

  }
};

这是由sequelize-cli生成的config.json文件

{
  "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
  }
}

这是用户模型创建用户的文件

authenticationController.js

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

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

1 个答案:

答案 0 :(得分:1)

回答可能很晚,但我刚刚找到了解决方法

这个问题发生在我身上,因为我已经对数据库进行了更改。

所以您必须先删除表并重新创建它。

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


我们使用上面的代码创建数据库 您只需要按如下所示放置表格


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