Как установить дополнительные атрибуты в таблице соединений для связи TypeScript «многие-ко-многим»? - PullRequest
1 голос
/ 28 мая 2020

У меня есть отношения "многие ко многим" между моделью Properties и Currency и объединенной моделью property_currency для таблицы соединений следующим образом. Файлы миграции записываются соответствующим образом, и таблицы базы данных создаются правильно. Однако я получаю сообщение об ошибке 'through' does not exist in type 'HasManyAddAssociationMixinOptions'.

    import {
      DataTypes,
      Model,
      HasManyGetAssociationsMixin,
      HasManyAddAssociationMixin,
      HasManyHasAssociationMixin,
      Association,
      HasManyCountAssociationsMixin,
      HasManyCreateAssociationMixin,
    } from "sequelize";
    import { sequelize } from "../database";
    import CURRENCY from "./currency";

    export class PROPERTIES extends Model {
      public userid!: number;
      public id!: number;
      public property_name!: string;
      public addCurrency!: HasManyAddAssociationMixin<CURRENCY, number>;
      public getCurrencies!: HasManyGetAssociationsMixin<CURRENCY>;
      public hasCurrency!: HasManyHasAssociationMixin<CURRENCY, number>;
      public countCurrencies!: HasManyCountAssociationsMixin;
      public createCurrency!: HasManyCreateAssociationMixin<CURRENCY>;
      public static associations: {
        projects: Association<PROPERTIES, CURRENCY>;
      };
    }
    PROPERTIES.init(
      {
        id: {
          allowNull: false,
          autoIncrement: true,
          primaryKey: true,
          type: DataTypes.INTEGER,
        },
        property_name: {
          allowNull: true,
          type: DataTypes.STRING(50),
        }
      },
      {
        sequelize,
        tableName: "properties",
        timestamps: true,
      }
    );

    CURRENCY.belongsToMany(PROPERTIES, {
      as: "Properties",
      through: "property_currency",
      foreignKey: "currency_id",
      otherKey: "property_id",
    });

    PROPERTIES.belongsToMany(CURRENCY, {
      as: "Currency",
      through: "property_currency",
      foreignKey: "property_id",
      otherKey: "currency_id",
    });

    export default PROPERTIES;

import { DataTypes, Model } from "sequelize";
import { sequelize } from "../database";
import PROPERTIES from "./properties";

export class CURRENCY extends Model {
  public id!: number;
  public name: string;
  public short_name: string;
  public rate: number;
  public readonly createdAt!: Date;
  public readonly updatedAt!: Date;
}

CURRENCY.init(
  {
    id: {
      autoIncrement: true,
      primaryKey: true,
      type: DataTypes.INTEGER,
    },
    name: {
      allowNull: true,
      type: DataTypes.STRING(50),
    },
    short_name: {
      allowNull: true,
      type: DataTypes.STRING(10),
    },
  },
  {
    sequelize,
    tableName: "currency",
    timestamps: true,
  }
);


export default CURRENCY;
import { sequelize } from "../database";

class PROPERTY_CURRENCY extends Model {
  public id!: number;
  public property_id: number;
  public currency_id: number;
  public rate: number;
  public readonly createdAt!: Date;
  public readonly updatedAt!: Date;
}

PROPERTY_CURRENCY.init(
  {
    id: {
      autoIncrement: true,
      primaryKey: true,
      type: DataTypes.INTEGER,
    },
    property_id: {
      allowNull: true,
      type: DataTypes.INTEGER,
    },
    currency_id: {
      allowNull: true,
      type: DataTypes.INTEGER,
    },
    rate: {
      allowNull: true,
      type: DataTypes.DOUBLE,
    }
  },
  {
    sequelize,
    tableName: "property_currency",
    timestamps: true,
  }
);

export default PROPERTY_CURRENCY;

Вот я пытаюсь добавить строку в таблицу ставок property_currency, но

let property = await Properties.findByPk(property_id);
let currency = await Currency.findByPk(currency_id);
property.addCurrency(currency, {through: { rate: rate}});

Я получаю это ошибка «Литерал объекта может указывать только известные свойства, а« сквозной »не существует в типе« HasManyAddAssociationMixinOptions ».»

Как решить эту проблему?

...