Hey,
you've correctly added the foreign key constraint in AddForeignkeysToUsersTable migration but the other migrations (AddForeignkeysToRolesTable, AddForeignkeysToTransactionsTable and AddForeignkeysToRolestransactionsTable) all reference "users" table instead of roles, transactions and rolestransactions respectively.
Thanks for the reply. I changed each reference on each migration and still get the same error. Here is one of them:
class AddForeignkeysToRolestransactionsTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('rolestransactions', function($table) {
$table->foreign('department_id')->references('id')->on('departments');
$table->foreign('role_id')->references('id')->on('roles');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('roles', function($table){
$table->drop_index('rolestransactions_department_id_foreign');
$table->drop_index('rolestransactions_role_id_foreign');
$table->drop_foreign('rolestransactions_department_id_foreign');
$table->drop_foreign('rolestransactions_role_id_foreign');
});
}
}
Hi swgj19, If you have any table than it has a foreign key on a related base table, you need, in the down method, delete before your secondary table and then delete the base table, because If you try to delete before you deleted the table than contains a FK you will get an error.
Another way could be drop the FK before deleting all tables.
"delete before your secondary table and then delete the base table"
Could you expand on this, I an not following you. I am new to the FK stuff.
Hi, swgj19, imagine that you have a user table and a userprofile table, with a 1:1 relationship. You link with a foreign key these two tables referencing the user_id in your users table as the Foreign Key in the userprofile table.
When you require down your migration you need delete your profile table before users table, and remove the drop your foreign key before drop your users table because If you drop before the users table one exception will be launch for this foreign key.
Hope it helps you
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community