我正在使用回送4关系。我有三个模型product,order和orderProducts。关系如下
但是当我尝试在OrderProducts上发布请求时,出现此错误:
500错误:模型数据中不允许使用导航属性(模型“ OrderProducts”属性“ product_id”)
这是我的代码。预先谢谢你
order.model.ts
import { Entity, model, property, hasMany} from '@loopback/repository';
import {OrderProducts} from './order-products.model';
@model({ settings: { strict: false } })
export class Orders extends Entity {
@property({
type: 'number',
id: true,
generated: true,
})
order_id?: number;
@property({
type: 'number',
required: true,
})
total: number;
@property({
type: 'string',
})
get: string;
@hasMany(() => OrderProducts, {keyTo: 'order_id'})
orderProducts: OrderProducts[];
// Define well-known properties here
// Indexer property to allow additional data
// eslint-disable-next-line @typescript-eslint/no-explicit-any
[prop: string]: any;
constructor(data?: Partial<Orders>) {
super(data);
}
}
export interface OrdersRelations {
// describe navigational properties here
}
export type OrdersWithRelations = Orders & OrdersRelations;
product.model.ts
import { Entity, model, property, hasMany } from '@loopback/repository';
import { OrderProducts } from './order-products.model';
@model({ settings: { strict: false } })
export class Product extends Entity {
@property({
type: 'number',
id: true,
generated: true,
})
product_id?: number;
@property({
type: 'string',
required: true,
})
name: string;
@property({
type: 'string',
})
desc?: string;
@hasMany(() => OrderProducts, { keyTo: 'product_id' })
orderProducts: OrderProducts[];
// Define well-known properties here
// Indexer property to allow additional data
// eslint-disable-next-line @typescript-eslint/no-explicit-any
[prop: string]: any;
constructor(data?: Partial<Product>) {
super(data);
}
}
export interface ProductRelations {
// describe navigational properties here
}
export type ProductWithRelations = Product & ProductRelations;
order-product.model.ts
import {Entity, model, property, belongsTo} from '@loopback/repository';
import {Product} from './product.model';
@model({settings: {strict: false}})
export class OrderProducts extends Entity {
@property({
type: 'number',
id: true,
generated: true,
})
o_id?: number;
@property({
type: 'number',
required: true,
})
quantity: number;
@property({
type: 'number',
})
order_id?: number;
@belongsTo(() => Product)
product_id: number;
// Define well-known properties here
// Indexer property to allow additional data
// eslint-disable-next-line @typescript-eslint/no-explicit-any
[prop: string]: any;
constructor(data?: Partial<OrderProducts>) {
super(data);
}
}
export interface OrderProductsRelations {
// describe navigational properties here
}
export type OrderProductsWithRelations = OrderProducts & OrderProductsRelations;
我认为问题可能是由命名约定引起的。 LoopBack 4默认情况下使用camelCased变量名称。
对于任何关系,都有三个重要的属性:源键,外键和关系名称。
这里是相关文件的链接:Relation Metadata。对于hasMany和hasOne关系,源键默认为id属性,在您的情况下为Order.order_id
和Product.product_id
。并且目标键(外键)默认为'TargetModelName'
+ 'Id'
。就是orderProducts.orderProductsId
。并且默认的关系名称是您用@hasMany
装饰器装饰的名称,在您的示例中为orderProducts
。您有keyFrom
处理自定义名称,所以两个hasMany关系很好。
对于归属关系,默认源键是目标模型的id属性,在您的情况下为Product.product_id
。源密钥是由@belongsTo
装饰器修饰的一个,即OrderProducts.product_id
。默认关系名称是..这是棘手的部分,它是product_id
在您情况下。从概念上讲,源密钥应为OrderProducts.productId
,默认关系名称为product
。在您的情况下,该属性与您的关系名称具有相同的名称。这是由LB4生成关系名称的方式引起的。这就是为什么它抱怨导航属性。
要修复它,您需要更改两个文件,orderProducts.model.ts
:
... // other properties
@belongsTo(() => Product, {name: 'product'}) // use a different name from the property name
product_id: number; // let's use 'product' here
...
和OrderProductsRepository
:
export class OrderProductsRepository extends DefaultCrudRepository<
OrderProducts,
typeof OrderProducts.prototype.o_id,
OrderProductsRelations
> {
public readonly product: BelongsToAccessor< // use the relation name
Product,
typeof OrderProducts.prototype.o_id
>;
constructor(
@inject('datasources.db') dataSource: DbDataSource,
@repository.getter('ProductRepository')
protected productRepositoryGetter: Getter<ProductRepository>,
) {
super(OrderProducts, dataSource);
// make sure the name is correct
this.product = this.createBelongsToAccessorFor(
'product',
productRepositoryGetter,
);
// make sure the name is correct
this.registerInclusionResolver(
'product',
this.product.inclusionResolver,
);
}
}
参考:BelongsTo Relation Metadata。
如果对LB4有任何疑问,也可以在我们的GitHub community上发布一个问题。