500 when creating child resources for parents that don't exist

Bug #1667809 reported by Thomas Maddox
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
craton
New
Undecided
Unassigned

Bug Description

Looks like we don't gracefully handle missing parents in the Craton API, so if someone tries to, say, create a cell for a region that doesn't exist, we get back a 500.

Example:

$ curl -s http://127.0.0.1:8080/v1/regions -H "Content-Type: application/json" -H "X-Auth-Token: demo" -H "X-Auth-User: demo" -H "X-Auth-Project: b9f10eca66ac4c279c139d01e65f96b4" | jq
{
  "links": [...],
  "regions": [
    {
      "created_at": "2017-02-24T20:51:19.000000",
      "id": 1,
      "name": "ORD135",
      "note": null,
      "project_id": "b9f10eca-66ac-4c27-9c13-9d01e65f96b4",
      "updated_at": null
    },
    {
      "created_at": "2017-02-24T20:51:20.000000",
      "id": 2,
      "name": "DFW",
      "note": null,
      "project_id": "b9f10eca-66ac-4c27-9c13-9d01e65f96b4",
      "updated_at": null
    }
  ]
}

Note there are only regions 1 and 2. Now try creating a cell for region 3, which doesn't exist.

$ curl -X POST -s http://127.0.0.1:8080/v1/cells -H "Content-Type: application/json" -H "X-Auth-Token: demo" -H "X-Auth-User: demo" -H "X-Auth-Project: b9f10eca66ac4c279c139d01e65f96b4" -d '{"name": "thomas-cell-1", "region_id": 3}'
{
  "message": "Unknown Error",
  "status": 500
}

Error in API logs:

2017-02-24 20:52:16.609 192 ERROR craton.api.v1.resources.inventory.cells [req-869e5402-ca4a-4571-bec5-641783a5e96b - - - - -] Error during Cells.post: (pymysql.err.IntegrityError) (1452, 'Cannot add or update a child row: a foreign key constraint fails (`craton`.`cells`, CONSTRAINT `cells_ibfk_2` FOREIGN KEY (`region_id`) REFERENCES `regions` (`id`))') [SQL: 'INSERT INTO cells (created_at, updated_at, region_id, project_id, name, note, variable_association_id) VALUES (%(created_at)s, %(updated_at)s, %(region_id)s, %(project_id)s, %(name)s, %(note)s, %(variable_association_id)s)'] [parameters: {'updated_at': None, 'created_at': datetime.datetime(2017, 2, 24, 20, 52, 16, 606973), 'region_id': 3, 'name': 'thomas-cell-1', 'note': None, 'variable_association_id': 32, 'project_id': 'b9f10eca66ac4c279c139d01e65f96b4'}]

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.