Support the ongoing development of Laravel.io →
posted 10 years ago
Database
Last updated 2 years ago.
0

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.

Last updated 2 years ago.
0

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');
		});
	}

}
Last updated 2 years ago.
0

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.

Last updated 2 years ago.
0

"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.

Last updated 2 years ago.
0

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

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

swgj19 swgj19 Joined 11 Mar 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.