K
K
Ken Jee2018-08-20 16:28:57
Laravel
Ken Jee, 2018-08-20 16:28:57

How to create indexes for PostgreSQL 10.1 fields in Laravel 5.6 migrations?

I want to add a field in the migration script to the table with a check for uniqueness. I try to do this in two ways, which I give below. But none of them creates the correct index for the field in PostgreSQL. The field itself is created, but the index is not. What could be the problem?

public function up()
{
    Schema::table('testtable', function (Blueprint $table) {
        $table->string('testfield')->unique();
    });
}

public function up()
{
    Schema::table('testtable', function (Blueprint $table) {
        $table->string('testfield');     
        $table->unique('testfield', 'testfield_unique');
    });
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
JhaoDa, 2018-08-20
@Machez

uniqueit is necessary to look not in the list of indexes, but in the list of restrictions (constraint).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question