From 45e3ccb983a22193aa6f71f6b662cc7e2b090a92 Mon Sep 17 00:00:00 2001 From: Lucas Vieira Date: Mon, 7 Mar 2022 22:06:48 -0300 Subject: [PATCH] chore: :wrench: implements associations --- src/database/migrations/20220308002240-feeding-history.js | 7 +++++++ src/model/FeedingHistory.js | 4 ++++ src/model/Pixel.js | 1 + 3 files changed, 12 insertions(+) diff --git a/src/database/migrations/20220308002240-feeding-history.js b/src/database/migrations/20220308002240-feeding-history.js index 9bb4b60..408ca8e 100644 --- a/src/database/migrations/20220308002240-feeding-history.js +++ b/src/database/migrations/20220308002240-feeding-history.js @@ -15,6 +15,13 @@ module.exports = { type: Sequelize.DATE, allowNull: false, }, + pixel_id: { + type: Sequelize.INTEGER, + allowNull: true, + references: { model: "pixels", key: "id" }, + onUpdate: "CASCADE", + onDelete: "CASCADE", + }, created_at: { type: Sequelize.DATE, allowNull: false, diff --git a/src/model/FeedingHistory.js b/src/model/FeedingHistory.js index c13e7f8..c45eda3 100644 --- a/src/model/FeedingHistory.js +++ b/src/model/FeedingHistory.js @@ -14,6 +14,10 @@ class FeedingHistory extends Model { } ); } + + static associate(models) { + this.belongsTo(models.Pixel, { foreignKey: "pixel_id" }); + } } module.exports = { FeedingHistory }; diff --git a/src/model/Pixel.js b/src/model/Pixel.js index 0824291..7297e99 100644 --- a/src/model/Pixel.js +++ b/src/model/Pixel.js @@ -18,6 +18,7 @@ class Pixel extends Model { static associate(models) { this.belongsTo(models.Team, { foreignKey: "team_id" }); this.hasMany(models.Feature, { foreignKey: "pixel_id" }); + this.hasMany(models.FeedingHistory, { foreignKey: "pixel_id" }); this.belongsTo(models.User, { foreignKey: "user_id" }); } }