Laravel:字符串数据,右截断:1406数据对于列而言太长 [英] Laravel: String data, right truncated: 1406 Data too long for column

查看:630
本文介绍了Laravel:字符串数据,右截断:1406数据对于列而言太长的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有酒店"列的表格.该项目是在Laravel 5.4中创建的,因此我使用了Migrations.

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

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

这是MYSQL VARCHAR(50).效果很好,因为在开发时我使用了诸如"HILTON NEW YORK 5 " *之类的简短酒店名称.

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

现在该项目正在生产中,客户询问为什么他们不能输入长的酒店名称.我已经用长很长很长很长很长很长很长而且非常非常长的酒店名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"

它给了我一个错误:

"SQLSTATE [22001]:字符串数据,右截断:1406数据对于 第1行的酒店"列"

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

我已在Sequel Pro中打开数据库并进行了更改

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

  • 首先访问VARCHAR(255)
  • 然后转到文本

每次更改后,我都使用相同的长很长很长很长很长很长很长而且非常非常长的酒店名称5开始"进行了测试,并得到了相同的错误(见上文).

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).

我已经用

SHOW FIELDS FROM table_name

它给了我

字段|输入

Field | Type

酒店|文字

因此字段的类型确实是文本"(65 535个字符).

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

也许它与Laravel迁移文件(请参见上文)以某种方式联系在一起,我一开始就在其中设置了VARCHAR(50)?但是我不能在生产上重新运行迁移,因为该表现在有数据.

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.

非常感谢您的帮助.

更新: 我发现它实际上将那个长旅馆名称保存在数据库中.但是用户每次提交表单后仍然会遇到这个烦人的错误...

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...

推荐答案

您需要创建一个新的迁移,并使用composer du命令进行注册,然后运行php artisan migrate命令来更改列的类型:

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();
});

这篇关于Laravel:字符串数据,右截断:1406数据对于列而言太长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆