Various bug fixes with foreign keys.