As we've already seen, you can alias models in associations using as
. In single associations (has one and belongs to), the alias should be singular, while for many associations (has many) it should be plural. Sequelize then uses the library to convert the alias to its singular form. However, this might not always work for irregular or non-english words. In this case, you can provide both the plural and the singular form of the alias:
This will add the functions add/set/get Tasks
to user instances.
Without , this adds subscriptionId
as expected. However, if you were to say Invoice.belongsTo(Subscription, { as: 'TheSubscription' })
, you will have both subscriptionId
and theSubscriptionId
, because sequelize is not smart enough to figure that the calls are two sides of the same relation. 'foreignKey' fixes this problem;