简体   繁体   English

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

[英]LARAVEL:MYSQL : Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails

I am trying to apply a foreign Key to column user_id, related with ID of table users 我正在尝试将外键应用于与表用户 ID相关的user_id列

I have tried doing it by migrations and in workbench and this error keeps coming: 我尝试通过迁移和在工作台中执行此操作,并且此错误不断出现:

Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails 违反完整性约束:1452无法添加或更新子行:外键约束失败

this is the mysql: 这是mysql:

ALTER TABLE `dh_booky`.`books` 

ADD CONSTRAINT `books_user_id_foreign`

  FOREIGN KEY (`user_id`)

  REFERENCES `dh_booky`.`users` (`id`)

  ON DELETE NO ACTION

  ON UPDATE NO ACTION;

1st migration books: 第一本迁移书籍:

 public function up()
    {
        Schema::create('books', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->bigInteger('title_id')->unsigned();
            $table->foreign('title_id')->references('id')->on('titles');
            $table->bigInteger('author_id')->unsigned();
            $table->foreign('author_id')->references('id')->on('authors');
            $table->string('image')->nullable();

        });
    }

Update migration books: 更新迁移书籍:

class UpdateBooksTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('users', function (Blueprint $table) {
        $table->bigInteger('user_id')->unsigned();
        $table->foreign('user_id')->references('id')->on('users');
        //php artisan make:migration update_votes_table



});

User migration: 用户迁移:

public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->timestamps();
            $table->string('name');
            $table->string('email')->unique();
            $table->string('password');
            $table->rememberToken();
        });
    }

Seems like you are updating users table instead of books table in UpdateBooksTable migration. 似乎您要更新的是用户表,而不是UpdateBooksTable迁移中的books表。

You might already have data on the books table. 您可能已经在books表上有了数据。 Because of which, the existing books does not reference the user_id and it is causing the error. 因此,现有书籍没有引用user_id,这会导致错误。

You can add nullable() to the user_id so that existing books have user_id null. 您可以将nullable()添加到user_id,以便现有书籍的user_id为null。

class UpdateBooksTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        //Seem you are updating books table. change users to books 
        Schema::table('users', function (Blueprint $table) {
            $table->bigInteger('user_id')->unsigned()->nullable();
            $table->foreign('user_id')->references('id')->on('users');
        });
    }
}

Set the user migration to earlier, before the books table migration happens. 在进行books表迁移之前,将用户迁移设置为早期版本。 Otherwise, there is no user table in existence yet to reference when creating the books table, and thus the error. 否则,在创建books表时,尚无要引用的用户表,因此会出现错误。

This one first : 这个第一

Schema::create('users', function (Blueprint $table) {...}

Then when you create the books table, the foreign key to users will have a table to connect to. 然后,在创建books表时,用户的外键将有一个表可连接。

Migration file code should be below: 迁移文件代码应如下:

        Schema::table('books', function (Blueprint $table) {
            $table->unsignedBigInteger('user_id');
            $table->foreign('user_id')
                  ->on('users')
                  ->references('id');
        });

It can be produce SQL like this: 可以这样产生SQL:

ALTER TABLE books

ADD CONSTRAINT books_user_id_foreign

FOREIGN KEY fk_user_id(user_id)

REFERENCES users (id)

ON DELETE NO ACTION

ON UPDATE NO ACTION;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Laravel 5:违反完整性约束:1452 无法添加或更新子行:外键约束失败 - Laravel 5: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails Laravel 5.2 - 违反完整性约束:1452 无法添加或更新子行:外键约束失败 - Laravel 5.2 - Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails Laravel:SQLSTATE [23000]:违反完整性约束:1452无法添加或更新子行:外键约束失败 - Laravel: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails SQLSTATE [23000]:违反完整性约束:1452无法添加或更新子行:Laravel 5中的外键约束失败 - SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails in Laravel 5 Laravel - 完整性约束违规:1452 无法添加或更新子行:外键约束失败 - Laravel - Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails SQLSTATE [23000]:完整性约束违规:1452 无法添加或更新子行:外键约束失败 - Laravel - SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails - Laravel Laravel-SQLSTATE [23000]:违反完整性约束:1452无法添加或更新子行:外键约束失败 - Laravel - SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails Laravel Seed:违反完整性约束:1452无法添加或更新子行:外键约束失败 - Laravel Seed: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails Laravel 6:完整性约束违规:1452 无法添加或更新子行:外键约束失败 - Laravel 6: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails Laravel 6 - SQLSTATE[23000]:违反完整性约束:1452 无法添加或更新子行:外键约束失败 - Laravel 6 - SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM