jar*_*rge 6 sequelize.js typescript sequelize-typescript
我在属性和货币模型以及连接表的连接 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;
Run Code Online (Sandbox Code Playgroud)
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;
Run Code Online (Sandbox Code Playgroud)
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;
Run Code Online (Sandbox Code Playgroud)
我正在尝试将行添加到 property_currency 汇率表中,但是
let property = await Properties.findByPk(property_id);
let currency = await Currency.findByPk(currency_id);
property.addCurrency(currency, {through: { rate: rate}});
Run Code Online (Sandbox Code Playgroud)
我收到此错误“对象文字只能指定已知属性,并且“HasManyAddAssociationMixinOptions”类型中不存在“through”。”
我该如何解决这个问题?
| 归档时间: |
|
| 查看次数: |
757 次 |
| 最近记录: |