Add a Foreign Key in PgAdmin

user4621642 picture user4621642 · Jan 4, 2016 · Viewed 30.3k times · Source

I have studidtemplates table below:

enter image description here

template_id is the primary_key

enter image description here

I want to create a new table referencing template_id as a foreign key. It is named studidtemplatetextelements. See image below:

enter image description here

I created a column template_id in the second table and want to make it a foreign key referencing template_id in studidtemplates table. I did it by clicking the button in the Constraints tab,pointed by an arrow in the image below.

enter image description here

I notice something different. In 'Referencing' option there's no template_id option available. See image below:

enter image description here

Where am I missing?

Answer

rotarydial picture rotarydial · Nov 21, 2017

In pgAdmin 4, here are the steps:

  1. Right-click on the table and select Properties.
  2. Within the dialog that appears, click Constraints / Foreign Key.
  3. Click the + icon on the upper-right of the Foreign key table.
  4. Click the pencil icon, which is all the way on the left of the new row that now appears in the Foreign key table.
  5. Under the General tab, give your foreign key a name (I follow this convention of {tablename}_{columnname}_fkey).
  6. Under the Columns tab, select your references.
  7. Click the + icon to the right of the "Local column".
  8. Click the Save button at the bottom.

Here's a screenshot:

pgAdmin 4 foreign key dialog