3

I'm trying to learn how to use Sequelize and I'm new to ORMs, and the concept of association is very confusing to me, it makes me contemplate going back to just SQL.

I have an Artists table and a Genres table. An artist can play in more than one genre, and genres can be played by more than one artist. It's a many to many relationship. In my SQL model, I have an assoc_artists_genres table, and if artist with id 2 plays in the genres 3 and 5, I insert the values (2,3) and (2,5) in the assoc table. Simple.

But with Sequelize, I'm puzzled. According to this answer, the way to do it is to implement belongsToMany() in both models.

  • How does that work exactly? I've added that in the .associate part of my model, but where do I go from here, how do I add the associations?

  • If I want to add that the artist 2 plays in the genres 3 and 5, do I have to update the artist object AND the genres objects? Isn't that very redundant?

  • What happens if I update the artist object with both genres but I don't update the genres objects with the artist?

Should I use an assoc table too (like it looks to be suggested in the docs) or just use no assoc table and put belongsToMany() in each model like suggested in the linked answer?


I tried adding an association model (Artists_Genres) but should I add the association in the individual model or in the assoc model? Is the assoc model empty then?


Code with my models:

models/artist.js

'use strict';
module.exports = (sequelize, DataTypes) => {
  var Artist = sequelize.define('Artist', {
    name: DataTypes.STRING,
    description: DataTypes.TEXT,
    picture: DataTypes.TEXT
  }, {});

  Artist.associate = function(models) {
    Artist.belongsToMany(models.Genre, {
      through: 'Artist_Genre',
      foreignKey: 'Artist_id',
    });
  };
  return Artist;
};

models/genres.js

'use strict';
module.exports = (sequelize, DataTypes) => {
  var Genre = sequelize.define('Genre', {
    name: DataTypes.STRING
  }, {});

  Genre.associate = function(models) {
    Genre.belongsToMany(models.Artist, {
      through: 'Artist_Genre',
      foreignKey: 'Genre_id',
    });
  };
  return Genre;
};

models/artist_genre.js

'use strict';
module.exports = (sequelize, DataTypes) => {
  var Artist_Genre = sequelize.define('Artist_Genre', {
    Artist_id: DataTypes.INTEGER,
    Genre_id: DataTypes.INTEGER
  }, {});

  Artist_Genre.associate = function(models) {
    // associations can be defined here
    //Should I add anything here?
  };
  return Artist_Genre;
};

I think the part I'm missing now is in the Artist controller. I want to assign a genre to an artist, using artist_id and genre_id. My controller looks like this:

const Artist = require('../models').Artist;
const Genre = require('../models').Genre; 

module.exports = {
  create(req, res) {
    return Artist
      .create({
        name: req.body.name,
        description: req.body.description,
        picture: req.body.picture,
      })
      .then(artist => res.status(201).send(artist))
      .catch(error => res.status(400).send(error));
  },

  list(req,res) {
    return Artist
      .all()
      .then(artists => res.status(200).send(artists))
      .catch(error => res.status(400).send(error));
  },

  addGenre(req,res) {
    // ?
  },
};
Teleporting Goat
  • 417
  • 1
  • 6
  • 20
  • Include your model code and how you're trying to associate them in the question rather than attempting to describe them. [This part of the documentation](http://docs.sequelizejs.com/manual/tutorial/associations.html#belongs-to-many-associations) may also help you. – Ankh May 22 '18 at 13:22
  • @Ankh I have a full project from [this tutorial](https://scotch.io/tutorials/getting-started-with-node-express-and-postgres-using-sequelize#generating-models) (with my tables instead of todos, it doesn't have a many-to-many example), so about 15 files with 20-40 lines each. What files are important? Should I include the code for models, controllers and routes? – Teleporting Goat May 22 '18 at 13:26
  • You shouldn't need to define anything against `Artist_Genre.associate` as it's just the join table. At a glance everything looks in order. Is it erroring when trying to use it? – Ankh May 22 '18 at 15:07
  • @Ankh I have no idea how to use it. I'm currently trying to use the `addGenre` function like in [these docs](http://docs.sequelizejs.com/manual/tutorial/associations.html#belongs-to-many-associations) but I'm struggling, I don't know where to start. – Teleporting Goat May 22 '18 at 15:17

0 Answers0