When I make a request through Sequelize Model.findAll(), an event_id appears in the response that is not in the model, and also all Foreign keys come in both the snake and the camel case.
Sequelize call:
ChatMessage.findAll({ offset, limit, raw: true });
Result :
{
campaignId: 1
campaign_id: 1
createdAt: "2022-01-11T21:41:39.931Z"
event_id: null
id: 3
isCampaignMessage: true
media: "https://link.com"
message: "Text"
messageFrom: "+11111111111"
messageTo: "+11111111111"
sid: "..."
status: "..."
targetId: 1
target_id: 1
}
Query now:
SELECT "id",
"target_id" AS "targetId",
"message_from" AS "messageFrom",
"message_to" AS "messageTo",
"message",
"media",
"sid",
"status",
"campaign_id" AS "campaignId",
"is_campaign_message" AS "isCampaignMessage",
"created_at" AS "createdAt",
"campaign_id",
"event_id",
"target_id"
FROM "chat_message" AS "ChatMessage"
LIMIT 25 OFFSET 0;
But should be without the last 3 ids, cause they repeat.
Extra foreign fields can appear when you add not valid associate. The same was in my case, extra relation for event_id field.
Event.hasMany(models.Campaign, { foreignKey: 'event_id' });
Event.hasMany(models.ChatMessage, { foreignKey: 'event_id' }); // <--- Unnecessary association
Event.hasMany(models.Target, { foreignKey: 'event_i
As for the recurring cases, it was necessary to bring all associations foreign keys to the form of a camel case. Before that, they were in a snake case.
ChatMessage.belongsTo(models.Target, { foreignKey: '<target_id INTO targetId>' });