Giving the following code:
- Welfare model:
// welfare.ts import { Model, DataType, Table, Column, ForeignKey, BelongsTo } from "sequelize-typescript"; import { Recruit } from "./recruit"; import { sequelize } from "../loaders/database"; @Table export class Welfare extends Model { @Column({ type: DataType.STRING(30), primaryKey: true, field: 'recruit_id' }) @ForeignKey(() => Recruit) recruitId!: string; @Column({ type: DataType.TINYINT, allowNull: false, defaultValue: false, field: 'four_major' }) fourMajor!: boolean; @Column({ type: DataType.TINYINT, allowNull: false, defaultValue: false, field: 'self_develop' }) selfDevelop!: boolean; @Column({ type: DataType.TINYINT, allowNull: false, defaultValue: false }) laptop!: boolean; @Column(DataType.STRING) etc!: string; @BelongsTo(() => Recruit, "recruit_id") recruit!: Recruit } Welfare.init({}, { sequelize, tableName: 'welfare', modelName: 'welfare' });
- Recruit model:
// recruit.ts import { Model, DataType, Table, Column, ForeignKey, BelongsTo, HasOne } from "sequelize-typescript"; import { sequelize } from "../loaders/database"; import { Welfare } from "./welfare"; @Table export class Recruit extends Model { @Column({ type: DataType.STRING(30), primaryKey: true, field: 'recruit_id' }) recruitId!: string; @Column(DataType.CHAR(10)) reception!: string; @Column(DataType.CHAR(10)) deadline!: string; @Column({ type: DataType.TINYINT, field: 'recruit_plan' }) recruitPlan!: boolean; @Column({ type: DataType.CHAR(5), field: 'start_time' }) startTime!: string; @Column({ type: DataType.CHAR(5), field: 'end_time' }) endTime!: string; @Column(DataType.INTEGER) salary!: number; @Column(DataType.INTEGER) period!: number; @Column({ type: DataType.TINYINT, defaultValue: false }) expired!: boolean; @Column(DataType.INTEGER) personnel!: number; @Column(DataType.STRING) detail!: string; @Column({ type: DataType.TINYINT, defaultValue: false }) writing!: boolean; @Column({ type: DataType.INTEGER, allowNull: false, defaultValue: 1 }) page!: number; @ForeignKey(() => Enterprise) @Column({ type: DataType.CHAR(12), allowNull: false, field: 'ent_no' }) entNo!: string; @HasOne(() => Welfare, "recruit_id") welfare!: Welfare; } Recruit.init({}, { sequelize, tableName: 'recruit', modelName: 'recruit' });
Then, inner join two models.
import { Recruit, Welfare } from "../entities"; export class RecruitRepository { public async getRecruitInfoById(recruitId: string) { return await Recruit.findOne({ attributes: [ 'ent_no', 'deadline', ['detail', 'workContent'], 'salary', 'period', 'start_time', 'end_time', 'personnel', 'reception' ], include: [ { model: Welfare, required: true, attributes: ['four_major', 'self_develop', 'labtop', 'etc'] } ], where: { recruit_id: recruitId }, }); } }
I tried to set model associations between Welfare and Recruit model with sequelize-typescript
. But, It didn't work and occur an exception.
SequelizeEagerLoadingError: welfare is not associated to recruit!
I find this exception, but I can't find any solution can apply my project.
What's the problem?
没有评论:
发表评论