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

Add support for alias to Legacy Models #423

Merged
merged 2 commits into from
Sep 17, 2023

Conversation

Cruikshanks
Copy link
Member

DEFRA/water-abstraction-team#97

We hit a snag as part of the Great Rename!

We want water.charge_elements to be ChargeReferences. We then want water.charge_purposes to be ChargeElements. But there is a relationship between the 2 that has to be defined in ChargeReferenceModel.

The problem is when you run const query = await ChargeReferenceModel.query().innerJoinRelated('chargeElements') you get an error from Objection.js.

select "charge_elements".* from "water"."charge_elements" inner join "water"."charge_purposes" as "charge_elements" on "charge_elements"."charge_element_id" = "charge_elements"."charge_element_id" - table name "charge_elements" specified more than once

If we can get Objection to alias charge_elements as charge_references in our query then we would remove the error. To do this we need to use alias() in the custom query builder used by the model. Currently, all our models extend LegacyBaseModel which uses SchemaQueryBuilder to ensure all legacy models apply a schema.

This change refactors SchemaQueryBuilder to also handle applying a table alias when one exists.

DEFRA/water-abstraction-team#97

We hit a snag as part of the **Great Rename**!

We want `water.charge_elements` to be `ChargeReferences`. We then want `water.charge_purposes` to be `ChargeElements`. But there is a relationship between the 2 that has to be defined in `ChargeReferenceModel`.

The problem is when you run `const query = await ChargeReferenceModel.query().innerJoinRelated('chargeElements')` you get an error from [Objection.js](https://vincit.github.io/objection.js).

```
select "charge_elements".* from "water"."charge_elements" inner join "water"."charge_purposes" as "charge_elements" on "charge_elements"."charge_element_id" = "charge_elements"."charge_element_id" - table name "charge_elements" specified more than once
```

If we can get Objection to alias `charge_elements` as `charge_references` in our query then we would remove the error. To do this we need to use `alias()` in the custom query builder used by the model. Currently, all our models extend `LegacyBaseModel` which uses `SchemaQueryBuilder` to ensure all legacy models apply a schema.

This change refactors `SchemaQueryBuilder` to also handle applying a table alias when one exists.
@Cruikshanks Cruikshanks added the housekeeping Refactoring, tidying up or other work which supports the project label Sep 16, 2023
@Cruikshanks Cruikshanks self-assigned this Sep 16, 2023
@Cruikshanks Cruikshanks marked this pull request as ready for review September 17, 2023 16:08
@Cruikshanks Cruikshanks merged commit fdad64a into main Sep 17, 2023
@Cruikshanks Cruikshanks deleted the support-table-alias-in-legacy-models branch September 17, 2023 16:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
housekeeping Refactoring, tidying up or other work which supports the project
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant