框架: Laravel 5.2数据库: MySQL PHP: 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)也是一个索引?如果不是..。我需要做什么才能使它成为索引?
谢谢?
发布于 2018-11-03 19:34:59
你可以做到的。
<?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']);
});
}
}
发布于 2019-05-22 19:27:30
当我写的时候,我遇到了问题:
$table->index('column_name');
因此,更准确地说,首先需要创建具有所需列类型的列,然后将其指定为索引,如下所示:-
$table->string('column_name');
$table->index('column_name');
//or
$table->string('column_name')->index();
我希望这能帮到你
发布于 2019-08-08 17:35:05
这是一个完美的解决方案。
Up
$table->string('poptin_id')->index('poptin_id');
关闭
$table->dropIndex(['poptin_id']);
修复迁移的建议
$table->string('id')->primary()->index();
将上面的替换为下面
$table->increments('id');
修复你的外键,如下所示
$table->integer('poptin_id')->index('poptin_id')->unsigned();
https://stackoverflow.com/questions/44226598
复制相似问题