【问题标题】:Laravel 5: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint failsLaravel 5:完整性约束违规:1452 无法添加或更新子行:外键约束失败
【发布时间】:2015-05-25 04:58:14
【问题描述】:

我有一个简单的文章模型和一个用户模型。

文章“属于”一个用户和一个用户“hasMany”文章。

因此我的文章迁移有一个名为“user_id”的外键。

    Schema::create('articles', function(Blueprint $table) {
        $table->increments('id');
        $table->string('title');
        $table->text('body');
        $table->integer('user_id')->unsigned();
        $table->foreign('user_id')->references('id')->on('users');
        $table->timestamps();
    });

然而,每当我创建文章并在隐藏字段中传递“user_id”时,我都会收到一条错误消息。

{!! Form::open(array('route' => 'articles.store')) !!}

    {!! Form::hidden('userId', $user->id) !!}

    <div class="form-group">
        {!! Form::label('title', 'Title') !!}
        {!! Form::text('title', null, array('class' => 'form-control')) !!}         
    </div>

    <div class="form-group">
        {!! Form::label('text', 'Write your Article') !!}
        {!! Form::textarea('text', null, array('class' => 'form-control')) !!}          
    </div>

    {!! Form::submit('Create Article', array('class' => 'btn btn-default btn-success')) !!}
{!! Form::close() !!}

这是错误信息。而且我知道我不会尝试将“user_id”的值插入到文章表中。

SQLSTATE[23000]:违反完整性约束:1452 无法添加或 更新子行:外键约束失败(tags.articles, 约束 articles_user_id_foreign 外键 (user_id) 参考users (id)) (SQL: 插入articles (title, body, updated_at, created_at) 值(标题,一些文本, 2015-03-21 23:19:33, 2015-03-21 23:19:33))

这是我的 AriclesController 上的存储方法:

    Article::create([
        'title'   => Input::get('title'),
        'body'    => Input::get('text'),
        'user_id' => Input::get('userId')
    ]);

    return Redirect::to('articles');

还有几十个标题相似的其他未解决的 Stackoverflow 问题,我正在寻找适合我的具体情况的答案但未成功,因此我提前感谢你好心的陌生人。

如何将文章保存到我的数据库中?

【问题讨论】:

    标签: php mysql laravel


    【解决方案1】:

    确保您的 Article 模型的 fillable 属性中有 user_id

    http://laravel.com/docs/5.0/eloquent#mass-assignment

    【讨论】:

    • 谢谢亚当。不敢相信我忘记将“user_id”放入可填充数组中。
    • 在将 'user_id' 添加到可填充数组时应注意,因为在某些情况下,它允许用户在您不想允许时更改模型的所有者
    【解决方案2】:

    我遇到了同样的问题。我向已经存在并有数据的表添加了一个外键,但我无法运行迁移,因为它一直失败。 使迁移工作的解决方案是使外键可以为空。

    $table->integer('user_id')->nullable()->unsigned();
    
    // then the foreign key
    $table->foreign('user_id')->references('id')->on('users');
    

    希望它可以在将来节省其他人的时间。

    【讨论】:

    • 我有三个表:users、products、product_user(一个数据透视表)。当我尝试删除数据透视表中引用的产品之一时,我收到“无法更新或删除父行”。你的解决方案对我有用。
    • 我有相关的产品和类别表。首先,我忘记创建外键关系。 $table->bigInteger('category_id')->unsigned() 抛出错误,但您的解决方案有效。我猜这是一个 mysql 问题。
    • @TheSammie 谢谢人。拯救了我的一天。
    【解决方案3】:

    在为现有表运行迁移时,我曾多次遇到此错误。原因如下:

    1. 'user_id' 和 users 'id' 的类型不同。 例如,当 'user_id' 为 INT 而用户 'id' 为 BIGINT。

    2. 'user_id' 列不为空,需要设为可空

    3. “user_id”列中存在用户“id”中不存在的值

    【讨论】:

    • 对我来说这是第三个选项:我向表 A 添加了一个外键列,它使用默认值 0,但由于引用的表 B 为空,0 不是有效值。所以我将外键列设置为可为空:$table-&gt;unsignedBigInteger('B_table_id')-&gt;nullable();
    • 更新我上面的评论:更好的解决方案是清除表 A 中的数据,然后我就可以避免使用 nullable()
    【解决方案4】:

    我遇到了这个问题,因为我正在使用现有数据更新现有表,因此添加外键会导致问题,因为没有默认外键分配给条目。我首先从表中删除数据,然后重新运行迁移解决了这个问题

    【讨论】:

      【解决方案5】:

      对我来说,通过在 Laravel 7+ 中添加 nullable() 你可以这样做 foreignId 所以对于你的情况 $table-&gt;foreignId('user_id')-&gt;nullable()-&gt;constrained()-&gt;cascadeOnDelete();

      对于回滚 $table-&gt;dropForeign(['user_id']); 然后 $table-&gt;dropColumn('user_id');

      【讨论】:

        猜你喜欢
        • 2020-08-13
        • 1970-01-01
        • 2014-03-12
        • 2016-10-27
        • 1970-01-01
        • 2014-01-09
        • 2017-04-13
        相关资源
        最近更新 更多