Fixed a bug when writing foreign keys