fixed some bugs and surprisingly works at least for primary keys, foreign not tested yet