Added tentative support for foreign keys. I just hope I didn't break too much other stuff...