String as Primary Key in Laravel migration

DGeo picture DGeo · May 28, 2017 · Viewed 29.4k times · Source

I've had to change a table in my database so that the primary key isn't the standard increments.

Here's the migration,

public function up()
{
    Schema::create('settings', function (Blueprint $table) {
        $table->text('code', 30)->primary();
        $table->timestamps();
        $table->text('name');
        $table->text('comment');
    });
}

However, MySQL keeps returning with,

Syntax error or access violation: 1170 BLOB/TEXT column 'code' used in key specification without a key length (SQL: alter table settings add primary key settings_code_primary(code)

I've tried leaving the normal increments id in there and modifying the table in a different migration but the same thing happens.

Any ideas of what I'm doing wrong?

Laveral Version 5.4.23

Answer

Sandeesh picture Sandeesh · May 28, 2017

Change it to string.

$table->string('code', 30)->primary();