Laravel: String data, right truncated: 1406 Data too long for column

匿名 (未验证) 提交于 2019-12-03 09:05:37

问题:

I have a table with a column 'hotel'. The project is created in Laravel 5.4, so I used Migrations.

    $table->string('hotel', 50); 

This is MYSQL VARCHAR (50). It was working good, because when I was developing I used short hotel names like "HILTON NEW YORK 5"*.

Now the project is on production and customer asked why they can't input long hotel names. I've tested it with such a mock hotel name as "Long long long long long long long long long and very-very-very long hotel name 5 stars"

It gave me an error:

"SQLSTATE[22001]: String data, right truncated: 1406 Data too long for column 'hotel' at row 1"

I've opened database in my Sequel Pro and changed it

  • first to VARCHAR (255)
  • then to TEXT

After each change I tested it with the same "Long long long long long long long long long and very-very-very long hotel name 5 starts" and get the same error (see above).

I've checked the type of column with

SHOW FIELDS FROM table_name 

and it gave me

Field | Type

hotel | text

so the type of the field is 'text' indeed (65 535 characters).

Maybe it's somehow connected with Laravel Migration file (see above) where I set VARCHAR (50) in the beginning? But I can't re-run migration on production, because the table has data now.

Would appreciate any help.


UPDATE: I discovered that it actually saves that long hotel name in the DB. But user still gets this annoying mistake every time after submitting the form...

回答1:

You need to create a new migration, register it with composer du command and run php artisan migrate command to change type of the column:

Schema::table('the_table_name', function (Blueprint $table) {     $table->string('hotel', 255)->change(); }); 


回答2:

change the type of column fromstring to text.

Then run a migrate refresh using php artisan migrate:refesh



回答3:

On your local development, try changing the column type to:

$table->longText('columnName') 

from your migration file. That solved it for me. But if you have gone live, then create a new migration just as Alexey has suggested and then use longText() column type.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!