I am learning Nodejs with sequelize-typescript my task is to create crud system for one to many relationship. I search a lot but cant find that what is wrong in my code. I successfully get the record using postman but cant create record properly when I post below mention json only the parent record has been set.
{
"mainitemcode": "00013",
"mainitemcodeDetails": "00013",
"disabled": false,
"description": "any discription",
"finishGoodsRecipeList": [
{
"assembling_master_code": "00013",
"mainitemcode": "00013",
"subitemcode": "00015",
"description": "null",
"qty": 1.000000,
"wastage_qty": 0.000000
},
{
"assembling_master_code": "00013",
"mainitemcode": "00011",
"subitemcode": "00014",
"description": "null",
"qty": 1.000000,
"wastage_qty": 0.000000
}
]
}
finishGoodsRecipeListService:
findOne(code: string): Promise<FinishGoodsRecipeMaster> {
return this.FinishGoodsRecipeMasterModel.findOne({
include:[FinishGoodsRecipeList,Product],
where:{mainitemcode:code}
});
}
create(CreatefinishGoodsRecipeListDto: CreatefinishGoodsRecipeListDto): Promise<FinishGoodsRecipeMaster> {
let t:any= CreatefinishGoodsRecipeListDto
return this.FinishGoodsRecipeMasterModel.create(t)
}
FinishGoodsRecipeMaster:
@Table({
tableName: 'assembling_master',
timestamps: true})
export class FinishGoodsRecipeMaster extends Model {
// @PrimaryKey
@Column({autoIncrement: true})
idno: number;
@PrimaryKey
@Column
mainitemcode: string;
@Column
disabled: number;
@Column
description: string;
@CreatedAt
@Column({ field: 'created_at' })
createdAt: Date;
@UpdatedAt
@Column({ field: 'updated_at' })
updatedAt: Date;
@DeletedAt
@Column({ field: 'deleted_at' })
deletedAt: Date;
@HasOne(() => Product, 'code')
mainitemcodeDetails:Product;
@HasMany(() => FinishGoodsRecipeList)
finishGoodsRecipeList: FinishGoodsRecipeList[];
}
FinishGoodsRecipeList:
@Table({
tableName: 'assembling_list',
timestamps: true})
export class FinishGoodsRecipeList extends Model {
@PrimaryKey
@Column
idno: number;
@ForeignKey(() => FinishGoodsRecipeMaster)
@Column
mainitemcode: string;
@Column
subitemcode: string;
@Column
description: string;
@Column
qty: number;
@CreatedAt
@Column({ field: 'created_at' })
createdAt: Date;
@UpdatedAt
@Column({ field: 'updated_at' })
updatedAt: Date;
@DeletedAt
@Column({ field: 'deleted_at' })
deletedAt: Date;
@BelongsTo(() => FinishGoodsRecipeMaster)
FinishGoodsRecipeMaster: FinishGoodsRecipeMaster;
}
Your .create()
call also needs an include
clause.
return this.FinishGoodsRecipeMasterModel.create(t, {
include: ...
})
Here is the relevant documentation: Creating With Associations