laravel 5 - 如何将 ->index() 添加到现有外键

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44226598/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:03:05  来源:igfitidea点击:

laravel 5 - How can i add ->index() to existing foreign key

phpmysqlsqllaravellaravel-5

提问by Dani Banai

Framework: Laravel 5.2 Database: MySQL PHP: 7.0

框架:Laravel 5.2 数据库:MySQL PHP:7.0

I have the table "pops":

我有桌子“弹出”:

    Schema::create('pops', function (Blueprint $table) {
        $table->string('id')->primary()->index();
        $table->string('ab_test_parent_id')->nullable();
        $table->string('cloned_parent_id')->nullable();
        $table->timestamps();
    });

And the table "conversions":

表“转换”:

    Schema::create('conversions', function (Blueprint $table) {
        $table->string('id')->primary()->index();
        $table->integer('users')->default(null);
        $table->string('name',256)->default(null);
        $table->string('pop_id');
        $table->foreign('pop_id')->references('id')->on('pops')->onDelete('cascade')->onUpdate('cascade');
        $table->timestamps();
    });

I have set a foreign key (pop_id) in the "conversions" table. Now does it's mean that the foreign key (pop_id) is also a index? if not... what i need to do in order to make it a index?

我在“conversions”表中设置了一个外键(pop_id)。现在这是否意味着外键(pop_id)也是一个索引?如果没有...我需要做什么才能使它成为索引?

Thank?

谢谢?

回答by chebaby

You can just do this.

你可以这样做。

<?php

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

class AddIndexToLeads extends Migration {

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('leads', function(Blueprint $table)
        {
            $table->index('trader_id');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('leads', function (Blueprint $table)
        {
            $table->dropIndex(['trader_id']);
        });
    }

}

credit to @bobbybouwmann

归功于@bobbybouwmann

回答by Sandeesh

Laravel only adds a foreign key constraint and doesn't add index implicitly. But some databases such as MySQL automatically index foreign key columns.

Laravel 只添加了外键约束,并没有隐式添加索引。但有些数据库如 MySQL 会自动索引外键列。

If you need to add index to a field in another migration. Then you could do

如果您需要为另一个迁移中的字段添加索引。那么你可以做

$table->index('email');

回答by zarpio

Here is a perfect solution for you.

这里有一个完美的解决方案。

Up

向上

$table->string('poptin_id')->index('poptin_id');

Down

$table->dropIndex(['poptin_id']);

Suggestion to fix your migrations

修复迁移的建议

$table->string('id')->primary()->index();

replace above with below

用下面替换上面

$table->increments('id');

and fix your foreign key as below

并修复您的外键如下

$table->integer('poptin_id')->index('poptin_id')->unsigned();

回答by Vipertecpro

I was facing problem when i just write :-

我刚写的时候遇到了问题:-

$table->index('column_name');

So to be more exact first you need to create column with desired column type then assign it to be indexlike this :-

因此,更准确地说,您首先需要创建具有所需列类型的列,然后将其分配为这样的索引:-

  $table->string('column_name');
  $table->index('column_name');
  //or
  $table->string('column_name')->index();

I hope this helps

我希望这有帮助