Sequelize-typescript 'HasManyCreateAssociationMixin' 不是函数
Posted
技术标签:
【中文标题】Sequelize-typescript \'HasManyCreateAssociationMixin\' 不是函数【英文标题】:Sequelize-typescript 'HasManyCreateAssociationMixin' is not a functionSequelize-typescript 'HasManyCreateAssociationMixin' 不是函数 【发布时间】:2020-02-04 23:27:04 【问题描述】:我在 sequelize-typescript 中有一个模型 Door.ts:
import Table, Model, Column, AutoIncrement, PrimaryKey, ForeignKey, DataType, AllowNull, BelongsTo, HasMany from 'sequelize-typescript';
import Location from '@modules/location';
import AkilesServiceV1, AkilesServiceV0, IDoorService from '@services/DoorService';
import BelongsToGetAssociationMixin from 'sequelize/types';
import DoorLog from '@modules/door_log';
import HasManyCreateAssociationMixin from 'sequelize';
@Table( tableName: 'door' )
class Door extends Model<Door>
@PrimaryKey
@AutoIncrement
@Column
id!: number;
@AllowNull(false)
@Column
type!: string;
@Column
button_id!: string;
@Column
gadget_id!: string;
@Column
action_id!: string;
@AllowNull(false)
@Column(DataType.ENUM('vehicular','pedestrian'))
access_type!: 'vehicular' | 'pedestrian';
@AllowNull(false)
@Column
description_tag!: string;
@Column(DataType.VIRTUAL)
description!: string;
@ForeignKey(() => Location)
@AllowNull(false)
@Column
location_id!: number;
@BelongsTo(() => Location)
location!: Location;
@HasMany(() => DoorLog)
door_logs!: DoorLog[];
public getLocation!: BelongsToGetAssociationMixin<Location>;
public createDoorLog!: HasManyCreateAssociationMixin<DoorLog>;
public async open ()
let doorService: IDoorService;
switch(this.type)
case 'akiles-v0':
doorService = new AkilesServiceV0();
break;
case 'akiles-v1':
doorService = new AkilesServiceV1();
break;
default:
doorService = new AkilesServiceV1();
break;
//await doorService.open(this);
return await this.createDoorLog( door_id: this.id, timestamp: new Date() );
public async getParking()
const location: Location = await this.getLocation();
return await location.getParking();
export default Door
如你所见,它有这两个与 Mixins 相关的函数:
public getLocation!: BelongsToGetAssociationMixin<Location>;
public createDoorLog!: HasManyCreateAssociationMixin<DoorLog>;
第一个可以完美地使用它:await this.getLocation()
。但是,当我这样称呼它时,第二个:await this.createDoorlog (door_id: this.id, timestamp: new Date ())
返回以下错误:
TypeError: this.createDoorLog is not a function
我也尝试过不带参数调用函数,但得到了相同的结果。我不明白为什么这两个函数虽然创建的几乎相同,但行为却不同。我错过了HasManyCreateAssociationMixin
的东西吗?
谢谢。
【问题讨论】:
你找到解决方案了吗? 【参考方案1】:因为当我不可避免地再次遇到这个问题时,对同样的问题感到困惑。答案是将“as”添加到 @HasMany
mixin。 Sequelize 似乎有驼峰类的问题。
所以在这种情况下添加
@HasMany(() => DoorLog, options: as: "doorLog" )
door_logs!: DoorLog[];
或者类似的东西应该允许你使用这个 mixin
【讨论】:
以上是关于Sequelize-typescript 'HasManyCreateAssociationMixin' 不是函数的主要内容,如果未能解决你的问题,请参考以下文章
Sequelize beforeConnect 钩子与 sequelize-typescript 未运行
sequelize/sequelize-typescript - 带有 HasMany 的 findAll 返回一个对象而不是数组
如何使用 sequelize-typescript 从源实例创建关联实例
如何使用 sequelize-typescript 从单个字段中引用多个模型?
如何使用 Sequelize-Typescript 模型定义指定 NestJs 控制器的返回类型?
sequelize-typescript - 当使用带有 `tsc` 编译器(带有 NestJS)的 glob 配置时,无法解析模型模块