No special messages at the moment. Just have a good day and stay hydrated!
Close

Junction Model Pattern: Many-to-Many - Sequelize

Apr 30th, 2019 / 4 min read / Share / Edit on GitHub
Many-to-many is a common modeling relationship between two entities. Here's one way to handle it with the Sequelize ORM.

Let's assume you're building a blog with Sequelize.

On your blog, you can create a bunch of Posts. As a way to describe your post, it can belong to many different Genres.

This blog post for example, might have the Sequelize, Orm, and Web Development tag.

So far, we have two models: Post and Genre.

// Post.js
module.exports = function(sequelize, DataTypes) {
  const Post =  sequelize.define('post', {
    post_id: {
      type: DataTypes.INTEGER(11),
      allowNull: false,
      autoIncrement: true,
      primaryKey: true
    },
    text: {
      type: DataTypes.TEXT,
      allowNull: false
    }
  }, {
    timestamps: true,
    underscored: true,
    tableName: 'post',
  });

  return Post;
};
// Genre.js
module.exports = function(sequelize, DataTypes) {
  const Genre =  sequelize.define('genre', {
    genre_id: {
      type: DataTypes.INTEGER(11),
      allowNull: false,
      autoIncrement: true,
      primaryKey: true
    },
    name: {
      type: DataTypes.STRING(60),
      allowNull: false,
      unique: true
    }
  }, {
    timestamps: true,
    underscored: true,
    tableName: 'genre',
  });

  return Genre;
};

Great. Now how do we model the many-to-many relationship between them?

Modeling the junction table

We need to create a juction table to store the relationship between the two.

In order to model it, we'll end up with three tables instead of just two.

I know that there's a way to get Sequelize to create this for you simply by using the correct association methods, but I like to be explicit about it.

Let's go ahead and create the additional model now.

// tagPostGenre.js
module.exports = function(sequelize, DataTypes) {
  const TagPostGenre = sequelize.define('tag_post_genre', {
    tag_post_genre_id: {
      type: DataTypes.UUID,
      defaultValue: DataTypes.INTEGER(11),
      primaryKey: true
    },
    post_id: {
      type: DataTypes.INTEGER(11),
      primaryKey: false,
      references: {
        model: 'post',
        key: 'post_id'
      },
      onDelete: 'cascade',
      onUpdate: 'cascade',
      unique: 'unique-genre-per-post'
    },
    genre_id: {
      type: DataTypes.INTEGER(11),
      primaryKey: false,
      references: {
        model: 'genre',
        key: 'genre_id'
      },
      onDelete: 'cascade',
      onUpdate: 'cascade',
      unique: 'unique-genre-per-post'
    },
  }, {
    timestamps: true,
    underscored: true,
    tableName: 'tag_post_genre'
  });

  return TagPostGenre;
};

Also, it's not necessary to prepend the model name with "Tag-", I just like to do that because it allows me to understand at a glance that that table is a Junction table between the other two tables mentioned in the name.

Here's what's important about this so far:

  • We've created foreign key relationships to the Post and Genre model.
  • We added a composite unique constraint in order to prevent a post from adding the same genre twice.

Now that we have the model created, we have to associate them all together with Sequelize so that we can get the benefits of using the convenience methods they expose.

Update the Associations

Add the associations on TagPostGenre junction table/model.

TagPostGenre.associate = (models) => {
  TagPostGenre.belongsTo(models.Post, { foreignKey: 'post_id', targetKey: 'post_id', as: 'Post' });
  TagPostGenre.belongsTo(models.Genre, { foreignKey: 'genre_id', targetKey: 'genre_id', as: 'Genre' });
}

Add the association on Genre.

Genre.associate = (models) => {
  Genre.belongsToMany(models.Post, { as: 'PostsInGenre', through: models.TagPostGenre, foreignKey: 'genre_id'});
}

Finally, add the association on Post.

Post.associate = (models) => {
  Post.belongsToMany(models.Genre, { as: 'GenresForPost', through: models.TagPostGenre, foreignKey: 'genre_id'});
}

The as key in the second argument's config object using belongsToMany is a way to specify the alias when we're doing Eager Loading / Include Queries.

Usage

If you want to set the genres for a Post instance...

const post = await Post.findOne({ where: { post_id: 1 }});

Since we've added the associations, you should have access to the convenience methods that Sequelize adds.

In this particular case, expect to see something like setGenres() on the post instance.

const genreIds = [1,2,3];
await post.setGenres(genreIds);

const genres = await post.getGenres();
console.log(genres) // genre instances! [{}, {}, {}]

We can use the ids of the genres to set the genres for this post.

You can also retrieve all of the genres using get-"plural junction association name"(). In this case, getGenres().

You can always double check that the methods have been added by using the vscode debugger.

Discussion

Thoughts? Share the article if you think it'll be useful to someone + join the discussion about this post on Twitter!


Stay in touch!



About the author

Khalil Stemmler

Khalil Stemmler is a Software developer / designer and musician based out of southern Ontario cities. He co-founded Univjobs, currently works as a consultant and writes about JavaScript, TypeScript, software development principles and best practices.


View more in Web Development

Fixing Sequelize Models with Migrations
Jul 17th, 2018 / 8 min read
Sequelize is a great ORM for NodeJS applications that are built on relational backends. Inevitably, you'll need to update your mod...
TypeScript vs. JavaScript [Pros / cons]
May 11th, 2019 / 4 min read
TypeScript is arguably one of the best things to come to the JavaScript world helping developers comfortably create complex enterp...
How to Build a Related Posts Component with Gatsby.js
Apr 16th, 2019 / 7 min read
Here's how to build a "Related Posts" component for your Gatsby.js site.
When To Use TypeScript - A Detailed Guide Through Common Scenarios
Apr 6th, 2019 / 15 min read
Strap yourself in. In this guide, we compare when it's absolutely vital to be using TypeScript, the strictly-typed programming lan...