Postgresql Constraint error

Asked

Viewed 485 times

0

I have a code that is giving the following error:

there is no Unique Constraint matching Given Keys for referenced table "schedule"

Code:

CREATE TYPE weekday AS ENUM ('Domingo', 'Segunda', 'Terça', 'Quarta', 'Quinta', 'Sexta', 'Sabado');

-- Table Turma
CREATE TABLE TURMA
(
  codigoTurma serial PRIMARY KEY,
  nome character varying(50) NOT NULL UNIQUE
);

-- Table Horario
CREATE TABLE HORARIO
(
  diaSemana weekday,
  hora time,

  PRIMARY KEY(diaSemana, hora)
);

-- Table: Horario Turma
create table HORARIOTURMA(

   codigoTurma serial REFERENCES TURMA(codigoTurma),
   diaSemana weekday  REFERENCES HORARIO(diaSemana),
   hora time          REFERENCES HORARIO(hora),

   PRIMARY KEY (codigoTurma, diaSemana, hora)
);

What may be generating this error?

1 answer

1

The error is in creating foreign key from table "HORARIOTURMA" which is related to the table "HOURLY"

The primary key of the table "HORARIO" consists of two columns (diaSemana, hour) so the foreign key of the table "HORARIOTURMA" must reference these two columns.

CREATE TABLE HORARIOTURMA(
   codigoTurma serial ,
   diaSemana weekday  ,
   hora time          ,
   PRIMARY KEY (codigoTurma, diaSemana, hora),
   FOREIGN KEY (codigoTurma) REFERENCES TURMA (codigoTurma),   
   FOREIGN KEY (diaSemana, hora) REFERENCES HORARIO (diaSemana, hora)
);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.