エラー内容
外部キー制約設定の際に、
php aritisan migrate でこのようなエラーが..
Illuminate\Database\QueryException : SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `files` add constraint `files_user_id_foreign` foreign key (`user_id`) references `users` (`id`))
at /var/www/html/kaikei-laravel/vendor/laravel/framework/src/Illuminate/Database/Connection.php:665
661| // If an exception occurs when attempting to run a query, we'll format the error
662| // message to include the bindings with SQL, which will make this exception a
663| // lot more helpful to the developer instead of just the database's errors.
664| catch (Exception $e) {
> 665| throw new QueryException(
666| $query, $this->prepareBindings($bindings), $e
667| );
668| }
669|
Exception trace:
1 PDOException::("SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint")
/var/www/html/kaikei-laravel/vendor/laravel/framework/src/Illuminate/Database/Connection.php:459
2 PDOStatement::execute()
/var/www/html/kaikei-laravel/vendor/laravel/framework/src/Illuminate/Database/Connection.php:459
Please use the argument -v to see more details.
マイグレートファイルのコードはこれ。
2020_01_04_132245_add_user_id_to_files.php
class AddUserIdToFiles extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('files', function (Blueprint $table) {
$table->bigInteger('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('files', function (Blueprint $table) {
$table->dropColumn('user_id');
});
}
}
いろいろ調べてみると、、
外部キーの参照元のuserテーブルのidがbigIncrementsだった。。
つまり、bigIncrementsとintegerで一致していない。
外部キーを設定する場合、どうやらデータ型が違うとだめらしい。
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
解決法
データ型を合わせるために外部キーに指定するuser_idのintegerをbigIntegerに修正してみる。
(bigIncrementsとbigIntegerで合わせるため)
2020_01_04_132245_add_user_id_to_files.php
class AddUserIdToFiles extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('files', function (Blueprint $table) {
$table->bigInteger('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('files', function (Blueprint $table) {
$table->dropColumn('user_id');
});
}
}
php artisan migrate:fresh を実行してみるとできました!
(もちろんDBレコードのデータは削除されます)
# php artisan migrate:fresh
Dropped all tables successfully.
Migration table created successfully.
laravel5.8以降はphp artisan make:migration 〜で作成した場合、
idはbigIncrementsに自動的になってしまうのですね。
参考
https://laraveldaily.com/be-careful-laravel-5-8-added-bigincrements-as-defaults/
https://qiita.com/ktknj/items/6a3e1936da727dbd92b3