The German parliament has narrowly rejected an opposition-sponsored bill calling for tougher rules on migration that risked becoming the first draft legislation to pass thanks to a far-right party.