laravel MySQL 列的整数值不正确,该列是整数并允许为空?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24347906/
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-09-14 09:41:11  来源:igfitidea点击:

Incorrect integer value '' for a MySQL column that's integer and allow null?

phpmysqlsqllaravellaravel-4

提问by Chris Farrugia

I'm working on inserting a CSV into a table. I don't have any control over what's in the CSV and a lot of the fields are blank. In my very first record for instance, the field "baths_full" is just empty (two commas back to back).

我正在将 CSV 插入表中。我无法控制 CSV 中的内容,而且很多字段都是空白的。例如,在我的第一条记录中,“baths_full”字段是空的(两个逗号背靠背)。

On my production server running MySQL 5.5.37, it inserts the record with the baths_fullas an empty field. On my local machine running MySQL 5.6.19, it gives me the error:

在我运行 MySQL 5.5.37 的生产服务器上,它将记录插入baths_full为空字段。在我运行 MySQL 5.6.19 的本地机器上,它给了我错误:

[Illuminate\Database\QueryException]
SQLSTATE[HY000]: General error: 1366 Incorrect integer value: '' for column 'baths_full' at row 1 (SQL: insert into `listings_queue` (`

The weird thing is that the schema for the tables is identical. In fact, I used the export of the production machine to create the local database.

奇怪的是,表的架构是相同的。其实我是用生产机的export来创建本地数据库的。

The field baths_fullis set to TinyInt, unsigned, allow null, default null. One thing to add is that it looks like in the SQL insert statement Laravel is creating, it is treating null values as spaces. Regardless, my production machine runs the script without trouble but locally it won't run.

该字段baths_full设置为 TinyInt,无符号,允许为空,默认为空。要添加的一件事是它看起来像 Laravel 正在创建的 SQL 插入语句中,它将空值视为空格。无论如何,我的生产机器运行脚本没有问题,但在本地它不会运行。

采纳答案by Chris Farrugia

I found my problem. My local MySQL is running in strict mode. The answer from this thread (General error: 1366 Incorrect integer value with Doctrine 2.1 and Zend Form update) fixed it.

我发现了我的问题。我的本地 MySQL 以严格模式运行。这个线程的答案(一般错误:1366 Incorrect integer value with Doctrine 2.1 and Zend Form update)修复了它。

回答by DBC Media Services

If the database column already contains a value of different data type not the same with the one you are changing to can throw error 1366, for example if a string value is already in one column of the table you are changing to tinyint. I once ran into this.

如果数据库列已经包含不同数据类型的值与您要更改的值不同,则可能会引发错误 1366,例如,如果字符串值已在您要更改为 tinyint 的表的一列中。我曾经遇到过这个。

回答by albertdiones

why not try type casting it to integer before inserting?

为什么不在插入之前尝试将它的类型转换为整数?

array( 'baths_full' => (int) $baths_full, ... )

like

喜欢

"VALUES(".((int) $baths_full).")"