Laravel Drop Foreign key
Hoe drop ik de foreign key in Laravel? Ik heb de volgende Up() function:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?php
public function up()
{
Schema::create('companys', function($table) {
$table->increments('id')->unsigned();
$table->string('company_name');
$table->string('slug')->unique();
});
Schema::create('user_company', function($table)
{
$table->increments('id')->unsigned();
$table->integer('user_id')->unsigned();
$table->integer('company_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users'); // assumes a users table
$table->foreign('company_id')->references('id')->on('companys');
});
}
public function up()
{
Schema::create('companys', function($table) {
$table->increments('id')->unsigned();
$table->string('company_name');
$table->string('slug')->unique();
});
Schema::create('user_company', function($table)
{
$table->increments('id')->unsigned();
$table->integer('user_id')->unsigned();
$table->integer('company_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users'); // assumes a users table
$table->foreign('company_id')->references('id')->on('companys');
});
}
[Illuminate\Database\QueryException]
SQLSTATE[HY000]: General error: 1025 Error on rename of '.\notifier\user_co
mpany' to '.\notifier\#sql2-df8-3a4' (errno: 152) (SQL: alter table `user_c
ompany` drop foreign key user_company_user_id_foreign)
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
class CreateCompanyTable extends Migration {
public function up()
{
Schema::create('companys', function($table) {
$table->increments('id')->unsigned();
$table->string('company_name');
$table->string('slug')->unique();
});
Schema::create('user_company', function($table)
{
$table->increments('id')->unsigned();
$table->integer('user_id')->unsigned();
$table->integer('company_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users'); // assumes a users table
$table->foreign('company_id')->references('id')->on('companys');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down(){
Schema::table('user_company', function(Blueprint $table) {
$table->dropForeign('user_company_user_id_foreign');
});
//Schema::drop('companys');
//Schema::drop('user_company');
}
}
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
class CreateCompanyTable extends Migration {
public function up()
{
Schema::create('companys', function($table) {
$table->increments('id')->unsigned();
$table->string('company_name');
$table->string('slug')->unique();
});
Schema::create('user_company', function($table)
{
$table->increments('id')->unsigned();
$table->integer('user_id')->unsigned();
$table->integer('company_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users'); // assumes a users table
$table->foreign('company_id')->references('id')->on('companys');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down(){
Schema::table('user_company', function(Blueprint $table) {
$table->dropForeign('user_company_user_id_foreign');
});
//Schema::drop('companys');
//Schema::drop('user_company');
}
}