PostgreSQL constraint error

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 might be causing this error?

    
asked by anonymous 13.02.2015 / 20:49

1 answer

1

The error is in the creation of the foreign key of the table "HORARIOTURMA" that is related to the "SCHEDULE" table

The primary key of the "TIME" table consists of two columns (daySpanish, hour) so the foreign key of the "TIMEZONE" table should 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)
);
    
14.02.2015 / 17:50