Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sequelize gives the wrong table name

I have a database. Where i have a "user" table

I am trying to create my first REST api using sequelize

however when it executes my query i get the following in the console:

SELECT `id`, `username`, `password`, `name`, `organization_id`, `type_id`, `join_date` FROM `users` AS `user` WHERE `user`.`id` = '1';

as you can see it tries to use a table called users however this table does not exists.

Here is some of my code:

Please do tell me if you need more i am not really sure where it goes wrong? :S

    var User = sequelize.define('user', {
    id: DataTypes.INTEGER,
    username: DataTypes.STRING,
    password: DataTypes.STRING,
    name: DataTypes.STRING,
    organization_id: DataTypes.INTEGER,
    type_id: DataTypes.INTEGER,
    join_date: DataTypes.STRING

}, {
    instanceMethods: {
        retrieveAll: function(onSuccess, onError) {
            User.findAll({}, {raw: true})
                .ok(onSuccess).error(onError);
        },
        retrieveById: function(user_id, onSuccess, onError) {
            User.find({where: {id: user_id}}, {raw: true})
                .success(onSuccess).error(onError);
        },
        add: function(onSuccess, onError) {
            var username = this.username;
            var password = this.password;

            var shasum = crypto.createHash('sha1');
            shasum.update(password);
            password = shasum.digest('hex');

            User.build({ username: username, password: password })
                .save().ok(onSuccess).error(onError);
        },
        updateById: function(user_id, onSuccess, onError) {
            var id = user_id;
            var username = this.username;
            var password = this.password;

            var shasum = crypto.createHash('sha1');
            shasum.update(password);
            password = shasum.digest('hex');

            User.update({ username: username,password: password},{where: {id: id} })
                .success(onSuccess).error(onError);
        },
        removeById: function(user_id, onSuccess, onError) {
            User.destroy({where: {id: user_id}}).success(onSuccess).error(onError);
        }
    }
});
like image 435
Marc Rasmussen Avatar asked Mar 18 '23 00:03

Marc Rasmussen


1 Answers

To solve your problem, you need to set freezeTableName = true inside your options object.

e.g.

    var User = sequelize.define('user', {
    id: DataTypes.INTEGER,
    username: DataTypes.STRING,
    password: DataTypes.STRING,
    name: DataTypes.STRING,
    organization_id: DataTypes.INTEGER,
    type_id: DataTypes.INTEGER,
    join_date: DataTypes.STRING

}, {
    freezeTableName: true,
    instanceMethods: {
        retrieveAll: function(onSuccess, onError) {
            User.findAll({}, {raw: true})
                .ok(onSuccess).error(onError);
        },
        retrieveById: function(user_id, onSuccess, onError) {
            User.find({where: {id: user_id}}, {raw: true})
                .success(onSuccess).error(onError);
        },
        add: function(onSuccess, onError) {
            var username = this.username;
            var password = this.password;

            var shasum = crypto.createHash('sha1');
            shasum.update(password);
            password = shasum.digest('hex');

            User.build({ username: username, password: password })
                .save().ok(onSuccess).error(onError);
        },
        updateById: function(user_id, onSuccess, onError) {
            var id = user_id;
            var username = this.username;
            var password = this.password;

            var shasum = crypto.createHash('sha1');
            shasum.update(password);
            password = shasum.digest('hex');

            User.update({ username: username,password: password},{where: {id: id} })
                .success(onSuccess).error(onError);
        },
        removeById: function(user_id, onSuccess, onError) {
            User.destroy({where: {id: user_id}}).success(onSuccess).error(onError);
        }
    }
});
like image 135
Yuri Zarubin Avatar answered Mar 23 '23 11:03

Yuri Zarubin