When trying to define column as Unique, error is generated by talking 767 bytes

Asked

Viewed 868 times

3

When I try to run a Migration on Laravel, the following error is generated:

[Illuminate Database Queryexception] SQLSTATE[HY000]: General error: 1709 Index column size Too large. The Maximum column size is 767 bytes. (SQ L: alter table usuarios add-on usuarios_email_unique(email))

[Pdoexception] SQLSTATE[HY000]: General error: 1709 Index column size Too large. The Maximum column size is 767 bytes.

The sql being executed is this:

create table `usuarios` ( 
  `id` int unsigned not null auto_increment primary key, 
  `nome` varchar(255) not null, 
  `email` varchar(255) not null, 
  `password` varchar(255) not null, 
  `nivel_id` int unsigned not null, 
  `empresa_id` int unsigned not null, 
  `departamento_id` int unsigned null, 
  `cargo_id` int unsigned null, 
  `status` tinyint(1) not null default '1', 
  `created_at` timestamp null, 
  `updated_at` timestamp null) 
default character set utf8mb4 collate utf8mb4_unicode_ci

alter table `usuarios` add unique `usuarios_email_unique`(`email`)

alter table `usuarios` add index `usuarios_password_index`(`password`)   

What could be generating this?

  • These are the settings of Mysql, I was also with this problem and solved through this link, see if it helps you: https://stackoverflow.com/questions/42043205/how-to-fix-mysql-index-column-size-too-large-laravel-migrate

  • This is because the email is too large to accept creating the index.unico. Solution diminiu the size from 255 to 100 will work I think also an exaggeration 255

  • @arllondias so worked kkkkk, I just switched the connection charset and worked correctly

2 answers

2

I solved this problem by changing the charset and the collation in the dataset settings, in config/database.php:

//'charset' => 'utf8mb4',
//'collation' => 'utf8mb4_unicode_ci',
'charset'   => 'utf8',
'collation' => 'utf8_unicode_ci',

Another solution, already presented in the other question, but can be done globally rather than individually, is to set the maximum string size to 191. This should be done in the AppServiceProvider::boot.

Schema::defaultStringLength(191);

Note: I don’t think it’s a good idea to use the second option, charset and the collation.

2

Summarizing the columns of the type string of Laravel comes with default 255 which causes the size to exceed the maximum for the Internet, go to your migration and change the lenght key field unique to 191 or to the size you prefer, for example:

$table->string('email', ['length' => 191])->unique();

You can add in your file AppServiceProvider.php within the method boot() the following instruction for your field string stick around default and do not need to declare at all migration the size of the string field:

Schema::defaultStringLength(191);

  • Not a good idea, in my opinion. The "password" field is also giving this same problem, and I believe the generated hash can pass this size...

  • No more than 191, I have some applications in Laravel and we set the default string for this size precisely to not give gap for errors futros

  • @Wallacemaxters so that.put index in the password and another will not pop the hash is much lower

  • @Wallacemaxters edited the answer, and that’s the way we solved it here, so you don’t have to keep changing the size of the string field.

  • @arllondias I solved the problem by changing from utf8mb4 for a utf8_*_ci of life.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.