SequelizeDatabaseError: SQLITE_ERROR: no such table: Users [英] SequelizeDatabaseError: SQLITE_ERROR: no such table: Users

查看:71
本文介绍了SequelizeDatabaseError: SQLITE_ERROR: no such table: Users的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

这是我的文件结构 img

文件结构

globalConfigs.js

const dotenv = require('dotenv');dotenv.config({路径:'./config.env'});模块.出口 = {端口:process.env.PORT ||9000,数据库:{方言:'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("用户", {ID: {类型:Sequelize.INTEGER(11),allowNull: 假,自动增量:真,主键:真,},用户名: {类型:Sequelize.STRING(35),allowNull: 假,独特:真实},密码: {类型:Sequelize.STRING(20),allowNull: 假}});

running : sequelize migration:create --name user_table

我得到了文件

'use strict';模块.出口 = {up: (queryInterface, Sequelize) =>{返回 queryInterface.createTable('users', {ID: {类型:Sequelize.INTEGER(11),allowNull: 假,自动增量:真,主键:真,},用户名: {类型:Sequelize.STRING(35),allowNull: 假,独特:真实},密码: {类型:Sequelize.STRING(20),allowNull: 假},createdAt: Sequelize.DATE,更新时间:Sequelize.DATE});},向下: (queryInterface, Sequelize) =>{return queryInterface.dropTable('users');}};

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

{发展": {"用户名": "root",密码":空,主机":127.0.0.1",方言":sqlite",存储":./db/dbFiles/portfolio.sqlite"},测试": {"用户名": "root",密码":空,"database": "database_test",主机":127.0.0.1","方言": "mysql",operatorsAliases":false},生产": {"用户名": "root",密码":空,"database": "database_production",主机":127.0.0.1","方言": "mysql",operatorsAliases":false}}

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

authenticationController.js

const User = require("../models/userModel");export.createUser = async (req, res, next) =>{const user_created = await User.create({用户名:userteste",密码:密码"});};

解决方案

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

出现这个问题是因为我对数据库进行了更改.

所以你必须先删除表并重新创建它.

续集.同步().then(() => {//这里有东西}).catch((e) => console.log(e));

我们使用上面的代码来创建数据库你只需要删除表如下

<预><代码>续集.同步().then(() => {//这里有东西返回 sequelize.drop();}).catch((e) => console.log(e));

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"
        });
  
};

解决方案

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

这篇关于SequelizeDatabaseError: SQLITE_ERROR: no such table: Users的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆