Skip to content

Commit

Permalink
req11
Browse files Browse the repository at this point in the history
  • Loading branch information
dopimentel committed Feb 21, 2024
1 parent a8616ee commit c96b0a9
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions src/models/PostCategory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const { DataTypes } = require("sequelize");

module.exports = (sequelize, _DataTypes) => {

const PostCategory = sequelize.define(
'PostCategory',
{
postId: {
type: DataTypes.INTEGER,
field: 'post_id',
primaryKey: true,
},
categoryId: {
type: DataTypes.INTEGER,
field: 'category_id',
primaryKey: true,
},
},
{
timestamps: false,
underscored: true,
tableName: 'posts_categories',
},
);

PostCategory.associate = (models) => {
models.BlogPost.belongsToMany(models.Category, {
as: 'categories',
through: PostCategory,
foreignKey: 'post_id',
otherKey: 'category_id',
});

models.Category.belongsToMany(models.BlogPost, {
as: 'blogPosts',
through: PostCategory,
foreignKey: 'category_id',
otherKey: 'post_id',
});
};

return PostCategory;
};

0 comments on commit c96b0a9

Please sign in to comment.