-1

I created a table called 'publicaciones' in the database called 'curso'. This table has a foreign key column that references to the id column of another table called 'usuarios'. When I try to insert a row into the 'publicaciones' table I got the Error Code 1452 which says 'Cannot add or update a child row; a foreign key contrait fails'.

TABLE publicaciones

CREATE TABLE curso.publicaciones (
    id int(11) NOT NULL AUTO_INCREMENT,
    autor_id int(11) NOT NULL,
    titulo varchar(150) NOT NULL,
    texto text NOT NULL,
    PRIMARY KEY(id),
    FOREIGN KEY (autor_id) REFERENCES curso.usuarios(id) ON DELETE CASCADE
);

INSERT query

INSERT INTO curso.publicaciones (
    autor_id,
    titulo,
    texto
) VALUES (
    '1',
    'Clean Code',
    'You should be a clean coder. Thanks.'
);

I tried adding the constraint "ON DELETE CASCADE", but it does not work.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Levliam
  • 11
  • 2
  • Does a row exist in `usuarios` with the id of 1? That id value in that table must be inserted before you can insert the row in `publicaciones` that references it. – Bill Karwin Feb 12 '23 at 20:58
  • https://stackoverflow.com/questions/1253459/mysql-error-1452-cannot-add-or-update-a-child-row-a-foreign-key-constraint-fa – Ken White Feb 12 '23 at 21:03
  • Your foreign key constraint says **a row with the same autor_id I am inserting must already exist in the `usarios` table**. Clearly, it does not if you're getting this error. Do you not understand the constraint? You're trying to add a publication by an author that does not exist yet. – Ken White Feb 12 '23 at 21:05
  • @KenWhite The `ON DELETE CASCADE` option is part of a foreign key constraint. In this case, it's correct to define it in the `publicaciones` table. It goes with the foreign key because you another table might contain a FK reference to `usuarios`, but without the cascading option. It's an option on the foreign key, not on the referenced primary key. – Bill Karwin Feb 12 '23 at 21:57
  • @BillKarwin: Yes, I am fully aware of what ON DELETE CASCADE is. My point was that the OP was putting it in the wrong place, and that it had no impact on the problem in the question. – Ken White Feb 12 '23 at 22:06
  • 1
    @KenWhite, It's irrelevant to the error they got, but it is a proper FK definition. It makes sense to put `ON DELETE CASCADE` in the `publicaciones` table shown, because if an author is deleted, then their publications will also be deleted. It's not in the wrong place. – Bill Karwin Feb 12 '23 at 22:18
  • Does it actually say *"a foreign key contrait fails"*? Not *"a foreign key [constraint](https://en.wiktionary.org/wiki/constraint#Noun) fails"*? (Possible typo with two missing letters). [contrait](https://en.wiktionary.org/wiki/contrait) is a French word (verb); infinitive: *"1. to block, to counter, to counteract"* – Peter Mortensen Feb 13 '23 at 08:27

1 Answers1

2

When you created the table curso.publicaciones you defined the column autor_id as a foreign key pointing curso.usuarios. That means the should exists a row in the table curso.usuarios where the id is the same autor_id that you are trying to insert. Make sure that there is a row in curso.usuarios with the id '1'.