Model object save gives Warning: Data truncated for column

673 views
Skip to first unread message

Chen Xu

unread,
Jan 15, 2013, 1:19:31 AM1/15/13
to django...@googlegroups.com
Hi Everyone,
I am new to Django, I am currently converting my site from php to Django. I have already have my database (all the tables) setup when I wrote in php; now when I convert to Django, I am basically matching each column with the existing column. After I finished doing this, I try to create an object, and doing a <object>.save()  gives me Warning: Data truncated for column '<some column>'. However, I checked, the length of my string did not excess the limit.

Could anyone help?


Thanks




--
⚡ Chen Xu

Bill Freeman

unread,
Jan 15, 2013, 7:32:22 AM1/15/13
to django...@googlegroups.com
Does the field in question have "max_length" specified (assuming that it is a character field or a sub class)?  Does introspecting the database with the database's tools indicate that the corresponding field has that size?  Was the database created using Django's syncdb, or are you attempting to use an existing database.  Which database engine are you using (e,g.; PostgreSQL, MySQL, etc. - different people will have insights into different back ends)?  Is a stack trace printed - if so, provide it for us?  At least, what is the exact text of the error message?

Bill

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Chen Xu

unread,
Jan 15, 2013, 10:06:18 AM1/15/13
to django...@googlegroups.com
I am using my existing mysql db. I found out that if I have an enum and try to insert a value not present in the enum this error will occur.

However, my problem occurs on a TimeField. I created this field using phpMyAdmin, it is a timestamp, and default is current time. Therefore, I did the following in my Django model:

message_create_time = models.TimeField(auto_now_add=True)

When I create an object, and save, it gives me:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/Library/Python/2.7/site-packages/django/db/models/base.py", line 463, in save
    self.save_base(using=using, force_insert=force_insert, force_update=force_update)
  File "/Library/Python/2.7/site-packages/django/db/models/base.py", line 551, in save_base
    result = manager._insert([self], fields=fields, return_id=update_pk, using=using, raw=raw)
  File "/Library/Python/2.7/site-packages/django/db/models/manager.py", line 203, in _insert
    return insert_query(self.model, objs, fields, **kwargs)
  File "/Library/Python/2.7/site-packages/django/db/models/query.py", line 1593, in insert_query
    return query.get_compiler(using=using).execute_sql(return_id)
  File "/Library/Python/2.7/site-packages/django/db/models/sql/compiler.py", line 912, in execute_sql
    cursor.execute(sql, params)
  File "/Library/Python/2.7/site-packages/django/db/backends/util.py", line 40, in execute
    return self.cursor.execute(sql, params)
  File "/Library/Python/2.7/site-packages/django/db/backends/mysql/base.py", line 114, in execute
    return self.cursor.execute(query, args)
  File "/Library/Python/2.7/site-packages/MySQL_python-1.2.4-py2.7-macosx-10.8-intel.egg/MySQLdb/cursors.py", line 203, in execute
    if not self._defer_warnings: self._warning_check()
  File "/Library/Python/2.7/site-packages/MySQL_python-1.2.4-py2.7-macosx-10.8-intel.egg/MySQLdb/cursors.py", line 117, in _warning_check
    warn(w[-1], self.Warning, 3)
Warning: Data truncated for column 'message_create_time' at row 1


However if I change it to :
message_create_time = models.DateTimeField()


It works perfectly.

Any ideas?

Thanks in advance


--
⚡ Chen Xu

Bill Freeman

unread,
Jan 15, 2013, 10:25:36 AM1/15/13
to django...@googlegroups.com
Not being a MySQL expert, I'll still have a guess.  The TimeField just represents the time of day, with no notion of what day, see http://docs.python.org/2.7/library/datetime.html#time-objects .  The term "timestamp", on the other hand, means to me a point in time in the more grand sense, such as the integer number of seconds since the "epoch", and thus embodies date and time.  So if the "timestamp" in your database is something from which you can figure out when something happened, including on which day it happened, then DateTimeField is the correct one to use.

By the way, Django provides a feature to "introspect" an existing database and to compose models for it.  I'll bet that it suggests the DataTimeField.

Bill

Chen Xu

unread,
Jan 15, 2013, 10:38:42 AM1/15/13
to django...@googlegroups.com
Yes, this sound reasonable, and I verified it. You are absolutely correct.

Thanks for your help
Best regards
Reply all
Reply to author
Forward
0 new messages