4

I already have tables in postgresql. I want to retrieve data from them using sequelize. I used several ways to do it. But still can't map the db schma to sequelize in node.js.

  const Test = Conn.define('test', {
          race_id: 
          {
           type: Sequelize.STRING,
           allowNull: false   
           },
          race_name:
          {
           type: Sequelize.STRING,
           allowNull: false
          }
   });

  Conn.sync(); 
export default Conn;

I define the schema in node.js and have a table with same schema in postgresql.

But when execute Db.models.test.findAll({ where: args });

The console will output result as following:

Executing (default): SELECT "id", "race_id", "race_name", "createdAt", "updatedAt" FROM "tests" AS "test";

And I will get another output that is

column \"id\" does not exist

I can't figure out why sequelize retrieve data from tests table and it retrieve columns that I don't have.

How could I select table from existing tables using sequelize? I need to use ORM for requirement and can't use other way to get data.

1 Answer 1

4

You should define your primary key :

const Test = Conn.define('test', {
      race_id: 
      {
       type: Sequelize.STRING,
       allowNull: false,
       primaryKey: true
       },
      race_name:
      {
       type: Sequelize.STRING,
       allowNull: false
      }
});

This will replace id in your query with your primary key.

Check working with legacy tables, since it might help you for future problems.

Sign up to request clarification or add additional context in comments.

1 Comment

@AkshayM fixed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.