Referential integrity problems and how to restore migrations in Laravel 5

2

Hi, I had some referential integrity problems making a model. Basically I had an article with a category and tags

For tags create another table that has the id of the article and the tag, since it may contain more than one. Here I had the first problem because if I want to delete an article I forgot the onDelete cascade in that pivot table

$table->foreign('articulo_id')->references('id')->on('articulos')->onDelete('cascade');

This woke me the doubt of what happened if I delete a category, the category_id in article as I do to handle it, in principle I would like to put null and I see how I handle it, since to place an OnDelete, I do not think that if it is deleted a category, necessarily have to delete an article. But I did not find how to do it from the migrations in laravel or do I have to modify the value of the name when I do the destroy?

And once the migrations have been edited. Is there a way to restore them without deleting the data in the database?

    
asked by Cidius 15.09.2016 в 19:33
source

1 answer

0

Assuming your foreign key is nullable , you could use set null :

$table->integer('articulo_id')->unsigned()->nullable();

$table->foreign('articulo_id')->references('id')->on('articulos')->onDelete('set null');

A bit of MySQL documentation: link

    
answered by 15.09.2016 / 19:44
source