未加星标

Data Truncated Errors

字体大小 | |
[数据库(mysql) 所属分类 数据库(mysql) | 发布者 店小二04 | 时间 2016 | 作者 红领巾 ] 0人收藏点击收藏

I recently ran into some of the Data truncated for column ... errors in my django apps. After a little digging, I've discovered that my particular problem lie in the structure of the underlying mysql tables. Particularly with varchar columns.

I have a model that contains a FileField:

class MyModel(models.Model):
file = models.FileField(upload_to="files/%Y/%m/%d")

Note that the MySQL table generated by this model will look something like the following:

+-------------+--------------+------+-----+---------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------------------+----------------+
| id | int(11) | | PRI | NULL | auto_increment |
| file | varchar(100) | | | | |
+-------------+--------------+------+-----+---------------------+----------------+

See the varchar(100) ! If you're users are uploading files with long names (mine are!), this may not be enough! Note that my model also has an upload_to that includes a year, month, and day. That's already 17 characters...

So when someone tries to upload a file named:

Who_Said_One_Hundred_Characters_Would_Ever_Be_Enough_For_A_REALLY_LONG_Filename_draft_one_.docx

It will get pre-pended with '/files/2009/05/06/', and what gets stored in your table?

files/2009/05/06/Who_Said_One_Hundred_Characters_Would_Ever_Be_Enough_For_A_REALLY_LONG_Filename_draft_one_.docx

That's 112 characters... and it won't work!

The solution?

You could alter your table so that the file column is a varchar(255)... which may help.

alter table myapp_mymodel modify file varchar(255);

OR, use shorter filenames!

Good Luck!

本文数据库(mysql)相关术语:navicat for mysql mysql workbench mysql数据库 mysql 存储过程 mysql安装图解 mysql教程 mysql 管理工具

主题: SQLMySQL
分页:12
转载请注明
本文标题:Data Truncated Errors
本站链接:http://www.codesec.net/view/480206.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 数据库(mysql) | 评论(0) | 阅读(42)