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

Updating using extra in related query #2593

Open
rajumb502 opened this issue Feb 1, 2024 · 0 comments
Open

Updating using extra in related query #2593

rajumb502 opened this issue Feb 1, 2024 · 0 comments

Comments

@rajumb502
Copy link

Refer: #413

Context:
I have a many to many relation between subjects and assessments through a mapping table class_subjects_assessments. The class_subjects_assessments table has a bunch of extra fields and I would like to update a specific relation specified by one of the extra fields csa_id using $relatedQuery():

                await subject
                    .$relatedQuery('assessments', tx)
                    .where('csa_id','=', csaId)
                    .patch({
                      state: status,
                      completed_questions: completedQuestions
                    });

The above generates the sql:

                update "class_subjects_assessments" set "state" = ?, "completed_questions" = ? 
                where 
                  "class_subjects_assessments"."assessment_id" in 
                    (
                      select "assessments"."id" 
                      from "assessments" inner join "class_subjects_assessments" 
                        on "assessments"."id" = "class_subjects_assessments"."assessment_id" 
                      where "class_subjects_assessments"."subject_id" in (?) 
                        and "csa_id" = ?
                    ) 
                  and "class_subjects_assessments"."subject_id" in (?)

However, what I required was this:

                update "class_subjects_assessments" set "state" = ?, "completed_questions" = ? 
                where 
                  "class_subjects_assessments"."assessment_id" in 
                    (
                      select "assessments"."id" 
                      from "assessments" inner join "class_subjects_assessments" 
                        on "assessments"."id" = "class_subjects_assessments"."assessment_id" 
                      where "class_subjects_assessments"."subject_id" in (?) 
                        and "csa_id" = ?
                    ) 
                  and "class_subjects_assessments"."subject_id" in (?)
                  and "csa_id" = ?  ---- <- NOTE

The first query ends up updating all the records where assessment_id and subject_id match, which is not what I required.

For now, I created a model for class_subjects_assessments as a workaround. Is there any way to use $relatedQuery for my requirement without creating this model?

Packages:
"graphql": "16.8.1",
"graphql-yoga": "4.0.3",
"knex": "2.5.1",
"objection": "3.1.2",
"pg": "8.11.3",

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant