JonathanGarro / SIMS-Portal

IFRC Surge Information Management Support Portal
3 stars 4 forks source link

Fix emergency type lookup on create #85

Closed JonathanGarro closed 2 months ago

JonathanGarro commented 1 year ago

Creating emergencies with certain types like biological raises an error:

sqlalchemy.exc.IntegrityError: (psycopg2.errors.ForeignKeyViolation) insert or update on table "emergency" violates foreign key constraint "emergency_emergency_type_id_fkey"
DETAIL:  Key (emergency_type_id)=(66) is not present in table "emergencytype".

[SQL: INSERT INTO emergency (emergency_name, emergency_status, emergency_glide, emergency_go_id, emergency_location_id, emergency_review_id, activation_details, slack_channel, dropbox_url, trello_url, emergency_type_id, updated_at) VALUES (%(emergency_name)s, %(emergency_status)s, %(emergency_glide)s, %(emergency_go_id)s, %(emergency_location_id)s, %(emergency_review_id)s, %(activation_details)s, %(slack_channel)s, %(dropbox_url)s, %(trello_url)s, %(emergency_type_id)s, %(updated_at)s) RETURNING emergency.id]
[parameters: {'emergency_name': 'Tester', 'emergency_status': 'Active', 'emergency_glide': '12341234', 'emergency_go_id': 123412, 'emergency_location_id': 14, 'emergency_review_id': None, 'activation_details': 'asdfasdf', 'slack_channel': '', 'dropbox_url': '', 'trello_url': '', 'emergency_type_id': 66, 'updated_at': None}]
(Background on this error at: https://sqlalche.me/e/14/gkpj)

Looks like the FK on the model needs to point to the correct column, but not sure why other types are working.

JonathanGarro commented 2 months ago

mirrored the emergency_type_go_id to the id to avoid this conflict.