Bug fix when using primary keys