rogerdpack
rogerdpack

Reputation: 66931

Postgres cannot create foreign key to serial

I'm trying to create some Postgres tables, one has a foreign key on anothers' index:

CREATE TABLE Foo (ID SERIAL PRIMARY KEY);
CREATE TABLE File (FooId TEXT NOT NULL REFERENCES Foo (ID));

But it results in ERROR: foreign key constraint file_fooid_fkey cannot be implemented

Upvotes: 2

Views: 2701

Answers (1)

rogerdpack
rogerdpack

Reputation: 66931

Appears the types have to "match" or you'll get that error.

This works (change to int to match the size/type of SERIAL KEY which is an INT):

CREATE TABLE Foo (ID SERIAL PRIMARY KEY);
CREATE TABLE File (FooId INT NOT NULL REFERENCES Foo (ID));

Upvotes: 4

Related Questions