Typescript Select Distinct Postgresql Create Foreign Key