Rohitus
Rohitus

Reputation: 159

Aliased associations must have unique aliases

In accordance with the documentation on creating my associations in pairs and using multiple associations on the same model, I get the error of

"You have used the alias author in two separate associations. Aliased associations must have unique aliases."

My author associations are;

User.hasMany(CourseRequest, {
  as: 'author',
  foreignKey: {
    name: 'authorId',
    type: Sequelize.UUID,
    allowNull: false,
    validate: {
      isUUID: {
        args: 4,
        msg: `author must be a valid ID`,
      },
      notNull: {
        msg: `author is required`,
      },
    },
  },
});

CourseRequest.belongsTo(User, {
  as: 'author',
  foreignKey: {
    name: 'authorId',
    type: Sequelize.UUID,
    allowNull: false,
    validate: {
      isUUID: {
        args: 4,
        msg: `author must be a valid ID`,
      },
      notNull: {
        msg: `author is required`,
      },
    },
  },
});

How can I resolve this?

Upvotes: 1

Views: 3100

Answers (1)

Rohitus
Rohitus

Reputation: 159

I managed to solve the problem. I was initially calling a function where the model is being used as

require('foo').bar()

instead of

const {bar} = require('foo'); 
bar();

Upvotes: 0

Related Questions