1

I don't use Postgres much (I used to use other DB's) so recently I was viewing a db in pgAdmin and saw an odd schema syntax which I can't find anything on the documentation and want to know a bit about it:

So I see this syntax:

CREATE TABLE table1
(
  table1_id serial NOT NULL,
  table2_fk_id t2_id NOT NULL, -- what is this line?
  ...
  CONSTRAINT fk_table2_chm FOREIGN KEY (table2_fk_id)
      REFERENCES table2 (t2_id_col) MATCH SIMPLE
      ON UPDATE RESTRICT ON DELETE RESTRICT
);

What is that marked line above? It seems like the foreign key col rather than specifying int/int8 it is specifying a custom name t2_id?

Whereas in table2 pk column which is being referenced above is of type serial and is labeled t2_id_col.

Is that some exotic syntax? Or something related to pgAdmin?

Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228
iQ.
  • 3,811
  • 6
  • 38
  • 57

1 Answers1

2

To tell for sure, run:

SELECT *
FROM   pg_type
WHERE  typname = 't2_id'

If typtype is d it's a domain, created with CREATE DOMAIN.
Or in pgAdmin just check the list of domains in the "Object browser".

And yes, this is somewhat related to pgAdmin, since it does you the courtesy of displaying a serial column as such. serial is not an actual type, the underlying type is integer. So your domain type must be based on integer as well to allow the foreign key, which requires matching base types. Details:
Safely and cleanly rename tables that use serial primary key columns in Postgres?

Community
  • 1
  • 1
Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228