I am trying to get record of related table name automation_tag but getting above error please provide solution to solve this error there is relation between three tables automation,automation_tag and automation_tag_relation here is the code I am trying to do
function for getting record:-
export const getAutomationById = async (req: Request, res: Response, next: NextFunction) => {
try {
const id = req.params.id;
const { branch_id, organization_id } = req.tokenData;
const automation = await getAutomationRepo({
where: { id, branch_id, organization_id },
include: [
{
model: AutomationSteps,
as: 'steps',
where: { branch_id, organization_id },
required: false,
},
{
model: AutomationTag,
as: 'automation_tag',
where: { branch_id, organization_id },
required: false,
through: { attributes: ['id'] },
},
],
});
return generalResponse(res, automation, '', 'success', false, 200);
} catch (error) {
return next(error);
}
};
automation model.ts file:-
@Table({
timestamps: true,
paranoid: true,
tableName: 'automation',
scopes: { defaultOrganization: (organization_id: number) => ({ where: { organization_id } }) },
})
export default class Automation extends Model<AutomationAttributes, RequiredAutomationAttributesType> {
@PrimaryKey
@AutoIncrement
@AllowNull(false)
@Column(DataType.BIGINT)
id: number;
@AllowNull(false)
@PrimaryKey
@Column(DataType.BIGINT)
@ForeignKey(() => Organization)
organization_id: number;
@AllowNull(false)
@PrimaryKey
@Column(DataType.BIGINT)
@ForeignKey(() => Branch)
branch_id: number;
@AllowNull(false)
@Column(DataType.STRING(100))
name: string;
@Column(DataType.STRING(1000))
description?: string | null;
@Default(false)
@Column(DataType.BOOLEAN)
status: boolean;
@AllowNull(false)
@Column(DataType.JSONB)
data: Record<string, any>;
@CreatedAt
created_at: Date;
@UpdatedAt
updated_at: Date;
@DeletedAt
deleted_at: Date;
@ForeignKey(() => User)
@AllowNull(false)
@Column(DataType.BIGINT)
created_by: number;
@ForeignKey(() => User)
@AllowNull(false)
@Column(DataType.BIGINT)
updated_by: number;
@HasMany(() => AutomationSteps, { foreignKey: 'automation_id' })
steps: AutomationSteps[];
@HasMany(() => AutomationHistory, { foreignKey: 'automation_id' })
history: AutomationHistory[];
@HasMany(() => AutomationRevision, { foreignKey: 'automation_id' })
timeline: AutomationRevision[];
@HasMany(() => AutomationSetting, { foreignKey: 'automation_id' })
automation_setting: AutomationSetting[];
@BelongsToMany(() => AutomationTag, {
through: { model: () => AutomationTagsRelation, unique: false },
foreignKey: 'automation_id',
constraints: false,
})
automation_tag: AutomationTag[];
@BelongsTo(() => Branch, { constraints: false, foreignKey: 'branch_id' })
branches: Branch;
@BelongsTo(() => Organization, { as: 'organization', constraints: false, foreignKey: 'organization_id' })
organization: Organization;
@BelongsTo(() => User, { as: 'creator', constraints: false, foreignKey: 'created_by' })
creator: User;
@BelongsTo(() => User, { as: 'modifier', constraints: false, foreignKey: 'updated_by' })
modifier: User;
}
automation_tag model.ts
@Table({
timestamps: true,
paranoid: true,
tableName: 'automation_tag',
scopes: { defaultOrganization: (organization_id: number) => ({ where: { organization_id } }) },
})
export default class AutomationTag extends Model<AutomationTagAttributes, RequiredAutomationTagAttributesType> {
@PrimaryKey
@AutoIncrement
@AllowNull(false)
@Column(DataType.BIGINT)
id: number;
@AllowNull(false)
@PrimaryKey
@Column(DataType.BIGINT)
@ForeignKey(() => Organization)
organization_id: number;
@AllowNull(false)
@PrimaryKey
@Column(DataType.BIGINT)
@ForeignKey(() => Branch)
branch_id: number;
@Column({
type: DataType.STRING(100),
allowNull: false,
})
name: string;
@Column({
type: DataType.STRING(100),
allowNull: false,
})
color: string;
@ForeignKey(() => User)
@AllowNull(false)
@Column(DataType.BIGINT)
created_by: number;
@ForeignKey(() => User)
@AllowNull(false)
@Column(DataType.BIGINT)
updated_by: number;
@CreatedAt
created_at: Date;
@UpdatedAt
updated_at: Date;
@DeletedAt
deleted_at: Date;
@BelongsTo(() => Branch, { constraints: false, foreignKey: 'branch_id' })
branches: Branch;
@BelongsTo(() => Organization, { as: 'organization', constraints: false, foreignKey: 'organization_id' })
organization: Organization;
@BelongsTo(() => User, { as: 'creator', constraints: false, foreignKey: 'created_by' })
creator: User;
@BelongsTo(() => User, { as: 'modifier', constraints: false, foreignKey: 'updated_by' })
modifier: User;
@BelongsToMany(() => Automation, {
through: { model: () => AutomationTagsRelation, unique: false },
foreignKey: 'tag_id',
constraints: false,
})
automation: Automation[];
}
automation_tag_relation model.ts
@Table({
timestamps: true,
paranoid: true,
tableName: 'automation_tags_relation',
scopes: { defaultOrganization: (organization_id: number) => ({ where: { organization_id } }) },
})
export default class AutomationTagsRelation extends Model<
AutomationTagsRelationAttributes,
RequiredAutomationTagsRelationAttributesType
> {
@PrimaryKey
@AutoIncrement
@AllowNull(false)
@Column(DataType.BIGINT)
id: number;
@ForeignKey(() => Automation)
@AllowNull(false)
@Column(DataType.BIGINT)
automation_id: number;
@ForeignKey(() => AutomationTag)
@AllowNull(false)
@Column(DataType.BIGINT)
tag_id: number;
@ForeignKey(() => Organization)
@AllowNull(false)
@PrimaryKey
@Column(DataType.BIGINT)
organization_id: number;
@ForeignKey(() => Branch)
@AllowNull(false)
@PrimaryKey
@Column(DataType.BIGINT)
branch_id: number;
@ForeignKey(() => User)
@AllowNull(false)
@Column(DataType.BIGINT)
created_by: number;
@ForeignKey(() => User)
@AllowNull(true)
@Column(DataType.BIGINT)
updated_by: number;
@CreatedAt
created_at: Date;
@UpdatedAt
updated_at: Date;
@DeletedAt
deleted_at: Date;
@BelongsTo(() => Branch, { constraints: false, foreignKey: 'branch_id' })
branches: Branch;
@BelongsTo(() => Organization, { as: 'organization', constraints: false, foreignKey: 'organization_id' })
organization: Organization;
@BelongsTo(() => User, { as: 'creator', constraints: false, foreignKey: 'created_by' })
creator: User;
@BelongsTo(() => User, { as: 'modifier', constraints: false, foreignKey: 'updated_by' })
modifier: User;
}