我有两个模型Company
和Reviews
。每个Company
将有多个Reviews
。
这是Company
模型
@Table()
export class Company extends Model<Company> {
@Column({ type: DataType.INTEGER.UNSIGNED, primaryKey: true, autoIncrement: true })
COMPANY_ID: number;
@Column({ type: DataType.TEXT })
Name: string;
@HasMany(() => Review)
reviews: Review[];
}
Reviews
模型如下
@Table()
export class Review extends Model<Review> {
@Column({ type: DataType.INTEGER.UNSIGNED, primaryKey: true, autoIncrement: true })
REVIEW_ID: number;
@ForeignKey(() => Company)
@Column
Company_ID: number;
@Column({ type: DataType.TEXT })
Review: string;
@Column({ type: DataType.INT })
rating: string;
@Column({ type: DataType.TEXT })
UserName: string;
@BelongsTo(() => Company)
Company: Company;
}
我正在尝试获取一家公司的所有评论列表。我到目前为止写的查询是
const pagedData = await Review.findAndCountAll({
attributes: [
['REVIEW_ID', 'id'],
['Review', 'feedback']
],
include: [{
model: Company,
attributes: ['Name']
}],
limit,
offset
});
我得到的回复是
{
"total": 2,
"reviews": [
{
"id": 917004723,
"feedback": "feedback 1",
"company": {
"Name": "www.acceleronpharma.com"
}
},
{
"id": 917004734,
"feedback": "feedback 2",
"company": {
"Name": "http://www.accenture.com"
}
}
]
}
问题:
Name
中的Company
,我希望它是companyName
或其他名称。reviews
数组中的对象拉出,还可以将其作为review
对象本身中的属性拉出。 {
"id": 917004723,
"feedback": "feedback 1",
"companyName": "www.acceleronpharma.com"
}
PS:请忽略大小写,我只是添加了一个我要解决的问题的示例。