Skip to content
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

has many Relations in postgresql do not work #5782

Closed
gioppoluca opened this issue Jun 19, 2020 · 5 comments
Closed

has many Relations in postgresql do not work #5782

gioppoluca opened this issue Jun 19, 2020 · 5 comments

Comments

@gioppoluca
Copy link

Steps to reproduce

Current Behavior

created 2 tables with a relation between them but the GET method do not return the related rows
Using the rest-crud controller

Expected Behavior

Since I defined a relation property I'm expecting to have it filled up

I'm adding the 2 files of the model, the generated controller and the tables definition:
`
CREATE TABLE "micadoapp"."topic"
(
id smallint PRIMARY KEY NOT NULL,
icon text,
published bool DEFAULT false NOT NULL,
publication_date date
)
;
CREATE UNIQUE INDEX topic_pkey ON "micadoapp"."topic"(id)
;

CREATE TABLE "micadoapp"."topic_translation"
(
id smallint DEFAULT nextval('topic_translation_id_seq'::regclass) NOT NULL,
lang varchar(10) NOT NULL,
topic varchar(20),
translation_date timestamp,
topic_id smallint NOT NULL,
CONSTRAINT topic_translation_pk PRIMARY KEY (id,lang)
)
;
ALTER TABLE "micadoapp"."topic_translation"
ADD CONSTRAINT topic_translation_fk
FOREIGN KEY (topic_id)
REFERENCES "micadoapp"."topic"(id)
;
ALTER TABLE "micadoapp"."topic_translation"
ADD CONSTRAINT topic_translation_lang_fkey
FOREIGN KEY (lang)
REFERENCES "micadoapp"."languages"(lang) ON DELETE CASCADE ON UPDATE CASCADE
;
CREATE UNIQUE INDEX topic_translation_pk ON "micadoapp"."topic_translation"
(
id,
lang
)
;
topic-translation.model.ts.txt
topic.model.ts.txt
topic.rest-config.ts.txt

`

@gioppoluca gioppoluca added the bug label Jun 19, 2020
@jannyHou
Copy link
Contributor

cc @hacksparrow @agnes512 could you help take a look? Thanks.

@agnes512
Copy link
Contributor

@gioppoluca If you think it's a bug, could you make it clear that why it is a bug and the steps to reproduce? or please use the question label. Btw you can also join our public channel in slack and ask questions there. Invite link.

As for questions, could you state it clear as well? i.e you'd like to have the related model returned only, or have the target returned along with its related model?

Since rest-crud doesn't support creating relations yet ( see #2483), you will need to run lb4 relation to create them. See cli docs. The cli creates controllers that allows you to to get the related Translation model returned with route /topics/{id}/translations.

If you'd like to have the target returned along with its related mode, you will need to have the inclusion resolver set. Then use filter include in the query to get the result, something similar to: GET /topics?filter[include][][relation]=translations. The doc should have everything you need.

Since I only have limited information about your question, I could only answer anything that might be related. Again, if you think it's a bug, please provide steps to reproduce it. If the above doesn't help, you might need to provide the repositories as well. Thanks.

@HrithikMittal
Copy link
Contributor

Hi @gioppoluca you please try with the inclusion resolver set as @agnes512 told and you can refer these links for more help.
https://loopback.io/doc/en/lb4/HasMany-relation.html#querying-related-models
https://loopback.io/doc/en/lb3/Include-filter.html
I think it solve your query.

@HrithikMittal
Copy link
Contributor

Also, I think this blog would help you.
https://strongloop.com/strongblog/inclusion-of-related-models/

@dhmlau
Copy link
Member

dhmlau commented Aug 10, 2020

Closing due to inactivity.

@dhmlau dhmlau closed this as completed Aug 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants