Model的一列数据,default=None与null=True,如果没有null=True,则不允许该数据为None,相当于default=None不起作用

# emos工单,投诉的内容
class Record(models.Model):
    jobid = models.CharField(max_length=50, default=None)
    customerserviceid = models.BigIntegerField()
 city = models.ForeignKey(City, on_delete=models.CASCADE, default=None, null=True)



Record.objects.create(customerserviceid=123, city=None)

可以看到报错最后一行写在不允许jobid为空,虽然有default=None,设置了默认值为空,但是不起作用。


Traceback (most recent call last):
  File "C:\Users\Administrator\PycharmProjects\first_django\venv\lib\site-packages\django\db\backends\mysql\base.py", line 71, in execute
    return self.cursor.execute(query, args)
  File "C:\Users\Administrator\PycharmProjects\first_django\venv\lib\site-packages\MySQLdb\cursors.py", line 250, in execute
    self.errorhandler(self, exc, value)
  File "C:\Users\Administrator\PycharmProjects\first_django\venv\lib\site-packages\MySQLdb\connections.py", line 50, in defaulterrorhandler
    raise errorvalue
  File "C:\Users\Administrator\PycharmProjects\first_django\venv\lib\site-packages\MySQLdb\cursors.py", line 247, in execute
    res = self._query(query)
  File "C:\Users\Administrator\PycharmProjects\first_django\venv\lib\site-packages\MySQLdb\cursors.py", line 411, in _query
    rowcount = self._do_query(q)
  File "C:\Users\Administrator\PycharmProjects\first_django\venv\lib\site-packages\MySQLdb\cursors.py", line 374, in _do_query
    db.query(q)
  File "C:\Users\Administrator\PycharmProjects\first_django\venv\lib\site-packages\MySQLdb\connections.py", line 277, in query
    _mysql.connection.query(self, query)
_mysql_exceptions.OperationalError: (1048, "Column 'jobid' cannot be null")
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "", line 1, in
  File "C:\Users\Administrator\PycharmProjects\first_django\venv\lib\site-packages\django\db\models\manager.py", line 82, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "C:\Users\Administrator\PycharmProjects\first_django\venv\lib\site-packages\django\db\models\query.py", line 417, in create
    obj.save(force_insert=True, using=self.db)
  File "C:\Users\Administrator\PycharmProjects\first_django\venv\lib\site-packages\django\db\models\base.py", line 729, in save
    force_update=force_update, update_fields=update_fields)
  File "C:\Users\Administrator\PycharmProjects\first_django\venv\lib\site-packages\django\db\models\base.py", line 759, in save_base
    updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
  File "C:\Users\Administrator\PycharmProjects\first_django\venv\lib\site-packages\django\db\models\base.py", line 842, in _save_table
    result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
  File "C:\Users\Administrator\PycharmProjects\first_django\venv\lib\site-packages\django\db\models\base.py", line 880, in _do_insert
    using=using, raw=raw)
  File "C:\Users\Administrator\PycharmProjects\first_django\venv\lib\site-packages\django\db\models\manager.py", line 82, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "C:\Users\Administrator\PycharmProjects\first_django\venv\lib\site-packages\django\db\models\query.py", line 1125, in _insert
    return query.get_compiler(using=using).execute_sql(return_id)
  File "C:\Users\Administrator\PycharmProjects\first_django\venv\lib\site-packages\django\db\models\sql\compiler.py", line 1281, in execute_sql
    cursor.execute(sql, params)
  File "C:\Users\Administrator\PycharmProjects\first_django\venv\lib\site-packages\django\db\backends\utils.py", line 100, in execute
    return super().execute(sql, params)
  File "C:\Users\Administrator\PycharmProjects\first_django\venv\lib\site-packages\django\db\backends\utils.py", line 68, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "C:\Users\Administrator\PycharmProjects\first_django\venv\lib\site-packages\django\db\backends\utils.py", line 77, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "C:\Users\Administrator\PycharmProjects\first_django\venv\lib\site-packages\django\db\backends\utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Users\Administrator\PycharmProjects\first_django\venv\lib\site-packages\django\db\backends\mysql\base.py", line 76, in execute
    raise utils.IntegrityError(*tuple(e.args))
django.db.utils.IntegrityError: (1048, "Column 'jobid' cannot be null")

你可能感兴趣的:(Model的一列数据,default=None与null=True,如果没有null=True,则不允许该数据为None,相当于default=None不起作用)