Maybe rewrite this code this first check if this tracker exists in the TrackerInfo table and then insert the mapping in the TorrentTrackerMapping.
If it doesn't exist, first insert it into the TrackerInfo table and then use the new tracker_id to insert the mapping.