简体   繁体   中英

Laravel: Migration error when having an index

I am getting the following error when running migrations. The error in my terminal console is:

SQLSTATE[42000]: Syntax error or access violation: 1170 BLOB/TEXT column 'referral_code' used in key specification without a key length (SQL: alter table `users` add index `users_referral_code_index`(`referral_code`))

Below is my actual migration file. I can't see to tell why it's happening, as I've used this pattern before with other migrations.

<?php

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

class AddReferralInfoToUsersTable extends Migration
{
    public function up()
    {
        Schema::table('shop_users', function (Blueprint $table) {
            $table->text('referral_code')->nullable();
            $table->integer('referral_uses')->nullable();
            $table->integer('referral_revenue')->nullable();
            $table->index(['referral_code']);
        });
    }
}

Try changing the datatype of referral_code to string and set a length.

Schema::table('shop_users', function (Blueprint $table) {
        $table->string('referral_code',100)->nullable();
}

You can try this:

Schema::table('shop_users', function (Blueprint $table) {
            $table->mediumText('referral_code')->nullable();
}

or

Schema::table('shop_users', function (Blueprint $table) {
            $table->string('referral_code',100)->nullable();
}

or

 Schema::table('shop_users', function (Blueprint $table) {
            $table->longText('referral_code')->nullable();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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