'Sequelize model field that I did not add

I have a user, role and their relation model, when I want to insert into the relation model I get this error:

  error: column "userUserId" of relation "roles_users_relationships" does not exist.

Can you help with this error?

(sorry if I wrote something wrong, this is my first question on )

This is how my model looks Role model:

const Schema = (sequelize, DataTypes) => {
  const table = sequelize.define(
    "roles", {
      role_id: {
        type: DataTypes.UUID,
        defaultValue: sequelize.literal("uuid_generate_v4()"),
        primaryKey: true,
      },
      name: {
        type: DataTypes.STRING,
        allowNull: false,
      }
    }, {
      timestamps: false
    }
  );

  table.associate = function (models) {
    table.belongsToMany(models.users, {
      through: "roles_users_relationship",
      foreignKey: "role_id",
    });
  };
  return table;
};

Users model:

const Schema = (sequelize, DataTypes) => {
  const table = sequelize.define(
    "users", {
      user_id: {
        type: DataTypes.UUID,
        defaultValue: sequelize.literal("uuid_generate_v4()"),
        primaryKey: true,
      },
      name: {
        type: DataTypes.STRING,
        allowNull: true,
      }
    }, {
      timestamps: false
    }
  );

  table.associate = function (models) {
    table.belongsTo(models.roles, {
      through: "roles_users_relationship",
      foreignKey: "user_id",
    });
  };

  return table;
};

Roles Users relationship model:

const Schema = (sequelize, DataTypes) => {
  const table = sequelize.define(
    "roles_users_relationship", {
      user_id: {
        type: DataTypes.UUID,
        allowNull: false,
      },
      role_id: {
        type: DataTypes.UUID,
        allowNull: false,
      },
    }, {
      timestamps: false
    }
  );
  return table;
};


Solution 1:[1]

In your through table you should add options in related table field:

references: {
  model: User,
  key: 'user_id'
}

Otherwise sequelize will do it automatically, like adding foreign key column in this way tableNamePrimaryKeyColumn in your case its 'userUserId'

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Eziz Hudayberdiyev