Skip to content

BDMS-112 Add missing fields to Thing model, schema, and tests#166

Merged
jirhiker merged 39 commits into
stagingfrom
thing_model_update_20250930
Oct 3, 2025
Merged

BDMS-112 Add missing fields to Thing model, schema, and tests#166
jirhiker merged 39 commits into
stagingfrom
thing_model_update_20250930

Conversation

@ksmuczynski

Copy link
Copy Markdown
Contributor

Why

This PR addresses the following problem / context:

  • The Thing model needed to inherit the new tables that have been created.
  • The Thing model needed a first_visit_date field to store the date of NMBGMR's first recorded interaction with a specific Thing.
  • A well_purpose field was needed to capture the primary function of a well (Monitoring, Irrigation, etc)

How

Implementation summary - the following was changed / added / removed:

  • New StatusHistoryMixin and PermissionMixin were imported and inherited by the Thing model.
  • Relationships were added and updated in the Thing model and related models.
  • Syntax/notation was updated to reflect the SQLAlchemy 2.0 style.
  • A first_visit_date field was added.
  • A well_purpose field was added.
  • A well_type field existed in schema files, test files, and the lexicon.json. The well_type field served the same purpose as the well_purpose field, despite not existing in the Thing model. Instances of well_type were replaced by well_purpose based on AMP feedback received on 2025-08-29.

Notes

Any special considerations, workarounds, or follow-up work to note?

  • Script files were not updated.

feat: add missing relationships to `Thing` model
feat: add relationships to `GroupThingAssociation` table.

feat: add proxy to `Group` table
@codecov-commenter

codecov-commenter commented Sep 30, 2025

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ All tests successful. No failed tests found.

Files with missing lines Coverage Δ
api/search.py 97.77% <ø> (ø)
db/__init__.py 97.50% <100.00%> (ø)
db/base.py 96.61% <ø> (ø)
db/contact.py 100.00% <100.00%> (ø)
db/deployment.py 100.00% <ø> (ø)
db/field.py 100.00% <100.00%> (ø)
db/group.py 100.00% <100.00%> (ø)
db/permission.py 94.73% <100.00%> (ø)
db/sample.py 100.00% <ø> (ø)
db/thing.py 100.00% <100.00%> (ø)
... and 4 more

…le from `foreign(permissible_id) == Thing.thing_id` to `foreign(permissible_id) == Thing.id`
…StatusHistoryMixin`

The previous logic dynamically generated a primary key name based on the
class name (e.g., "thing_id"), which failed for models using the
AutoBaseMixin that provides a standard "id" column.

This change modifies the f-string to use the correct ".id" attribute,
resolving the AttributeError during mapper configuration.
The polymorphic relationships `_thing_target` and `_location_target`
caused a `NameError` at runtime because they referenced the `Thing` and
`Location` models directly. These models were only imported within a
`TYPE_CHECKING` block to avoid circular dependencies, making them
unavailable when the code was executed.

This commit refactors the `primaryjoin` condition to be a string. This
allows SQLAlchemy to defer the evaluation of the join until all models
are fully loaded, resolving the `NameError` and making the relationship
definitions more robust.
Comment thread api/search.py Outdated
Comment thread db/contact.py
Comment thread db/deployment.py Outdated
Comment thread db/thing.py Outdated
Comment thread db/thing.py
Comment thread tests/conftest.py Outdated
Comment thread db/thing.py
Comment thread schemas/thing.py Outdated
Comment thread schemas/thing.py Outdated
Comment thread schemas/thing.py
…, and addresses relationships in `Contact` model
Columns, relationships, and proxies were randomly added to the `Contact` model. It has been reorganized so like things are grouped together under a designed header.
SQLAlchemy was raising an `ArgumentError` because it could not infer
the direction of the one-to-many polymorphic relationships defined in
the `StatusHistoryMixin` and `PermissionMixin`.

Without a formal `ForeignKey` constraint on the child columns
(`statusable_id` and `permissible_id`), SQLAlchemy could not determine
which side of the join was the foreign key.

This commit resolves the ambiguity by wrapping these child columns with
the `foreign()` annotation in their respective `primaryjoin` conditions.
This explicitly marks them as the foreign-key side of the join,
allowing the mapper to configure correctly.
Comment thread db/deployment.py Outdated
The `Thing` model's search_vector referenced the
`well_casing_description` column, but it was not defined in the
schema, causing an `AttributeError` on startup.

The `well_casing_description` column was renamed `well_casing_material` so the search_vector was updated accordingly.
`first_visit_date` was missing from the `schemas/thing.py` UPDATE model. It has been added.

Renamed instances of 'well_type' to 'well_purpose'

Commented out the `Thing.description` field
The `well_type` property was renamed to `well_purposed` in the `make_well_response` function.
Instances of ondelete, CASCADE should be a str e.g. ondelete="CASCADE". The CASCADE from tkinter and tkinter itself was removed.
`first_visit_date` field should be of type `PastDate` to ensure a future date is not entered.
…n-nullable)

When creating a new `Thing` record, users should be required to enter a value for `first_visit_date`. This applies to new records being created - the database itself will still allow NULLS in order to accommodate legacy data.
The `thing_groups` relationship was updated to `group_associations` for clarity. It more descriptive of the relationship and is also consistent with the naming convention used in the group table (`group_associations)`.

The back_populates property reference in the group.thing relationship was updated from "thing_groups" to "group_associations" to reflect the update to the relationship name in the Thing table.
# noqa: F821 calls were removed since TYPE_CHECKING is invoked.
Comment thread db/contact.py Outdated
Comment thread db/contact.py
Comment thread schemas/thing.py Outdated
…dEventParticipants`. Update instances of `field_event_contact_associations` to `field_event_participants`
The foreign key on  `field_event_contact_id` field was incorrectly named "field_event_participant.id". It was updated to "field_event_participants.id"
Change data type for `first_visit_date` from `date` to `PastDate`

@jacob-a-brown jacob-a-brown left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good to me

@jirhiker jirhiker merged commit 9a9fd8d into staging Oct 3, 2025
3 checks passed
@ksmuczynski ksmuczynski deleted the thing_model_update_20250930 branch October 8, 2025 19:17
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.

4 participants