ansanta ansanta - 2 months ago 11
Python Question

django.db.utils.IntegrityError: (1048, "Column 'create_timestamp' cannot be null")

This is the full output after execute script this error is present when run script on python I use django 1.10 python 2.7

File "/usr/bin/snort-sig-parser.py", line 213, in <module>
main()
File "/usr/bin/snort-sig-parser.py", line 205, in main
sig_parse(sig_line, 0,options.branch,options.revision)
File "/usr/bin/snort-sig-parser.py", line 173, in sig_parse
sig_obj.save()
File "/usr/local/lib/python2.7/site-packages/django/db/models/base.py", line 796, in save
force_update=force_update, update_fields=update_fields)
File "/usr/local/lib/python2.7/site-packages/django/db/models/base.py", line 824, in save_base
updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "/usr/local/lib/python2.7/site-packages/django/db/models/base.py", line 908, in _save_table
result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "/usr/local/lib/python2.7/site-packages/django/db/models/base.py", line 947, in _do_insert
using=using, raw=raw)
File "/usr/local/lib/python2.7/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/usr/local/lib/python2.7/site-packages/django/db/models/query.py", line 1043, in _insert
return query.get_compiler(using=using).execute_sql(return_id)
File "/usr/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 1054, in execute_sql
cursor.execute(sql, params)
File "/usr/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/usr/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/usr/local/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 117, in execute
six.reraise(utils.IntegrityError, utils.IntegrityError(*tuple(e.args)), sys.exc_info()[2])
File "/usr/local/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 112, in execute
return self.cursor.execute(query, args)
File "/usr/local/lib/python2.7/site-packages/MySQLdb/cursors.py", line 205, in execute
self.errorhandler(self, exc, value)
File "/usr/local/lib/python2.7/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
raise errorclass, errorvalue
django.db.utils.IntegrityError: (1048, "Column 'create_timestamp' cannot be null")


this script not write on mysql database.

This is part of code on models.

class SignatureUpdateSet(models.Model):
create_timestamp = models.DateTimeField()
branch = models.CharField(max_length=80)
revision = models.IntegerField()
new_signatures = models.ManyToManyField(Signature,related_name='new_signatures')
updated_signatures = models.ManyToManyField(Signature,related_name='updated_signatures')
deleted_signatures = models.ManyToManyField(Signature,related_name='deleted_signatures')
#blogpost_id = models.IntegerField(blank=True, null=True)
blogpost = models.ForeignKey(BlogPost,null=True)

class Meta:
managed = False
db_table = 'content_app_signatureupdateset'

Answer

Add auto_now=True in your create_timestamp field if you want to add Time Stamp automatically, else if you want to add anything other than current time stamp, you can addblank=True and add any custom time stamp if you like before saving.

create_timestamp = DateTimeField(auto_now=True) # To save current datetime

create_timestamp = DateTimeField(blank=True) # To save any custom date time 
Comments