Hi all,
I'm implementing polymorphic many to many relationships. My table structure is identical to that shown in the Laravel documentation: https://laravel.com/docs/4.2/eloquent#many-to-many-polymorphic-relations
I remember experimenting with a standard many to many relationship in the past and noticed that without setting a foreign constraint on the pivot table, there would be "orphaned" records left around after deleting from one of the tables. For example, if I had a Posts table and a Tags table and deleted a Post, I'd no longer want the joining record in the pivot table and since Laravel doesn't delete it automatically I'd need to set a foreign key constraint with ON DELETE CASCADE to delete it.
Now I want to apply the Tags to multiple models, I'm trying the same technique adding the foreign key, but of course Laravel's schema builder automatically names the foreign key constraints for you, so the following doesn't work...
$table->foreign('taggable_id')->references('id')->on('videos')->onDelete('cascade');
$table->foreign('taggable_id')->references('id')->on('posts')->onDelete('cascade');
When running the migration, it complains that the constraint "taggables_taggable_id_foreign" already exists.
So I suppose my question is - should I manually write out the SQL for the constraint to my database migration or am I approaching the whole thing incorrectly?
Thanks in advance.