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

时间:2017-07-09 14:30:21

标签: php mysql laravel-5 laravel-5.4 seeding

我使用Laravel5.4和While referring to this

构建Twitter克隆应用程序

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

在我的终端输入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),
    ];
});

1 个答案:

答案 0 :(得分:1)

  

完整性约束违规:1452无法添加或更新子级   row:外键约束失败(twittertweets,CONSTRAINT   tweets_user_id_foreign外键(user_ id)参考users   (id)ON DELETE CASCADE)

上述错误只是意味着您试图seed(插入)tweets表中的值,而该值在父users表中不可用。

在外行术语中,当两个表共享外键关系时,只能将这些值插入到父表中已存在的子表中。在您的情况下,您违反了上述规则。