I'm using PostgreSQL with Sequelize in Nodejs.
There are 3 tables: tc_user_intervention, tc_interventions and tc_users.
tc_user_intervention has 2 fields: userid (from tc_users) and interventionid (from tc_interventions).
Here's the Sequelize model (auto-generated from the existing database):
const UserIntervention = sequelize.define('tc_user_intervention', {
userid: {
type: DataTypes.INTEGER,
allowNull: false,
references: {
model: 'tc_users',
key: 'id'
}
},
interventionid: {
type: DataTypes.INTEGER,
allowNull: false,
references: {
model: 'tc_interventions',
key: 'id'
}
}
}, {
sequelize,
tableName: 'tc_user_intervention',
schema: 'public',
timestamps: false
});
If I try to do a raw query, everything works, I can insert a new raw into tc_user_interventions:
INSERT INTO `tc_user_intervention`
VALUES (value1, value2);
But if I try to do the same query with Sequelize, I receive the error:
error: column "id" of relation "tc_user_intervention" does not exist
The query simply is:
UserIntervention.create({ 'userid': user, 'interventionid': intervention});
But Sequelize translate this query into:
sql:
'INSERT INTO "public"."tc_user_intervention" ("id","userid","interventionid") VALUES (DEFAULT,$1,$2) RETURNING "id","userid","interventionid";',
and obviously this give me the error, because the id column does not exist neither in the model nor in the database. Why is this happening?