[英]Can I create a unique constraint on Loopback4's hasManyThrough (M:M) table
[英]How can i do the join of two table in loopback4
我在代碼中創建了以下提到的控制器,模型和存儲庫。 請看看。
我已經開發了下面提到的代碼,但仍然無法執行聯接操作。
- Info table having one foreign key which is belong to person table.
- Person table: id, name, status
- Info table : id, person_id , name , status
我還為信息和人員創建了存儲庫,模型和控制器文件。
人員存儲庫(person.repository.ts)
) {
super(Person, dataSource);
this.infos = this._createHasOneRepositoryFactoryFor(
'info',
getInfoRepository,
);
}
人員模塊(person.module.ts)
@hasOne(() => Info)
infos?: Info;
constructor(data?: Partial<Person>) {
super(data);
}
信息模塊(info.module.ts)
@belongsTo(() => Person)
personId: number;
constructor(data?: Partial<Info>) {
super(data);
}
它向我顯示了這樣的錯誤GET / people / fetchfromtwotable?filter [offset] = 0&filter [limit] = 10&filter [skip] = 0:500 TypeError:無法讀取未定義的屬性“ target”
關於加入有什么想法嗎?
drp,感謝您分享模型。 我的帖子被刪除了,因為我剛剛起步,需要詢問更多信息,這似乎很奇怪。 無論如何,請嘗試更改此行:
this.infos = this._createHasOneRepositoryFactoryFor(
'info',
getInfoRepository
);
至
this.infos = this._createHasOneRepositoryFactoryFor(
'infos',
getInfoRepository,
);
框架無法在模型上找到“信息”關系,因為您將屬性稱為“信息”
這是當前適用於我的示例(運行最新的lb4和postgres):
User.model.ts
import { model, property, hasOne, Entity } from '@loopback/repository';
import { Address } from './address.model';
@model()
export class User extends Entity {
constructor(data?: Partial<User>) {
super(data);
}
@property({ id: true })
id: number;
@property()
email: string;
@property()
isMember: boolean;
@hasOne(() => Address, {})
address?: Address;
}
Address.model.ts:
import { model, property, belongsTo, Entity } from '@loopback/repository';
import { User } from '../models/user.model';
@model()
export class Address extends Entity {
constructor(data?: Partial<Address>) {
super(data);
}
@property({ id: true })
id: number;
@property()
street1: string;
@property()
street2: string;
@property()
city: string;
@property()
state: string;
@property()
zip: string;
@belongsTo(() => User)
userId: number;
}
User.repository.ts:
import { HasOneRepositoryFactory, DefaultCrudRepository, juggler, repository } from '@loopback/repository';
import { User, Address } from '../models';
import { PostgresDataSource } from '../datasources';
import { inject, Getter } from '@loopback/core';
import { AddressRepository } from '../repositories'
export class UserRepository extends DefaultCrudRepository<
User,
typeof User.prototype.id
> {
public readonly address: HasOneRepositoryFactory<Address, typeof User.prototype.id>;
constructor(
@inject('datasources.postgres')
dataSource: PostgresDataSource,
@repository.getter('AddressRepository')
protected getAccountRepository: Getter<AddressRepository>,
) {
super(User, dataSource);
this.address = this._createHasOneRepositoryFactoryFor('address', getAccountRepository);
} // end ctor
}
User.controller.ts(節略):
@get('/users/{id}/address')
async getAddress(
@param.path.number('id') userId: typeof User.prototype.id,
@param.query.object('filter', getFilterSchemaFor(Address)) filter?: Filter,
): Promise<Address> {
return await this.userRepository
.address(userId).get(filter);
}
希望這可以幫助。
祝好運!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.