sequelize.js - Hvordan setter jeg tilleggsattributter i koblingstabellen i TypeScript sequelize Mange-til-mange forhold?
Jeg har et mange-til-mange-forhold mellom Properties og Currency-modellen, og koblingsmodellen property_currency som junction-tabell som følger. Migrasjonsfiler er skrevet tilsvarende og databastabellene er opprettet korrekt. Imidlertid får jeg feilen 'through' eksisterer ikke i typen '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;
Her er jeg som prøver å legge til rad i property_currency-ratetabellen, men
let property = await Properties.findByPk(property_id);
let currency = await Currency.findByPk(currency_id);
property.addCurrency(currency, {through: { rate: rate}});
Jeg får denne feilen «Object literal may only specify known properties, and 'through' does not exist in type 'HasManyAddAssociationMixinOptions'.»
Hvordan løser jeg dette?