Fix primary key handling when primary key is a `ForeignKey`