Use hasattr to see if a foreign key exists

There's no associated ticket and this was literally a five-second change.

All tests pass.