【问题标题】:Data too long for column in Django on MySQLMySQL 上 Django 中的列数据太长
【发布时间】:2013-01-14 01:35:02
【问题描述】:

我在模型中声明为 TextField 并且在数据库中是 LONGTEXT 的列中收到此错误,并且我尝试插入的值只有 3KB 长。 AFAIK LONGTEXT 理论限制是 2GB,所以我不知道为什么限制或如何绕过它。有什么想法吗?

我得到的具体例外是:

  File "C:\Projetos\iapyx\servers\itese2\views\documents.py", line 46, in create
    self._saveItem(item)

  File "C:\Projetos\iapyx\servers\itese2\views\documents.py", line 59, in _saveItem
    doc.save()

  File "C:\Python27\lib\site-packages\django\db\models\base.py", line 460, in save
    self.save_base(using=using, force_insert=force_insert, force_update=force_update)

  File "C:\Python27\lib\site-packages\django\db\models\base.py", line 553, in save_base
    result = manager._insert(values, return_id=update_pk, using=using)

  File "C:\Python27\lib\site-packages\django\db\models\manager.py", line 195, in _insert
    return insert_query(self.model, values, **kwargs)

  File "C:\Python27\lib\site-packages\django\db\models\query.py", line 1436, in insert_query
    return query.get_compiler(using=using).execute_sql(return_id)

  File "C:\Python27\lib\site-packages\django\db\models\sql\compiler.py", line 791, in execute_sql
    cursor = super(SQLInsertCompiler, self).execute_sql(None)

  File "C:\Python27\lib\site-packages\django\db\models\sql\compiler.py", line 735, in execute_sql
    cursor.execute(sql, params)

  File "C:\Python27\lib\site-packages\django\db\backends\util.py", line 34, in execute
    return self.cursor.execute(sql, params)

  File "C:\Python27\lib\site-packages\django\db\backends\mysql\base.py", line 89, in execute
    return self.cursor.execute(query, args)

  File "C:\Python27\lib\site-packages\MySQLdb\cursors.py", line 174, in execute
    self.errorhandler(self, exc, value)

  File "C:\Python27\lib\site-packages\MySQLdb\connections.py", line 36, in defaulterrorhandler
    raise errorclass, errorvalue 

   DatabaseError: (1406, "Data too long for column 'text' at row 1")

这是导致错误的内容示例:

But is this the case across nations? Does increasing creativity necessarily lead to increased economic inequality? To get at this, we examine the relationship between the GCI and a standard measure of income inequality—the Gini Index. Exhibit 19 shows the relationships between income inequality and the GCI overall as well as each of the 3 T indices that comprise it. While this may come as a surprise for those familiar with the case of the United States, we find that the GCI is in fact systematically associated with lower levels of socio-economic inequality—and hence greater equality—across the nations of the world. Each and every one of the correlations between the GCI and the Gini is negative. The correlation between inequality and the overall GCI is −0.43. The Gini is also quite negatively associated with Technology (− 0.47) and Talent (−0.52) but much less so with Tolerance (− 0.06 and not statistically significant). This last is a bit surprising as one might expect more tolerant societies to be more equal on balance. That said, we believe the overall finding of a negative association between creativity and inequality to be an important one, for it implies that the general trajectory of economic development is associated with lower levels of inequality. Exhibit 20 is a scatter-graph which plots the association between the Gini measure of income inequality and the GCI for the nations of the world. The fit is not especially good and there are lots of countries above and below the line. This suggests that there are two distinctive paths for high creativity countries. On the one hand, there are countries like the United States, the United Kingdom, Singapore, and to a lesser extent, Australia and New Zealand, where high levels of creativity, productivity and economic competitiveness go hand in hand with higher levels of inequality. But there are also a substantial number of countries where high levels of creativity, competitiveness and productivity combine with much lower low levels of inequality. These are largely Scandinavian and Northern

European countries, including Sweden, Denmark, Finland, Norway, the Netherlands, and Germany. Japan is represented as well. Among the less developed nations, we find high levels of inequality in South American nations like Paraguay, Bolivia, Panama, Brazil, Honduras, Ecuador, and Argentina. Of the BRIC nations, China, Russia and particularily Brazil all exhibit much higher levels of inequality than their GCI scores would predict. There appears to be two distinct paths available to high creative development: for every high-creativity, high-inequality nation there is a high-creativity, low-inequality counterpart. This is a likely reflection of these countries’ differing levels of social welfare. Though more systematic study is needed before we can draw any firm conclusions, this finding gives us reason for optimism; at the same time, it suggests that sustainable, long-term prosperity requires a significant investment in education and skill development. This is the topic to which we now turn.

The GCI and Human Development
What is the connection between creativity and human development? To get at this, we explore the association between the GCI and the United Nations’ Human Development Index [27]. “People often value achievements that do not show up at all, or not immediately,

The GCI and inequality (correlations)

Exhibit 19

Talent

Tolerance

Technology

Global Creativity Index

−0.6

−0.4

−0.2

0.0

Correlation with Income Inequality

22 | Creativity and Prosperity: The 2010 Global Creativity Index

注意,出现在某些数字之前的“-”实际上是 +- 特殊字符,以 UTF-8 编码

【问题讨论】:

  • 你能举例说明你插入的文本吗?数据库是如何编码的(例如/ latin1,utf-8),因为这可能会在这里产生影响。
  • 如果你这样做INSERT IGNORE会发生什么?
  • 根据报错,列类型是text而不是longtexttext的最大大小是64KB。很明显,您的有效载荷超过 64KB。
  • @BurhanKhalid 列的类型肯定是 longtext,发生的情况是它的 NAME 是'text'。
  • 当您增加 char 字段的长度限制但忘记运行迁移时,也会出现类似的错误。

标签: python mysql django textfield


【解决方案1】:

确保您的数据实际上是 UTF8 编码的。专栏真的是utf8吗?

如果不是,MySQL 可能会引发此错误。您可以通过执行INSERT IGNORE 语句并查看插入数据库的内容来测试这一点(除其他外)。

您可以在information_schema 中查看table, column and database 是否是这种情况。

【讨论】:

    猜你喜欢
    • 2021-05-09
    • 2016-02-26
    • 2019-02-07
    • 1970-01-01
    • 1970-01-01
    • 2015-10-15
    • 2015-09-24
    • 1970-01-01
    • 2011-10-04
    相关资源
    最近更新 更多