Create a Sequelize object with many depths associations

Viewed 198

I'm trying to create objects in one call, with many depths associations, with a Many-to-Many relationship. I have a User, an Account, a link table UsersAccountsMap (for Many-to-Many relationship between Account & User, because a User can have multiple Accounts, and an Account can have multiple Users) and a Project models. I want to create in one call, a new User linked to a new Account, with a new Project.

Currently, with my query, only the User and the Account (and the link table UsersAccountsMap) are created, but the Project is not, and no error is shown. For your information, before the Many-to-Many relationship, it was a One-to-Many (with one Account for multiples Users), and I was able to create the Project. Since then, I have updated my models (see below), updated my Create query by changing Account with Accounts

The create query : (I tried with Projects and Project in the value, but doesn't work)

const myuser = await User.create(
  {
    Accounts: {
      UsersAccountsMap: {
        role: "OWNER",
      },
      Projects: {
        text: "my text",
        score: 0.5
      },
      Project: {
        text: "my text",
        score: 0.5
      },
    },
    email: "email",
    password: "password",
  },
  {
    include: [
      {
        model: Account,
        include: [Project],
      },
    ],
  }
);

Models:

class Account extends Model {
  static associate({
    Account,
    Project,
    User,
  }: {
    [key: string]: any;
  }) {
    Account.hasMany(Project, { foreignKey: "accountId" });
    Account.belongsToMany(User, { through: UsersAccountsMap })
  }

  static init2(sequelize: Sequelize) {
    this.init(
      {
        company: DataTypes.STRING,
      },
      {
        paranoid: true,
        sequelize,
      }
    );
  }

  public id!: number;
  public Users?: User[];
  public Projects?: Project[];
}
class Project extends Model {
  public static init2(sequelize: Sequelize): void {
    this.init(
      {
        accountId: DataTypes.INTEGER,
        text: DataTypes.TEXT({ length: "long" }),
        score: {
          type: DataTypes.FLOAT,
          defaultValue: 0,
        },
      },
      {
        charset: "utf8mb4",
        paranoid: true,
        sequelize,
      }
    );
  }

  public static associate({
    Project,
    Account,
  }: {
    [index: string]: any;
  }) {
    Project.belongsTo(Account, { foreignKey: "accountId", as: "Accounts" });
  }

  public accountId!: string;
  public Account!: Account;
  public text?: string;
  public score?: number;
  public id!: number;
}
class User extends Model {
  public static init2(sequelize: Sequelize) {
    this.init(
      {
        // accountId: DataTypes.INTEGER, // <-- before with One-to-Many relationship
        email: DataTypes.STRING,
        password: DataTypes.STRING,
      },
      {
        paranoid: true,
        sequelize,
      }
    );
  }

  public static associate({ Account, User }: { [index: string]: any }) {
    // User.belongsTo(Account, { foreignKey: "accountId" });
    User.belongsToMany(Account, { through: UsersAccountsMap })
  }

  public id!: number;
  public Accounts!: Account[]; // <-- change Account by Accounts
  public UsersAccountsMaps!: UsersAccountsMap[];
  public email?: string;
  public password?: string;
}
class UsersAccountsMap extends Model {
  public static init2(sequelize: Sequelize) {
    this.init(
      {
        role: DataTypes.STRING,
      },
      {
        paranoid: true,
        sequelize,
      }
    );
  }

  public AccountId!: number;
  public Account!: Account;
  public UserId!: number;
  public User!: User;
}
0 Answers
Related