-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create related model in hasMany relation inconsistency #1944
Comments
@b-admike , could you please take a look? Thanks. |
Thank you @israelglar for reporting this issue. It looks like a bug in our current implementation to me, possibly specific to MongoDB connector. We don't support embedded relations in LB4 yet. Even though the An attempt to create a IIRC, when we decorate a property with We can try to change I am not entirely sure about ramifications of this change. For example, I think the JSON/OpenAPI schema generated for So, I think this issue may be relatively easy to fix at the end:
@israelglar would you like to contribute this patch yourself? |
Hi Bajtos, thx |
To me, this looks like a single issue. I think I don't fully understand what you are trying to say?
The behavior I am expecting from the modified
|
@bajtos I have a use case where it does make sense to me to have the decorated property be part of the model. I'm actually POC-ing with LB4 and try to implement some kind of translatable property support where my models looks like: Base Model @model()
export class MyModel extends Entity {
@property({
type: 'number',
id: true,
})
id?: number;
@hasMany(() => MyModelTranslation)
translations: MyModelTranslation[];
constructor(data?: Partial<Widget>) {
super(data);
}
} Translation Model @model()
export class MyModelTranslation extends Entity {
@property({
type: 'number',
id: true,
})
id?: number;
@property({
type: 'number',
required: true,
})
myModelId: number;
@property({
type: 'string',
required: true,
})
localeCode: string;
@property({
type: 'string',
required: true,
})
someTranslatedProperty: string;
constructor(data?: Partial<MyModelTranslation>) {
super(data);
}
} In my case the url form |
Fix for issue #1944 - Create related model in hasMany relation inconsistency
I try to get data like below (Parent and child data). [{
"_id" : ObjectId("5bd83d9ae8abbe501d4b68d0"),
"name" : "testTeam",
"todos" : [
{
"id" : "should run function Profilerepository.create that creates a new ObjectId",
"username" : "test user",
"password" : "test pass",
"name" : "test name",
"email" : "testemail@email.com"
}
]
},
{
"_id" : ObjectId("5bd83d9ae8abbe501d4b68d0"),
"name" : "testTeam",
"todos" : [
{
"id" : "should run function Profilerepository.create that creates a new ObjectId",
"username" : "test user",
"password" : "test pass",
"name" : "test name",
"email" : "testemail@email.com"
}
]
}] But I Got the error "the relation is not defined for model loopback 4" when I use include filter option. Below code, I used in my application @get('/todo-lists', {
responses: {
'200': {
description: 'Array of TodoList model instances',
content: { 'application/json': { schema: { 'x-ts-type': TodoList } } },
},
},
})
async find(
): Promise<TodoList[]> {
return await this.todoListRepository.find({ include: [{ relation: 'todos' }] });
} |
@AnanthGopal Please look at #1352 for inclusion of related models. |
Description / Steps to reproduce / Feature proposal
I have 2 Models:
Profile
andTeam
. The relation isTeam hasMany Profile
.I have a controller for that relation TeamProfilesController
If I already have a team created and use POST /teams/{id}/members, a new profile is created 👍
Current Behavior
The problem is that on my team repository the POST /teams receives:
The repository accepts
members
as valid property and if I create a team with the propertymembers
set, what ends up in my Database is:No new Profile is created. What is created is a Team that has property members embedded in it. And the objects on that array don't qualify as Profile.
When I try to GET /teams/5bd83d9ae8abbe501d4b68d0/members is returns empty array.
Expected Behavior
When I create a team with the property
members
I Expected 1 of 2.1. One Team and one profile is created.
2. Like LB3 to not be possible to create a Team with the members property on the POST /teams. And if the property is sent, so get caught by the model as an invalid property because it is a "strict" model.
The text was updated successfully, but these errors were encountered: