1

外部キー 'user_id' を使用して、別のデータベースにあるテーブル 'users' に移行しました: 入力するとエラーが発生します (php artisan migrate)。

 [Illuminate\Database\QueryException]
 SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL
 : alter table `safetyreports` add constraint safetyreports_user_id_foreign
 foreign key (`user_id`) references `users` (`id`) on delete cascade)

これで、laravel は「user_id」列を除いてテーブルを作成しました。どうすればこれを達成できますか?

これは私の移行コードです:

Schema::create('safetyreports', function(Blueprint $table) {
            $table->increments('id');
            $table->string('afdeling');
            $table->string('contractor');
            $table->string('directe chef');
            $table->string('ploeg');
            $table->string('team');
            $table->string('plant_afd');
            $table->string('datum');
            $table->string('plaats');
            $table->string('tijd');
            $table->string('omschrijving');
                        $table->integer('user_id')->unsigned();
            $table->foreign('user_id')
      ->references('id')->on('users')
      ->onDelete('cascade');
            $table->timestamps();
        });

これは私のユーザーモデルコードです:

protected $connection = 'mysql2';
protected $table = 'users';

Users モデルをテストしました。できます。User:all() でテストしてから、すべてのユーザーを画面に表示しました。

これにより、外部キークロスデータベーステーブルが可能になりますか?

4

0 に答える 0