sigil
sigil

Reputation: 9546

When updating Sequelize model, fails and returns different table name than I provided?

I'm running PostgreSQL 11, and attempting to update one of its table entries using Sequelize.

My NodeJS code:

require('env2')('.env');
const Sequelize = require('sequelize');

const sequelize=new Sequelize(process.env.database,process.env.username,process.env.password,{
    dialect:'postgres'
});

const Agent=sequelize.define('agent');
updateValues={available:false};
Agent.update(updateValues,{where:{phonenumber:'+18005551212'}}).then(result=>{console.log(result)});

The table agent has the structure:

id primary key serial,
phonenumber varchar(100),
available boolean

When I run the NodeJS code, I get this error:

Executing (default): UPDATE "agents" SET "updatedAt"='2018-12-27 10:16:54.504 +0
0:00' WHERE "phonenumber" = '+18005551212'
Unhandled rejection SequelizeDatabaseError: relation "agents" does not exist

Why is this update failing? I don't understand why the error is talking about the relation "agents", when I provided the table name as "agent" in sequelize.define(agent).

The update is successful when I use raw SQL as follows:

sequelize.query("update agent set available=false where phonenumber='+18005551212'").then(result=>{
    console.log(result);
});

Upvotes: 1

Views: 819

Answers (1)

Harsh Vishwakarma
Harsh Vishwakarma

Reputation: 120

By default sequelize creates a table with a plural of its definition name, so when you do sequelize.define('agent') it actually creates a table with name agents . If you don't want to change your table name naturally you can use freezeTableName: true option in sequelize definition. More can be found in this answer. Refer Sequelize configuration doc .

Upvotes: 1

Related Questions