1
votes

I am using the mysql2 library with NodeJS. I have the same code and database structure on my local machine, as well as a server. When I upload a photo into the "photos" table, on my local machine, it works fine. When I use the server, I get the following error:

{ Error: Data too long for column 'photo' at row 1 at Packet.asError (/srv/project/server/node_modules/mysql2/lib/packets/packet.js:716:13) at Query.Command.execute (/srv/project/server/node_modules/mysql2/lib/commands/command.js:28:22) at Connection.handlePacket (/srv/project/server/node_modules/mysql2/lib/connection.js:502:28) at PacketParser.onPacket (/srv/project/server/node_modules/mysql2/lib/connection.js:81:16) at PacketParser.executeStart (/srv/project/server/node_modules/mysql2/lib/packet_parser.js:77:14) at Socket. (/srv/project/server/node_modules/mysql2/lib/connection.js:89:29) at Socket.emit (events.js:182:13) at addChunk (_stream_readable.js:283:12) at readableAddChunk (_stream_readable.js:264:11) at Socket.Readable.push (_stream_readable.js:219:10) code: 'ER_DATA_TOO_LONG', errno: 1406, sqlState: '22001', sqlMessage: 'Data too long for column \'photo\' at row 1' }

What is going on?

2

2 Answers

3
votes

I presume you are trying to upload the photos as a blob? To fix this you use a longblob data type for your photos column.

If you are using a VARCHAR field try increasing the length and test your uploads again.

3
votes

You probably have strict mode enabled on your server, and not on your local MySQL instance.

Check it this way:

mysql> SELECT @@sql_mode;
+-------------------------------------------------------------------------------------------------------------------------------------------+
| @@sql_mode                                                                                                                                |
+-------------------------------------------------------------------------------------------------------------------------------------------+
| ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION |
+-------------------------------------------------------------------------------------------------------------------------------------------+

The above is the default sql_mode for MySQL 5.7.8 and later. It includes strict mode.

One of the effects of strict mode is that if you store a string value or numeric value into a column that isn't large enough to take that value, you get an error. Without strict mode, MySQL inserts the row — but it truncates the value.

Here's a demonstration:

mysql> create table t ( v varchar(10) );

mysql> insert into t set v = 'Now is the time for all good men to come to the aid of their country';
ERROR 1406 (22001): Data too long for column 'v' at row 1

mysql> set sql_mode='';

mysql> insert into t set v = 'Now is the time for all good men to come to the aid of their country';
Query OK, 1 row affected, 1 warning (0.00 sec)

mysql> show warnings;
+---------+------+----------------------------------------+
| Level   | Code | Message                                |
+---------+------+----------------------------------------+
| Warning | 1265 | Data truncated for column 'v' at row 1 |
+---------+------+----------------------------------------+

mysql> select * from t;
+------------+
| v          |
+------------+
| Now is the |
+------------+

Strict mode is a good thing. It's better to know that your data won't fit, instead of having it get truncated.

You should develop with a local MySQL instance that has the same version and the same sql_mode as the MySQL instance to which you will eventually deploy in production. Otherwise, you will find your code has errors in production that don't occur in development, and it will be confusing and hard to debug.

You probably can't duplicate every aspect of your production environment, but if you can, make your development environment as close as possible.

To resolve the problem, make sure you choose a data type for your column that can hold the long data you send it.

  • VARBINARY and BLOB holds up to 64KB
  • MEDIUMBLOB holds up to 16MB
  • LONGBLOB holds up to 4GB

Read https://dev.mysql.com/doc/refman/8.0/en/string-type-overview.html for more details.