9

我正在尝试使用 Sequelize 构建一个简单的 Node/Express 应用程序,但是当我尝试在我的关系数据库中创建新记录时,我收到了错误消息Unhandled rejection SequelizeDatabaseError: SQLITE_ERROR: no such table: main.User。基本上,我在表中创建一个用户,Users然后尝试在表中创建一个相关地址Addresses- 用户已成功创建,但在创建地址时失败并出现此错误......它main从表名中获取前缀的位置? (下面的完整错误读数)...

首先,这是我的程序的概要......

  • 我的 Sequelize 版本是Sequelize [Node: 6.8.1, CLI: 2.4.0, ORM: 3.29.0],我使用 Sequelize CLI 命令sequelize init设置我项目的这一部分。

  • 我正在使用 SQLite3 进行本地开发,并且config/config.json我将开发数据库定义为

    "development": {
        "storage": "dev.sqlite",
        "dialect": "sqlite"
    }
    
  • 我的用户迁移:

    'use strict';
        module.exports = {
            up: function(queryInterface, Sequelize) {
                return queryInterface.createTable('Users', {
                    id: {
                        allowNull: false,
                        autoIncrement: true,
                        primaryKey: true,
                        type: Sequelize.INTEGER
                    },
                    first_name: {
                        type: Sequelize.STRING
                    },
                    last_name: {
                        type: Sequelize.STRING
                    },
                    createdAt: {
                        allowNull: false,
                        type: Sequelize.DATE
                    },
                    updatedAt: {
                        allowNull: false,
                        type: Sequelize.DATE
                    }
                });
            },
            down: function(queryInterface, Sequelize) {
                return queryInterface.dropTable('Users');
            }
        };
    
  • 和地址迁移(缩写):

    module.exports = {
        up: function(queryInterface, Sequelize) {
            return queryInterface.createTable('Addresses', {
                id: {
                    allowNull: false,
                    autoIncrement: true,
                    primaryKey: true,
                    type: Sequelize.INTEGER
                },
                address_line_one: {
                    type: Sequelize.STRING
                },
                UserId: {
                    type: Sequelize.INTEGER,
                    allowNull: false,
                    references: {
                        model: "User",
                        key: "id"
                    }
                }
            })
        }
    
  • 用户模型:

    'use strict';
    module.exports = function(sequelize, DataTypes) {
        var User = sequelize.define('User', {
            first_name: DataTypes.STRING,
            last_name: DataTypes.STRING
        }, {
       classMethods: {
           associate: function(models) {
               models.User.hasOne(models.Address);
           }
        }
     });
    return User;
    };
    
  • 和地址模型:

    'use strict';
    module.exports = function(sequelize, DataTypes) {
        var Address = sequelize.define('Address', {
            address_line_one: DataTypes.STRING,
            UserId: DataTypes.INTEGER
        }, {
            classMethods: {
                associate: function(models) {
                    models.Address.hasOne(models.Geometry);
                    models.Address.belongsTo(models.User, {
                        onDelete: "CASCADE",
                        foreignKey: {
                            allowNull: false
                        }
                    });
                }
              }
          });
     return Address;
     };
    
  • 最后,我的路线index.js

    router.post('/createUser', function(req, res){
        var firstName = req.body.first_name;
        var lastName = req.body.last_name;
        var addressLineOne = req.body.address_line_one;
    
        models.User.create({
            'first_name': newUser.firstName,
            'last_name': newUser.lastName
        }).then(function(user){         
            return user.createAddress({
                'address_line_one': newUser.addressLineOne
        })
    })
    

因此,当我尝试发布到 时/createUser,将成功创建用户,并且控制台会说已创建新地址(INSERT INTO 'Addresses'...),但未创建地址并记录以下错误:

Unhandled rejection SequelizeDatabaseError: SQLITE_ERROR: no such table: main.User at Query.formatError (/Users/darrenklein/Desktop/Darren/NYCDA/WDI/projects/world_table/wt_test_app_1/node_modules/sequelize/lib/dialects/sqlite/query.js:348:14) at afterExecute (/Users/darrenklein/Desktop/Darren/NYCDA/WDI/projects/world_table/wt_test_app_1/node_modules/sequelize/lib/dialects/sqlite/query.js:112:29) at Statement.errBack (/Users/darrenklein/Desktop/Darren/NYCDA/WDI/projects/world_table/wt_test_app_1/node_modules/sqlite3/lib/sqlite3.js:16:21)

几个月前,我用 Sequelize 做过一次这样的事情,它很成功,我一生都无法弄清楚我在这里缺少什么。为什么应用程序在寻找main.User,我怎样才能让它寻找正确的表格?谢谢!

4

1 回答 1

10

啊哈!一个小错误使我的整个操作脱轨。在迁移文件中,references.model一定要复数!

references: {
    model: "Users",
    key: "id"
}

繁荣。

于 2017-01-09T22:04:20.420 回答