1

UPDATE sometable SET total_cost=cost*sec/60.0DjangoORMのようにクエリを実行する必要があります。クエリは、このチケットで説明されている問題と同様の警告を生成します。

new_records = Service.objects.filter(upd='U')

print new_records.update(
    total_cost=F('cost')*F('billsec')/Decimal('60.0')
)

cost&&secフィールドはDECIMAL(10,4)タイプです)。結果:

Traceback (most recent call last):
File "./test.py", line 15, in <module>
    total_cost=F('cost')*F('billsec')/Decimal('60.0')
File "/usr/lib/python2.7/dist-packages/django/db/models/query.py", line 533, in update
    rows = query.get_compiler(self.db).execute_sql(None)
File "/usr/lib/python2.7/dist-packages/django/db/models/sql/compiler.py", line 986, in execute_sql
    cursor = super(SQLUpdateCompiler, self).execute_sql(result_type)
File "/usr/lib/python2.7/dist-packages/django/db/models/sql/compiler.py", line 818, in execute_sql
    cursor.execute(sql, params)
File "/usr/lib/python2.7/dist-packages/django/db/backends/util.py", line 40, in execute
    return self.cursor.execute(sql, params)
File "/usr/lib/python2.7/dist-packages/django/db/backends/mysql/base.py", line 114, in execute
    return self.cursor.execute(query, args)
File "/usr/lib/python2.7/dist-packages/MySQLdb/cursors.py", line 176, in execute
    if not self._defer_warnings: self._warning_check()
File "/usr/lib/python2.7/dist-packages/MySQLdb/cursors.py", line 92, in _warning_check
    warn(w[-1], self.Warning, 3)
_mysql_exceptions.Warning: Data truncated for column 'total_cost' at row 1

Decimal()フィルタクエリから削除すると、正常に機能します。

4

1 に答える 1