【发布时间】:2019-03-15 02:24:37
【问题描述】:
我正在尝试使用 Mongo 数据库建立 hasMany 关系。 我按照指南在环回 4 文档 (https://loopback.io/doc/en/lb4/HasMany-relation.html) 中创建了一个 hasMany 关系,并尝试设置不同的属性,但外键 custId 保存为字符串而不是 ObjectID。
我还从其他主题中找到了一些其他属性或选项,但人们使用的是 Loopback 3,它似乎不适用于 Loopback 4。
我错过了什么还是有什么解决方法?
这是我的模型:
@model()
export class Order extends Entity {
@property({
type: 'string',
id: true,
generated: true,
})
id: string;
@property({
type: 'array',
itemType: 'string',
required: true,
})
product: string[];
@property({
type: 'number',
required: true,
})
price: number;
@property({
type: 'string',
id: true,
generated: true,
})
custId: string;
constructor(data?: Partial<Order>) {
super(data);
}
}
@model()
export class Customer extends Entity {
@property({
type: 'string',
id: true,
generated: true,
})
id: string;
@property({
type: 'string',
required: true,
})
name: string;
@property({
type: 'string',
})
adress?: string;
@hasMany(() => Order, {keyTo: 'custId'})
orders?: Order[];
constructor(data?: Partial<Customer>) {
super(data);
}
}
【问题讨论】:
-
我不知道保存为字符串的外键是否有任何相关性,但您可以在 Loopback 的 GitHub 存储库中找到一些关于关系的问题:github.com/strongloop/loopback-next/labels/Relations 很抱歉这个链接但是我不知道其中哪一个与您的问题最直接相关。即使您将外键保存为 ObjectId,我认为这种关系也行不通……
标签: mongodb loopbackjs loopback objectid