Skip to content

Define ORM objects for EMIS v2 observations and medications tables#2736

Closed
alarthast wants to merge 4 commits intomainfrom
aw/add-observation-and-medication-issue-schema
Closed

Define ORM objects for EMIS v2 observations and medications tables#2736
alarthast wants to merge 4 commits intomainfrom
aw/add-observation-and-medication-issue-schema

Conversation

@alarthast
Copy link
Copy Markdown
Contributor

@alarthast alarthast commented Apr 1, 2026

Closes #2725.

The schemas were added semi-manually in that column types were obtained via connecting to EMIS's staging environment, and cross-referenced with the spreadsheet provided by the Optum team.

The types were obtained by extending @rebkwok's jupyter notebook and adding a cell that pulls out the columns using sqlalchemy.inspect(engine).get_columns(schema=schema, table_name=table_name).

The cell's code is here, where the schema and table_names used for this extraction were "explorer_open_safely" and ['patient','medication_issue_record', 'observation','consultation'] respectively.
I leave it up to the reviewer of this PR to decided whether to review the code snippet as well: I think it is reasonable to choose to only review the added schema definitions themselves, which I have checked to be consistent with the information in the spreadsheet linked in the ticket.

Based on this slack conversation about the possibility that we might use consultation datetime for medications and observations instead of the effective_datetime values on the tables themselves, I have added a commit that defines the consultation ORM object as well.

We will be asserting more definitions within this test,
so rename it `test_schema` and lightly refactor the imports
and variable names.
Connecting to the EMIS staging environment via
SQLAlchemy revealed that:

- `patient_id` is defined as `VARBINARY` without a specified length.
Previously 16 was assumed based on the example value provided by Optum,
but it seems safer not to assume a specific length.

- `date_of_birth` uses the Trino `TIMESTAMP` type rather than
SQLAlchemy's `DateTime`.

Use the types obtained from the staging environment instead.

I chose the `trdt` shorthand for `trino.sqlalchemy.datatype` somewhat
arbitrarily, but it seems distinct enough from `t` and not too long to
be cluttering in the file.

Note that I was unable to obtain primary key / foreign key information
from connecting to the staging environment. I have nonetheless kept
the `unique` constraint on `patient_id` since the schema spreadsheet
provided by Optum indicates that this is a unique identifier.
- Types were obtained from connecting to the EMIS staging environment
via SQLAlchemy.

- Test values used were adapted from the excel sheet provided by Optum,
except for the binary patient_id which is `bytes(range(16))` for
clarity.

- Tables are ordered alphabetically to be consistent with the TPP
schema file.

- This commit does not include the `consultation_id` column in the two
tables; the next commit will do so.
We might decide in the future to use the datetime of the consultation
as the effective datetime for the observation and medication issue
records. Add a consultation schema to allow for this possibility.

Note that I was unable to obtain primary key / foreign key information
from connecting to the staging environment. I have nonetheless used
a `unique` constraint on `consultation_id` since the schema spreadsheet
provided by Optum indicates that this is a unique identifier.
@alarthast alarthast marked this pull request as ready for review April 2, 2026 08:36
@alarthast alarthast marked this pull request as draft April 2, 2026 14:52
@alarthast
Copy link
Copy Markdown
Contributor Author

Discussed on Slack: moving the bit for the patient table to #2739. We'll re-do this PR when the minimal backend has been implemented.

@alarthast alarthast closed this Apr 7, 2026
@alarthast alarthast deleted the aw/add-observation-and-medication-issue-schema branch April 10, 2026 17:05
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

Successfully merging this pull request may close these issues.

Define ORM objects manually for EMIS v2 observations and medications tables

1 participant