Justin
Justin

Reputation: 972

Sequelize - how to include a custom column details

I have 2 tables: User and Follower

The basic schema looks like this:

User Table

var User = sequelize.define(
    "User",
    {
      id: {
        type: DataTypes.INTEGER,
        autoIncrement: true,
        primaryKey: true
      },
      username: DataTypes.STRING,
    },
    {
      timestamps: true,
      tableName: "users",
      underscored: true
  ); 

  //Associations;
  User.associate = function(models) {
      User.hasMany(models.Follower, {
        constraints: false,
        onDelete: "cascade",
        hooks: true
      })
  };

Follower Table

var Follower = sequelize.define(
    "Follower",
    {
      id: {
        type: DataTypes.INTEGER,
        autoIncrement: true,
        primaryKey: true
      },
      follower_id: {
        type: DataTypes.INTEGER,
        foreignKey: true
      },
      following_id: {
        type: DataTypes.INTEGER,
        foreignKey: true
      }
    },
    {
      timestamps: true,
      tableName: "followers",
      underscored: true
    }
  ); 

  //Associations;
  Follower.associate = function(models) {
    Follower.belongsTo(models.User, {
      foreignKey: "following_id",
      constraints: false
    }),
    Follower.belongsTo(models.User, {
      foreignKey: "follower_id",
      constraints: false
    });
  };

I want get a list of followers for a user but I want to show the user they are following and now their own data in.

I am currently running this procedure:

const following = await models.Follower.findAndCountAll({
    attributes: models.Follower.fields,
    where: { follower_id: user_id },
    include:[
      {
        model: models.User,
        attributes: models.User.fieldsPublic,
        required: yes
      }
    ]
})

The issue is that the include is returning the current user and not the user details of the person they are following.

Essentially the join seems to be between User.id = Follower.follower_id

How can I get it to include and join with User.id = Follower.following_id

Upvotes: 2

Views: 4939

Answers (1)

5ar
5ar

Reputation: 2210

You can add as both in your Model definition and include to be able to define which foreign key should be used (docs)

So do:

Follower.associate = function(models) {
    Follower.belongsTo(models.User, {
      foreignKey: "following_id",
      as: 'following',
      constraints: false
    }),
    Follower.belongsTo(models.User, {
      foreignKey: "follower_id",,
      as: 'follower',
      constraints: false
    });
  };

and

...
include:[
      {
        model: models.User,
        attributes: models.User.fieldsPublic,
        as: 'follower'
        required: yes
      }
    ]
...

Alternatively, you can remove required: yes and use the where attribute to define the join condition

Furthermore, include seems to have an on attribute that is said to be similar to where (according to the docs), but I was unable to find an example.

Upvotes: 1

Related Questions