There are 3 concepts for this that should be understood.
1. A Pokémon has a trainer details to indicate who was the first trainer the Pokémon had.
2. A Pokémon has a trainer details to indicate who last had possession of it, besides the original trainer.
3. A Pokémon has a flag indicating who is currently handling it, because Friendship is tracked separately.
PKHeX displays the current handler as a green background. PKHeX automatically updates this flag whenever a Pokémon is set to a save file. If a Pokémon's OT details matches that of the save file it is being set to, its handler flag will be set to OT, otherwise, the HT (Handling Trainer) data is updated and the current handler flag is set to indicate HT.
Your confusion is likely arising due to creating data for a save file using the same trainer details for other-game data. Example: an Emerald Pikachu having the trainer details of your Sword game will be recognized as being handled by the OT, because PKHeX (mimicking the games) does not check to see if the Version is different.
Don't reply to posts that are 4 years old with an attitude like that, lmao. PKHeX's legality checker is a legality checker, not a "here's how to fix every single hacked piece of data imaginable".