14

Edit: Some time after I wrote this Q&A, I've made improvements to the Sequelize documentation itself, regarding multiple topics, including this one. For those interested, the original Q&A is kept below, but I recommend just reading the new documentation instead:


Original Question

While going through the sequelize docs, more specifically the documentations about associations (edit: warning: this link points to an old version of the documentation), I see that the guide casually shows the reader methods such as setTasks(), addTask(), setProject(), that seem to be automatically created by sequelize for all model instances with respect to the created associations.

I couldn't find detailed information on what methods are available, and whether they are created with the singular version or plural version (since there is both setTasks() and setProject(), for example), and what exactly are the parameters they expect, and such. The docs apparently just casually mention them inside the examples...

So, what methods/mixins sequelize adds to the models when an association is made? And what are the parameters and return values, i.e. what's the documentation for those methods? Or, at least, where can I find them?

Pedro A
  • 3,989
  • 3
  • 32
  • 56

2 Answers2

35

Edit: Some time after I wrote this Q&A, I've made improvements to the Sequelize documentation itself, regarding multiple topics, including this one. For those interested, the original Q&A is kept below, but I recommend just reading the new documentation instead:


Original Answer

The documentation about associations you linked is a tutorial/guide. There is also the API Reference (edit: warning: this link points to an old version of the documentation), which is another type of documentation, more technical-oriented, which is helpful in this case. You can find it by clicking on the "Reference" link available in the side menu of the links you mentioned (and it took me quite a while to find that - it doesn't even look like a clickable thing IMO).

The parts you're interested here are these (edit: warning: these links points to an old version of the documentation):

  • Sequelize docs for BelongsTo type of associations: here
  • Sequelize docs for BelongsToMany type of associations: here
  • Sequelize docs for HasMany type of associations: here
  • Sequelize docs for HasOne type of associations: here

Understanding the API Reference

Since the docs linked above can be very confusing, here is an explanation to assist you to understand the docs.

Let's assume, for example, that we have a belongs to many association between Person and Hypothesis. Note that their plural forms, People and Hypotheses, are automatically inferred by Sequelize. This magic is done under the hood by the awesome library called inflection - see How do plurals work in Sequelize? for more details.

// Assuming that the models Person, Hypothesis and Person_Hypothesis are already defined
Person.belongsToMany(Hypothesis, { through: Person_Hypothesis });
Hypothesis.belongsToMany(Person, { through: Person_Hypothesis });

And we want to use the Sequelize docs for BelongsToMany type of associations to learn what methods were automatically added to instances of the Person and Hypothesis models. There, we can find the following table:

Table with the Method Summary of the Public Methods from the sequelize docs

To understand what this table means, recall that in the beginning of that page of the docs it says that "In the API reference below, add the name of the association to the method". The most confusing part of this is that it's not so clear when you should add the singular version of the name and when you should add the plural version. But although the docs do not make this clear, I assure you that you can just use common sense to guess. And if you think both versions could make sense (for example, for add), be surprised that actually both versions are available. Therefore, from the table above, we can conclude:

  • Methods added to instances of Person models:

    • addHypothesis()
    • addHypotheses()
    • countHypotheses()
    • createHypothesis()
    • getHypotheses()
    • hasHypothesis()
    • hasHypotheses()
    • removeHypothesis()
    • removeHypotheses()
    • setHypotheses()
  • Methods added to instances of Hypothesis models:

    • addPerson()
    • addPeople()
    • countPeople()
    • createPerson()
    • getPeople()
    • hasPerson()
    • hasPeople()
    • removePerson()
    • removePeople()
    • setPeople()

Another way to figure this out without room for doubt is by checking the Sequelize source code itself, namely here, where we can find:

this.accessors = {
    get: 'get' + plural,
    set: 'set' + plural,
    addMultiple: 'add' + plural,
    add: 'add' + singular,
    create: 'create' + singular,
    remove: 'remove' + singular,
    removeMultiple: 'remove' + plural,
    hasSingle: 'has' + singular,
    hasAll: 'has' + plural,
    count: 'count' + plural
};

Note: although it might seem counter-intuitive, in fact both methods addPerson() and addPeople() mentioned above work with the same parameters, which can be either a single value or an array. In other words, the methods add and addMultiple from the source code are actually the same, in the end. The same applies to remove() and removeMultiple(), and hasSingle() and hasAll().

Hopefully with this you can now understand what the Sequelize docs really mean with those tables.

If you prefer to check the source code directly, analogously to what I showed above, these are the relevant lines for the other kinds of associations:

  • BelongsTo: here

      this.accessors = {
          get: 'get' + singular,
          set: 'set' + singular,
          create: 'create' + singular
      };
    
  • HasOne: here

      this.accessors = {
          get: 'get' + singular,
          set: 'set' + singular,
          create: 'create' + singular
      };
    
  • HasMany: here

      this.accessors = {
          get: 'get' + plural,
          set: 'set' + plural,
          addMultiple: 'add' + plural,
          add: 'add' + singular,
          create: 'create' + singular,
          remove: 'remove' + singular,
          removeMultiple: 'remove' + plural,
          hasSingle: 'has' + singular,
          hasAll: 'has' + plural,
          count: 'count' + plural
      };
    
Pedro A
  • 3,989
  • 3
  • 32
  • 56
  • Do you know if these methods use any kind of performance optimizations? Like would `personInstance.getHypotheses()` be faster than defining an instance method on the person model that does a `findAll` to get their hypotheses? – Dan Mandel Mar 26 '18 at 16:34
  • 1
    @DanMandel - sorry to take so long. I am not 100% sure, but looks like there isn't really any performance optimizations when compared to a `findAll`. I checked the source code, and the `get` method actually calls `findAll` internally, after preparing the options / where clauses: [here](https://github.com/sequelize/sequelize/blob/f5fcf1644669bcd18bbc206d70279b8eb2307640/lib/associations/has-many.js#L222) and [here](https://github.com/sequelize/sequelize/blob/703d4feb16849fe13500296f31411f216428d478/lib/associations/belongs-to-many.js#L430) – Pedro A Apr 01 '18 at 22:19
  • 1
    You are my hero. Working with sqlz association mixins is easily the most aggravating thing about sqlz. – Tom Apr 17 '18 at 22:43
  • Very explanatory asnwer. Thank you. Do you know if there's any way to check what the exact plural version of my model will be? Sometimes it can get complicated and the set methods simply do not work. I had to rename my model. It would be nice if I could name my model whatever I wanted, and if I could check how inflection named my add, set methods etc. – Ester Dec 05 '18 at 16:22
  • 3
    @EsterVojkollari Thank you very much! I did this Q&A exactly because it took me a while to understand this part of sequelize. For your question, I suggest using [npm's runkit](https://npm.runkit.com/inflection) and running `inflection.pluralize(yourString)`. – Pedro A Dec 05 '18 at 17:33
  • You answered your own question but used the second person: > The documentation about associations you linked, ... ;) – DeeZone Oct 12 '19 at 01:05
  • 1
    @DeeZone Tried to make it even more subtle ;) – Pedro A Oct 12 '19 at 01:07
  • Is there a way to know if any of the sequelize mixins/methods like get, set, add, remove have completed or failed. Also do they support transactions, so I can roll back a record creation that precedes any of these method's – Nathaniel Babalola Feb 08 '21 at 12:54
  • @NathanielBabalola if they fail they will throw an error. Yes they support transactions. In the API reference you should see the transaction option listed. – Pedro A Feb 08 '21 at 13:05
  • Ooh I checked and I didn't see it – Nathaniel Babalola Feb 08 '21 at 13:21
  • @Pedro A I have a situation where on a table I used a different field apart from ```id``` as PRIMARY KEY. So when I used ```set``` to create the relationship on the through table, so while using the node debugger in vs code, I saw that Sequelize was trying to create the association by linking it to ```id``` field, but this id field wasn't referenced from the through table so it isn't a FOREIGN KEY. Is there a way to tell Sequelize the correct field it should link to instead of automatically assuming it's the ```id``` field – Nathaniel Babalola Feb 16 '21 at 01:45
  • @PedroA i posted the full question here, i would be grateful if you can help me out, https://stackoverflow.com/questions/66221799/change-sequelize-default-foreign-key-with-through-tables – Nathaniel Babalola Feb 16 '21 at 09:33
  • For anyone looking for the updated link to Sequelize docs, the updated link is here: https://sequelize.org/api/v6/class/src/associations/belongs-to-many.js~belongstomany – plutownium Oct 27 '22 at 04:47
  • @plutownium Thank you for the heads up. I've updated the question and the answer. – Pedro A Oct 27 '22 at 16:08
12

To get a listing of the added methods try:

    const model = %yourSequelizeModel%
    for (let assoc of Object.keys(model.associations)) {
      for (let accessor of Object.keys(model.associations[assoc].accessors)) {
        console.log(model.name + '.' + model.associations[assoc].accessors[accessor]+'()');
      }
    }

Credit goes to https://gist.github.com/Ivan-Feofanov/eefe489a2131f3ec43cfa3c7feb36490

To adjust the association names use "as" option:

Model.hasOne(models.series_promotions, { as: 'seriesPromotions' });

which changed the association method name from:

series.getSeries_promotion()
series.setSeries_promotion()
series.createSeries_promotion()

to

series.getSeriesPromotions()
series.setSeriesPromotions()
series.createSeriesPromotions()

based on the snippet above.

DeeZone
  • 770
  • 7
  • 16