警告:在 django 中使用数据库 mysql 时,第 1 行的“用户名”列的数据被截断

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

warning :data truncated for column 'username' at row 1 when using database mysql with django

mysqldatabasedjangowarningsmysql-python

提问by user2323800

I am getting the above mentioned warning

我收到上述警告

data truncated for column 'username' at row 1

第 1 行的“用户名”列的数据被截断

I am getting this for my model "suserprofile" with "username" as my first listed fieldin the model

我为我的模型“suserprofile”获取了这个,其中“用户名”作为我在模型中列出的第一个字段

I checked max_length option which is not the problem for me, if I change the order of my fields in the my model(changed "email_id" field to the first in list of the model),then I am getting the warning replacing 'username' with whatever field I mentioned first(in this case email_id) in the model.

我检查了 max_length 选项,这对我来说不是问题,如果我更改模型中字段的顺序(将“email_id”字段更改为模型列表中的第一个字段),那么我会收到警告替换“用户名”使用模型中我首先提到的任何字段(在本例中为 email_id)。

I am not getting this warning when running syncdb but getting warning only when I am trying to save a new object in my model("suserprofile") in my views.py file using django by actually running the local browser. I am not understanding why is it happenning

运行syncdb时我没有收到此警告,但仅当我尝试通过实际运行本地浏览器使用django在我的views.py文件中的模型(“suserprofile”)中保存新对象时才收到警告。我不明白为什么会这样

edit:

编辑:

my model:

我的模型:

class SUserProfile(models.Model):
    email_id            = models.EmailField(max_length=30)
    username      = models.CharField(max_length=50,blank=True)  
    first_name          = models.CharField(max_length=30)
    last_name           = models.CharField(max_length=30)

my view

我的看法

from django.allauth.models import SocialAccount
def profileview(request):
    user=request.user   
    if user.is_authenticated:
        a=SocialAccount.objects.filter(user=user)   
        try:    
            f=a.get(provider='facebook')
        except:
            f=None
    if f:
        fusername=f.get_provider_account    
        fdata=f.extra_data 
        ffirst_name=fdata['first_name']     
        flast_name=fdata['last_name']
        femail=fdata.get('email')
        try: 
            old_user=SUserProfile.objects.get(email_id=femail)
        except:
            new_user=SUserProfile( 
            username=fusername,
            email_id=femail,
            first_name=ffirst_name,
            last_name=flast_name,)
            new_user.save() 

warning details I'm getting when running the browser:

运行浏览器时收到的警告详细信息:

 Exception Type:    Warning

 Exception Value:   Data truncated for column 'email_id' at row 1

 Exception Location:/usr/lib/python2.7/dist-packages/MySQLdb/cursors.py in _warning_check, line 92

Traceback Switch to copy-and-paste view

usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py in get_response

       response = callback(request, *callback_args, **callback_kwargs)

...

? Local vars
/usr/local/lib/python2.7/dist-packages/django/contrib/auth/decorators.py in _wrapped_view

       return view_func(request, *args, **kwargs)

...

? Local vars
/home/varun/webops/mysite/allauth/account/views.py in profileview

       new_user.save()

...

? Local vars
/usr/local/lib/python2.7/dist-packages/django/db/models/base.py in save

self.save_base(using=using, force_insert=force_insert, force_update=force_update)

...

? Local vars
/usr/local/lib/python2.7/dist-packages/django/db/models/base.py in save_base

result = manager._insert([self], fields=fields, return_id=update_pk, using=using, raw=raw)

...

? Local vars
/usr/local/lib/python2.7/dist-packages/django/db/models/manager.py in _insert

       return insert_query(self.model, objs, fields, **kwargs)

...

? Local vars
/usr/local/lib/python2.7/dist-packages/django/db/models/query.py in insert_query

    return query.get_compiler(using=using).execute_sql(return_id)

...

 ? Local vars
 /usr/local/lib/python2.7/dist-packages/django/db/models/sql/compiler.py in execute_sql

    cursor.execute(sql, params)

...

 ? Local vars
 /usr/local/lib/python2.7/dist-packages/django/db/backends/util.py in execute

    return self.cursor.execute(sql, params)

...

? Local vars
/usr/local/lib/python2.7/dist-packages/django/db/backends/mysql/base.py in execute

    return self.cursor.execute(query, args)

...

? Local vars
/usr/lib/python2.7/dist-packages/MySQLdb/cursors.py in execute

    if not self._defer_warnings: self._warning_check()

...

? Local vars
/usr/lib/python2.7/dist-packages/MySQLdb/cursors.py in _warning_check

     warn(w[-1], self.Warning, 3)

...

? Local vars 

回答by warungman

check the column format and length. i got same error when inserting string data with 20 char in varchar column with 10 in length.

检查列格式和长度。在长度为 10 的 varchar 列中插入带有 20 个字符的字符串数据时,我遇到了同样的错误。

回答by ShanG

syncDB will not overwrite existing table/column definitions. So if you've already checked to make sure the lengths in your file are correct, you may want to check that you have remade your tables - you need to drop and recreate them in order for the updated column definitions to be put in place (I've made this error a couple of times myself)

syncDB 不会覆盖现有的表/列定义。因此,如果您已经检查以确保文件中的长度正确,您可能需要检查您是否重新制作了表格 - 您需要删除并重新创建它们以便将更新的列定义放置到位(我自己也犯过几次这个错误)