Skip to content

Commit

Permalink
Fix adding duplicate officers to incidents (#1006)
Browse files Browse the repository at this point in the history
## Description of Changes
Merging fix OrcaCollective#347
upstream.

**Original PR description**:
Only add new officers to incidents.

When a user edits an incident currently, we retrieve the Incident from
the database and append all officers in the form to that Incident. This
led to sqlalchemy trying to insert duplicate rows into the
officer_incidents association table.

As far as I can tell, this was silently ignored by sqlalchemy in the
past and no unexpected rows were created. With the upgrade to sqlalchemy
2.0.19 (OrcaCollective#344), the
library appears to have started raising exceptions related to this
behavior.

This change checks to see whether an officer already exists in
Incident.officers before adding them so as to not add duplicate
officers.

## Notes for Deployment
N/A

## Screenshots (if appropriate)
N/A

## Tests and linting
 - [x] This branch is up-to-date with the `develop` branch.
 - [x] `pytest` passes on my local development environment.
 - [x] `pre-commit` passes on my local development environment.
  • Loading branch information
sea-kelp authored Aug 3, 2023
1 parent 6a27393 commit cb63dfe
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion OpenOversight/app/main/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1626,7 +1626,7 @@ def populate_obj(self, form, obj):
except ValueError:
our_id = officer["oo_id"].split('value="')[1][:-2]
of = Officer.query.filter_by(id=int(our_id)).first()
if of:
if of and of not in obj.officers:
obj.officers.append(of)

license_plates = form.data.pop("license_plates")
Expand Down

0 comments on commit cb63dfe

Please sign in to comment.