Laravel 种子:违反完整性约束:1452 无法添加或更新子行:外键约束失败

Laravel Seed: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails

我构建 Twitter 克隆应用程序使用 Laravel5.4 和 While referring to this

现在我想用测试数据播种数据库

在我的终端输入php artisan db:seed --class=TweetsTableSeeder 收到错误

Integrity constraint violation: 1452 Cannot add
  or update a child row: a foreign key constraint fails (`twitter`.
  `tweets`, CONSTRAINT `tweets_user_id_foreign` FOREIGN KEY (`user_
  id`) REFERENCES `users` (`id`) ON DELETE CASCADE)

我看了错误,也试着去理解,但是没有什么好的结果。 我正在看官方文档,但因为初学者,我看不懂。

所以请帮助我

2017_07_09_create_tweets_table

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateTweetsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('tweets', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id')->unsigned()->index();
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
            $table->string('body', 140);
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('tweets');
    }
}

Tweet.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Tweet extends Model
{
    protected $fillable = [
        'user_id', 'body',
    ];
}

TweetsTableSeeder

<?php

use App\Tweet;
use Illuminate\Database\Seeder;

class TweetsTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        factory(Tweet::class, 10)->create([
        'user_id' => 2
        ]);
    }
}

ModelFactory.php

<?php

/*
|--------------------------------------------------------------------------
| Model Factories
|--------------------------------------------------------------------------
|
| Here you may define all of your model factories. Model factories give
| you a convenient way to create models for testing and seeding your
| database. Just tell the factory how a default model should look.
|
*/

/** @var \Illuminate\Database\Eloquent\Factory $factory */
$factory->define(App\User::class, function (Faker\Generator $faker) {
    static $password;

    return [
        'name' => $faker->name,
        'email' => $faker->unique()->safeEmail,
        'password' => $password ?: $password = bcrypt('secret'),
        'remember_token' => str_random(10),
    ];
});

$factory->define(App\Tweet::class, function (Faker\Generator $faker) {
    return [
        'body' => $faker->realText(140),
    ];
});

Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (twitter. tweets, CONSTRAINT tweets_user_id_foreign FOREIGN KEY (user_ id) REFERENCES users (id) ON DELETE CASCADE)

上述错误仅表示您正在尝试 seed(插入)tweets table 中的值,而该值在父 users 中不可用table.

通俗地说,当两个 table 共享 foreign key 关系时,只有那些已经存在于父 table 中的值才能插入到子 table 中],在您的情况下,您违反了上述规则。