A simple workaround: Place something to block the relays of the first factory, like an assembler. Place the first factory, and because it's relays are blocked you can break the assembler and put the second factory down. They will automatically link when you do that.
It's not ideal, but there's no easy way to code it without things being even more confusing unfortunately, the Factorio API just isn't flexible enough for weird situations like this.