php Laravel:字符串数据,右截断:1406 列的数据太长

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48477861/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:41:13  来源:igfitidea点击:

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

phpmysqllaraveltexttypes

提问by Sergej Fomin

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

我有一张桌子,上面有一列“酒店”。该项目是在 Laravel 5.4 中创建的,所以我使用了迁移。

    $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"*.

这是 MYSQL VARCHAR (50)。它运行良好,因为在我开发时我使用了简短的酒店名称,例如“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"

现在项目正在制作中,客户问为什么他们不能输入长酒店名称。我已经用这样的模拟酒店名称对其进行了测试,例如“长长长长长长长长很长很长很长的酒店名称 5 星”

It gave me an error:

它给了我一个错误:

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

“SQLSTATE[22001]:字符串数据,右截断:1406 第 1 行‘酒店’列的数据太长”

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

我在 Sequel Pro 中打开了数据库并更改了它

  • first to VARCHAR (255)
  • then to TEXT
  • 首先到 VARCHAR (255)
  • 然后到文本

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

每次更改后,我都使用相同的“Long long long long long long long long long long 和非常非常非常长的酒店名称 5 开始”对其进行了测试,并得到相同的错误(见上文)。

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

所以该字段的类型确实是“文本”(65 535 个字符)。

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.

也许它与我在开始时设置 VARCHAR (50) 的 Laravel 迁移文件(见上文)有某种联系?但是我无法在生产中重新运行迁移,因为该表现在有数据。

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

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

采纳答案by Alexey Mezenin

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

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

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

回答by JDK Ben

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.

从您的迁移文件。那为我解决了。但是如果你已经上线,那么就像 Alexey 建议的那样创建一个新的迁移,然后使用longText()列类型。

回答by Abi Raj Nijanandi

Change column's datatype from string to text and do not give length.

将列的数据类型从字符串更改为文本并且不提供长度。

回答by Pramodya Abeysinghe

change the type of column fromstringto text.

将列的类型从 更改stringtext

Then run a migrate refresh using php artisan migrate:refesh

然后使用运行迁移刷新 php artisan migrate:refesh

回答by Guvanch Hojamov

Change column's datatype from string to text and do not give length.

将列的数据类型从字符串更改为文本并且不提供长度。

$table->text('hotel')->change();

回答by Ing Oscar MR

I was storing pictures as base64 on a text colum so I got a SQL error:

我将图片作为 base64 存储在文本列上,因此出现 SQL 错误:

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

I did my migration as

我做了我的迁移

$table->text('picture')

then I changed de column picture as:

然后我将 de 列图片更改为:

$table->mediumText('picture')

I realiced that text column allows to store only 64 KB

我意识到文本列只允许存储 64 KB

TEXT: 65,535 characters - 64 KB MEDIUMTEXT: 16,777,215 - 16 MB LONGTEXT: 4,294,967,295 characters - 4 GB

文本:65,535 个字符 - 64 KB 中等文本:16,777,215 - 16 MB 长文本:4,294,967,295 个字符 - 4 GB

For more info visit: understanding-strorage-sizes-for-mysql-text-data-types

更多信息请访问:understanding-strorage-sizes-for-mysql-text-data-types