gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 00:29:20 24 4
gpt4 key购买 nike

框架:Laravel 5.2数据库:MySQLPHP:7.0

我有“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();
});

还有“转化”表:

    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();
});

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

谢谢?

最佳答案

你可以这样做。

<?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']);
});
}

}

归功于@bobbybouwmann

关于php - laravel 5 - 我如何添加 ->index() 到现有的外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44226598/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com