简体   繁体   English

Laravel 5 - 将列和外键添加到包含数据的现有表中

[英]Laravel 5 - Add column and foreign key to an existing table with data

I am very new at Laravel!我是 Laravel 的新手! But I'm trying.但我正在努力。 Here is were I could use some help;这是我可以使用的一些帮助;

I have a posts table.我有一个帖子表。 I have a user table.我有一个用户表。 But I forgot to add a foreign key in the posts table that links to the user id.但是我忘了在链接到用户 ID 的帖子表中添加外键。

The create users table migration: create users 表迁移:

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('users');
    }
}

The create posts table migration:创建帖子表迁移:

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreatePostsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->increments('id');
            $table->string('title');
            $table->text('body');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('posts');
    }
}

I have created the new migration file:我已经创建了新的迁移文件:

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AlterTablePostsAddForeignUser extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
      Schema::table('posts', function (Blueprint $table) {
          // I HAVE NO IDEA WHAT TO DO NEXT
      });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        //
    }
}

I have no idea how to fill up the "public function up()" method here!我不知道如何填写这里的“public function up()”方法! If anyone can help!如果有人可以帮忙! It will be much appreciated!将不胜感激!

I tried this and this worked for me.我试过这个,这对我有用。

Schema::table('posts', function (Blueprint $table) {
      $table->integer('user_id')->nullable()->unsigned();
      $table->foreign('user_id')->references('id')->on('users');
 });

Here When you adding foreign key, by default it will have a Null value.This 'null referencing' may cause the issue.这里当您添加外键时,默认情况下它将具有 Null 值。此“空引用”可能会导致问题。 Using nullable() foreign key will avoid that issue.使用nullable()外键可以避免这个问题。

See the docs here请参阅此处的文档

You need to install doctraine/dbal before modifying a column, be sure to add the doctrine/dbal dependency to your composer.json file.您需要在修改列之前安装doctraine/dbal ,请务必在您的 composer.json 文件中添加学说/dbal 依赖项。 The Doctrine DBAL library is used to determine the current state of the column and create the SQL queries needed to make the specified adjustments to the column and the you should try what I found on this SO post Doctrine DBAL 库用于确定列的当前状态并创建对列进行指定调整所需的 SQL 查询,您应该尝试我在这篇SO 帖子中找到的内容

Schema::table('posts', function (Blueprint $table) {
    $table->integer('user_id')->unsigned()->change();

    $table->foreign('user_id')->references('id')->on('users');
}); 

change() method for change structure of column change()列结构的change()方法

after this run the artisan command在此之后运行工匠命令

php artisan migrate

if this doesn't work for you shoot the comment here!如果这对您不起作用,请在此处发表评论! :) :)

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

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