How do I solve error due to foreign key restriction?

0

I have an error in my database in some tables, they have information inside, the problem is that when I try to delete some data I miss this error

ERROR:  update o delete en «cronograma» viola la llave foránea «fk_participantes_id_cronograma» en la tabla «participantes»
DETALLE:  La llave (id_cronograma)=(3) todavía es referida desde la tabla «participantes».

For what I understand is that a data is still referenced with the one of the table that I want to eliminate, I would like to know how it is solved

    
asked by Edwin Aquino 08.09.2017 в 18:14
source

2 answers

2

If what you want to do is that when deleting a schedule you also delete the participants what you can do is specify "cascade erasure" when defining the relationship:

ALTER TABLE cronograma 
  DROP CONSTRAINT fk_participantes_id_cronograma;
ALTER TABLE
  ADD FOREIGN KEY (id_cronograma) 
    REFERENCES participantes (id_cronograma)
    ON DELETE CASCADE;

Note: The names of the tables and fields were deducted from your question, but they could be others.

    
answered by 09.09.2017 в 03:01
0

If you want to delete record from table «cronograma» and it does not really affect the elimination of the relationship that exists between these tables, what you should do is delete the foreign key and there if you can delete it from that table, without worrying for the relationship.

This is the sentence you should use:

ALTER TABLE cronograma
DROP CONSTRAINT fk_participantes_id_cronograma;
    
answered by 08.09.2017 в 18:39