John Erbynn
John Erbynn

Reputation: 375

PostreSql: ERROR: column referenced in foreign key constraint does not exist

What might possibly gone wrong?

ERROR: column "parameter_id" referenced in foreign key constraint does not exist
1 statement failed.

So i created 2 tables(parameters and periods) with the commands;

create table parameters(
  parameter_id serial primary key,
  temperature real,
  feels_good_temperature int,
  humidity int,
  max_uv_index int
);

and

create table periods(
  period_id serial primary key,
  time_stamp text
);

...and now i want to have a column, fk_paramter_id in periods table, as a foreign key to the parameters table

I tried achieving it with;

ALTER TABLE periods 
ADD CONSTRAINT fk_parameter_id FOREIGN KEY (parameter_id) 
REFERENCES parameters(parameter_id);

Upvotes: 1

Views: 2524

Answers (1)

Laurenz Albe
Laurenz Albe

Reputation: 248095

You first have to create and populate the column:

ALTER TABLE periods ADD parameter_id integer;

Then use UPDATEs to set the correct values.

Now you can define the constraint.

Note: the constraint (fk_paramter_id) is not a column; it is defined on a column (parameter_id).

Upvotes: 2

Related Questions