(1054, "Unknown column 'nan' in 'field list'") 姜戈 bulk_create

(1054, "Unknown column 'nan' in 'field list'") Django bulk_create

尝试使用 Django 的 bulk_create 方法插入数据时出现以下错误。插入所有数据都不会发生这种情况。

(1054, "Unknown column 'nan' in 'field list'")

我真的不明白 'nan' 是从哪里来的,因为我在这里明确声明了我的所有字段。

我试图一次向数据库中插入大量对象(大约 10,000 个或更多)

我的代码如下,其中observation是另一个对象,TemporaryPhotometry是使用的模型:

    # With thanks to  for the
    # example on how to use the bulk_create function so we don't thrash the DB

    phot_objects = [
        TemporaryPhotometry(
            calibrated_magnitude=function_to_make_calibrated_magnitude(),
            calibrated_error=uncertainty_stars[i],
            magnitude_rms_error=mage_2[i],
            x=x_2[i],
            y=y_2[i],
            alpha_j2000=ra_2[i],
            delta_j2000=de_2[i],
            fwhm_world=fwhm_2[i],
            flags=flag_2[i],
            magnitude=mag_2[i],
            observation=observation,
        )
        for i in range(0, len(num_2))
    ]

    TemporaryPhotometry.objects.bulk_create(phot_objects)

对于解决此问题的任何帮助,我将不胜感激。谢谢。

完整的堆栈跟踪如下:

File "/var/www/image_processing/analysis/utils/calibration.py" in do_calibration
  545.         TemporaryPhotometry.objects.bulk_create(phot_objects)

File "/usr/lib64/python2.7/site-packages/django/db/models/manager.py" in manager_method
  85.                 return getattr(self.get_queryset(), name)(*args, **kwargs)

File "/usr/lib64/python2.7/site-packages/django/db/models/query.py" in bulk_create
  443.                 ids = self._batched_insert(objs_without_pk, fields, batch_size)

File "/usr/lib64/python2.7/site-packages/django/db/models/query.py" in _batched_insert
  1099.                 self._insert(item, fields=fields, using=self.db)

File "/usr/lib64/python2.7/site-packages/django/db/models/query.py" in _insert
  1076.         return query.get_compiler(using=using).execute_sql(return_id)

File "/usr/lib64/python2.7/site-packages/django/db/models/sql/compiler.py" in execute_sql
  1099.                 cursor.execute(sql, params)

File "/usr/lib64/python2.7/site-packages/django/db/backends/utils.py" in execute
  65.                 return self.cursor.execute(sql, params)

File "/usr/lib64/python2.7/site-packages/django/db/utils.py" in __exit__
  94.                 six.reraise(dj_exc_type, dj_exc_value, traceback)

File "/usr/lib64/python2.7/site-packages/django/db/backends/utils.py" in execute
  65.                 return self.cursor.execute(sql, params)

File "/usr/lib64/python2.7/site-packages/django/db/backends/mysql/base.py" in execute
  101.             return self.cursor.execute(query, args)

File "/usr/lib64/python2.7/site-packages/MySQLdb/cursors.py" in execute
  205.             self.errorhandler(self, exc, value)

File "/usr/lib64/python2.7/site-packages/MySQLdb/connections.py" in defaulterrorhandler
  36.     raise errorclass, errorvalue

Exception Type: OperationalError at /process/calibration/624/
Exception Value: (1054, "Unknown column 'nan' in 'field list'")

我能够通过使用以下 if 语句包装所有 NumPy 数组(例如 uncertainty_stars[i] 来检查它们是否为 NaN 来解决此问题。

uncertainty_stars[i] if not numpy.isnan(uncertainty_stars[i]) else None

可能是一条线索:在 C# 中,在计算中除以零时,“NaN”被分配给双精度数。 尝试将此结果插入数据库时​​,出现 returns 此错误。 除法前检查你的分母。