简体   繁体   English

如何将具有外键的列修改为null?

[英]How do I modify a column to null that has foreign key?

I have to change the type of the user_id column from accounts to nullable, but it's a foreign key with the id column of users, how should it be done correctly? 我必须将user_id列的类型从帐户更改为可为空,但这是具有用户id列的外键,应如何正确完成?

I'm working with Laravel 5.6.27 and MySQL 5.6 , I tried using the functions of the facade, but it did not work. 我正在使用Laravel 5.6.27MySQL 5.6 ,尝试使用Facade的功能,但没有用。 Now I am testing with the statements and I have this error: 现在,我正在测试这些语句,并且出现此错误:

In Connection.php line 664: SQLSTATE[HY000]: General error: 1025 Error on rename of './gonano/#sql-c_b' to './gonano/accounts' (errno: 150 - For key constraint is incorrectly formed) (SQL: ALTER TABLE accounts CHANGE user_id user_id INT DEFAULT NULL) 在Connection.php行664中:SQLSTATE [HY000]:常规错误:1025将'./gonano/#sql-c_b'重命名为'./gonano/accounts'时出错(错误号:150-键约束的格式不正确) (SQL:ALTER TABLE帐户更改user_id user_id INT默认为NULL)

create_users_table: create_users_table:

public function up(): void
{
    Schema::create('users', function (Blueprint $table): void {
        $table->increments('id');
        $table->string('first_name', 100);
        $table->string('last_name', 100);
        $table->string('email')->unique();
        $table->bigInteger('cuit')->unsigned();
        $table->string('password');
        $table->enum('status', ['activated', 'blocked'])->default('activated');
        $table->integer('country_id')->unsigned()->references('id')->on('countries');
        $table->timestampsTz();
    });
}

create_accounts_table: create_accounts_table:

public function up(): void
{
    Schema::create('accounts', function (Blueprint $table): void {
        $table->bigIncrements('id');
        $table->string('address')->default('');
        $table->integer('user_id')->index()->unsigned();
        $table->integer('system_id')->index()->unsigned()->default(0);
        $table->foreign('user_id')->references('id')->on('users');
        $table->timestampsTz();
    });
}

change_user_id_to_nullable_in_accounts: change_user_id_to_nullable_in_accounts:

public function up()
{
    Schema::disableForeignKeyConstraints();
    DB::table('accounts')->truncate();
    Schema::table('accounts', function (Blueprint $table): void {
        $table->integer('user_id')->nullable()->change();
    });
    Schema::enableForeignKeyConstraints();
}

I need that user_id can be nullable and I can not achieve it, thanks for the help 我需要user_id可以为空并且无法实现,谢谢您的帮助

Create new migration for make nullable to your user_id. 创建新的迁移,以使您的user_id可为空。

and paste this code in your migration. 并将此代码粘贴到您的迁移中。

public function up()
{
    \Illuminate\Support\Facades\DB::statement('SET FOREIGN_KEY_CHECKS=0;');
    \Illuminate\Support\Facades\DB::table('accounts')->truncate();
    Schema::table('accounts', function (Blueprint $table) {
        $table->integer('user_id')->nullable()->change();
    });
    \Illuminate\Support\Facades\DB::statement('SET FOREIGN_KEY_CHECKS=1;');
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM