Sequelize in Node / Express - 'no such table: error main.User`

I am trying to create a simple Node / Express application with Sequelize, but when I try to create a new record in my relational database, I get an error Unhandled rejection SequelizeDatabaseError: SQLITE_ERROR: no such table: main.User. Basically, I create a user in a table Users, and then try to create a linked address in a table Addresses- the user was created successfully, but an error occurs when creating the address ... where does he get the Prefix mainfrom the table name? (full error reading below) ...

First of all, here is a summary of my program ...

  • My version is Sequelize Sequelize [Node: 6.8.1, CLI: 2.4.0, ORM: 3.29.0], and I used the Sequelize CLI command sequelize initto configure this part of my project.

  • I am using SQLite3 for local development, and in config/config.jsonI have a development db defined as

    "development": {
        "storage": "dev.sqlite",
        "dialect": "sqlite"
    }
    
  • My user migration:

    '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');
            }
        };
    
  • and address migration (abbreviated):

    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"
                    }
                }
            })
        }
    
  • User Model:

    '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;
    };
    
  • and address model:

    '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;
     };
    
  • finally my route 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
        })
    })
    

Therefore, when I try to send a message to /createUser, the user will be created successfully, and the console will say that the new address was created ( INSERT INTO 'Addresses'...), but the address is NOT created and the following error is logged:

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)

I did such things with Sequelize once before a few months ago, and it was successful, I can’t understand for life what I’m missing here. Why is the application looking main.User, and how can I make it look for the correct table? Thank!

+4
1

! . references.model !

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

Boom.

+4

Source: https://habr.com/ru/post/1666308/


All Articles