【问题标题】:How to permanetly fix a mysql error: Data truncated for column with an alter table statement?如何永久修复mysql错误:使用alter table语句截断列的数据?
【发布时间】:2013-04-19 20:44:00
【问题描述】:

我有一个字段

logo=models.ImageField(upload_to="restaurant_detail/restaurant_detail", help_text = "upload your restaurant logo")

这会返回一个错误,Data truncated for column 'logo' at row 1

所以我继续用这个语句来修复它

alter table my_table modify logo varchar(500) 

(我增加了字符。)

这可行,但每次我重置数据库时,我都必须再次执行此操作并且当我尝试执行上面的 alter 语句并插入数据时,它仍然失败并出现 Data truncated for column 'logo' at row 1 错误。 p>

如何永久解决此问题,而不必一直更改表格?

【问题讨论】:

    标签: mysql django


    【解决方案1】:

    将您的字段定义更改为:

    logo = models.ImageField(max_length=500, upload_to="restaurant_detail/restaurant_detail", help_text="upload your restaurant logo")
    

    我添加了 max_length kwarg,它告诉 Django 该字段可以存储多少个字符,并将导致正确创建数据库字段。

    【讨论】:

    • 在这之后,是否需要重新生成数据库?
    • 是的,您需要使用迁移或以其他方式更新数据库以匹配模型。
    猜你喜欢
    • 1970-01-01
    • 2012-11-24
    • 1970-01-01
    • 1970-01-01
    • 2010-09-09
    • 2015-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多