【问题标题】:Laravel Seed: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint failsLaravel 种子:违反完整性约束:1452 无法添加或更新子行:外键约束失败
【发布时间】:2017-07-09 14:30:21
【问题描述】:

我使用 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),
    ];
});

【问题讨论】:

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


    【解决方案1】:

    违反完整性约束:1452 无法添加或更新子项 行:外键约束失败(twitter.tweets, CONSTRAINT tweets_user_id_foreign 外键 (user_ id) 引用 users (id) 删除级联)

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

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

    【讨论】:

    • 感谢您的回复。感谢您提供易于理解的答案。为了插入这个种子,我应该更改违规的内容,对吗?如果可以,请告诉我要修复哪个部分?
    • 有时候解决方法很简单,你甚至看不到!谢谢!!
    • @christostsang 享受咖啡和快乐的编码;)
    • 根据经验,在构建 DatabaseSeeder.php 文件时遵循迁移顺序会更好。
    猜你喜欢
    • 2020-08-13
    • 1970-01-01
    • 2018-04-26
    • 2014-03-12
    • 2016-10-27
    • 2015-05-25
    • 1970-01-01
    相关资源
    最近更新 更多