Better foreign key handling: correct SQL output