Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String as Primary Key in Laravel migration

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

like image 947
DGeo Avatar asked May 28 '17 15:05

DGeo


1 Answers

Change it to string.

$table->string('code', 30)->primary(); 
like image 147
Sandeesh Avatar answered Oct 13 '22 19:10

Sandeesh