Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sequelize associations - special methods/mixins are not created?

As per documentation:https://sequelize.org/docs/v6/core-concepts/assocs/#special-methodsmixins-added-to-instances

When an association is defined between two models, the instances of those models gain special methods to interact with their associated counterparts.

Special methods/mixins should be available to user instance, such like user.addFriend(), user.removeFriend(), when there is an applied belongsToMany,hasMany association, but there is a problem.



import { Model, DataTypes, Optional } from "sequelize";
import sequelize from "../../sequelize";
import { UserAttributes } from "./user.def";

interface UserCreationAttributes extends Optional<UserAttributes, "userId"> { }

export class User
  extends Model<UserAttributes, UserCreationAttributes>
  implements UserAttributes {
  public userId!: number;
  public active!: boolean;
  public firstName!: string;
  public lastName!: string;
  public username!: string;
}

const instance = User.init(
  {
    userId: {
      field: "user_id",
      type: DataTypes.BIGINT,
      primaryKey: true,
      autoIncrement: true,
    },
    active: {
      field: "active",
      type: DataTypes.BOOLEAN,
      defaultValue: true,
      allowNull: false,
    },
    firstName: {
      field: "first_name",
      type: DataTypes.STRING(100),
      allowNull: false,
    },
    lastName: {
      field: "last_name",
      type: DataTypes.STRING(100),
      allowNull: false,
    },
    username: {
      field: "username",
      type: DataTypes.STRING(),
      allowNull: false,
      unique: true,
    },
  },
  {
    sequelize,
    tableName: "user",
    freezeTableName: true,

  }
);
User.belongsToMany(User, { foreignKey: 'friend_id', as: 'Friend', through: 'UserFriend' })
export { instance };

These methods are not created. By the way, the 'through' table UserFriend is created in db.

I don't know if this problem has something with typescript, or with the way of creating this self-referential association. The first thing I suspect is the problem, is the way of creating these associations, but I tried all possibilities for connecting models, and special methods were never created.

Is there any way for me to check if these methods are created, because my IDE shows me this message - Property 'addFriend' does not exist on type 'User' (clearly not created). If anyone recognizes these types of problem, or where I can learn more about this topic and these types of problems, I'd be very grateful. Any advice will be helpful.

like image 350
Nikola Bjekovic Avatar asked Oct 26 '25 17:10

Nikola Bjekovic


1 Answers

The problem is that these functions are defined in runtime while typescript is going to fail if they are not declared in compile time, this is why you need to declare their signatures in the model yourself, only the signature, the implementation is taken care of by sequelize itself. since you have a belongsToMany relation you can add the following signatures as follows in the model class

export class User
  extends Model<UserAttributes, UserCreationAttributes>
  implements UserAttributes {
  public userId!: number;
  public active!: boolean;
  public firstName!: string;
  public lastName!: string;
  public username!: string;

  declare getFriends: BelongsToManyGetAssociationsMixin<Friend>;
  declare addFriend: BelongsToManyAddAssociationsMixin<Friend, number>;
  // etc...
}

see https://sequelize.org/docs/v6/other-topics/typescript/#usage

like image 65
Mahmoud Youssef Avatar answered Oct 28 '25 07:10

Mahmoud Youssef