0

I'm using sequelize 4.32 and I was trying to write a self-association in one of the tables, I'm not sure if there is something else that I need to do to solve this relation, my goal is to get all the records in my table and include all the records associated with each one this is the error that I'm getting in the console:

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

below you'll find my model:

module.exports = (sequelize, DataTypes) => {
  const stands = sequelize.define('stands', {
    id: {
      type: DataTypes.INTEGER,
      unique: true,
      allowNull: false,
      primaryKey: true,
      autoIncrement: true,
    },
    name: {
      type: DataTypes.STRING,
      field: 'name',
      unique: {
        args: true,
        msg: 'Name already exists ',
      },
    },
    location: {
      type: DataTypes.JSON,
      field: 'location',
    },
    remote: {
      type: DataTypes.BOOLEAN,
      field: 'remote',
      defaultValue: false,
    },
    created_at: {
      type: DataTypes.DATE(3),
      defaultValue: sequelize.literal('CURRENT_TIMESTAMP(3)'),
    },
    updated_at: {
      type: DataTypes.DATE(3),
      defaultValue: sequelize.literal('CURRENT_TIMESTAMP(3)'),
    },
  }, { freezeTableName: true, timestamps: true, underscored: true });

  stands.associate = (models) => {
    stands.hasMany(stands, { as: 'adjucent_stands' });
  };

  return stands;
};
  • 1
    How do you want the self association to work? It looks like you're trying to join two rows with the same PK... which isn't possible. – KenOn10 Jul 23 '19 at 16:30
  • @KenOn10 I want any stand to have an array of adjacent stands, how can I implement that to let sequelize create a join table with the stand id in one column and adjacent stand id in another column – Farah Amawi Jul 24 '19 at 06:48
  • I'm wondering why this answer didn't work with me https://stackoverflow.com/questions/18950886/sequelizejs-hasmany-to-hasmany-on-the-same-table-with-a-join-table – Farah Amawi Jul 24 '19 at 06:58

0 Answers0