Query using Sequelize resulting in "column "id" does not exist" error

Asked

Viewed 493 times

0

I am testing the REST API I am developing. I am using Sequelize and Postgresql. In the API there is the table users with the following fields:

  • user_id
  • user_nickname
  • user_fullname
  • user_password
  • created_at
  • updated_at

When trying to insert or return users the following error is pointed out in the Nodejs console:

Executing (default): SELECT "id", "user_nickname", "user_fullname", >"user_password", "created_at" AS "createdAt", "updated_at" AS "updatedAt" FROM >"users" AS "User"; Unhandledpromiserejectionwarning: Sequelizedatabaseerror: column "id" does not >exist

The Migration made through Sequelize was the following:

module.exports = {
  up: async (queryInterface, Sequelize) => {
    return queryInterface.createTable('users', {
      user_id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true,
        allowNull: false
      },
      user_nickname: {
        type: Sequelize.STRING,
        allowNull: false
      },
      user_fullname:{
        type: Sequelize.STRING,
        allowNull: false
      },
      user_password: {
        type: Sequelize.STRING,
        allowNull: false
      },
      created_at: {
        type: Sequelize.DATE,
        allowNull: false
      },
      updated_at: {
        type: Sequelize.DATE,
        allowNull: false
      }
    });
  },

  down: async (queryInterface, Sequelize) => {
    return queryInterface.dropTable('users');
  }
};

And the inherited class of Model is the following:

const { Model, DataTypes } = require('sequelize')

class User extends Model {
  static init(connection) {
    super.init({
      user_nickname: DataTypes.STRING,
      user_fullname: DataTypes.STRING,
      user_password: DataTypes.STRING
    }, {
      sequelize: connection
    })
  }
}

module.exports = User

The query is being made like this:

async findAll(req, res) {
  const users = await User.findAll()

  return res.json(users)
}

I entered some users by the database itself to try to make a SELECT through Sequelize in the API, but results in the same error.

  • The consultation is apparently wrong. You did SELECT "id", ... while you should be wearing SELECT "user_id"...

1 answer

2


The field id is the default primary key used by Sequelize when none is reported in the model, which is your case (see method init of User).

  • Migrations are used to create the structure of your database;
  • Models serve as a model to perform queries, insertions and etc. with Sequelize. It is based on the model that Sequelize knows how its table is.

That is, Sequelize does not know the structure of its database, only knows what specified in Model.

Your User should be something like:

class User extends Model {
  static init(connection) {
    super.init({
      user_id: { primaryKey: true, type: DataTypes.INTEGER }, // Indicando a chave primária
      user_nickname: DataTypes.STRING,
      user_fullname: DataTypes.STRING,
      user_password: DataTypes.STRING
    }, {
      sequelize: connection
    })
  }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.