There are two bugs here.
One, the duchy containing the capital should not be allowed to switch realms.
Two, the last duchy of a realm should not be allowed to switch realms.
Whoever made the first duchy change without reporting it as a bug is as guilty as Sebastian.