johnobc
johnobc

Reputation: 581

Tricky NOT NULL error django utils Integrity error

Tricky NULL Constraint error. Previously working when i just delete py and pyc files from migrations folder. now when i make migrations it is no longer working.

 File "/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
 File "/usr/local/lib/python2.7/dist-packages/django/db/backends/sqlite3/base.py", line 337, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.IntegrityError: NOT NULL constraint failed: Htweets2_htweets2.tweet_location

Here is my models.py

class Htweets2(models.Model):
    tweet_id = models.BigIntegerField(blank=True)
    tweet_timestamp = models.CharField(blank=True, max_length=200)
    tweet_screenname = models.CharField(blank=True, max_length=200)
    tweet_favour_count = models.CharField(blank=True, max_length=200)
    tweet_recount = models.BigIntegerField(blank=True)
    tweet_location = models.CharField(blank=True, null=True, max_length=200)
    tweet_text = models.TextField(blank=True)
    tweet_media_entities = models.URLField(blank=True)

from my views.py, i am trying to save to a table that doesnt exists

e = json.load(json_data)
json_data.close()

tweets = Htweets2()
for x in e:
    tweets.tweet_timestamp = x['timestamp_ms']
    tweets.tweet_id = x['id']
    tweets.tweet_screename = x['user']['screen_name']
    tweets.tweet_recount = x['retweet_count']
    tweets.tweet_favour_count = x['favorite_count']
    tweets.tweet_text = x['text']
    tweets.tweet_location = x['user']['location']
    tweets.tweet_media_entities = x['source']

tweets.save()

But surely when i make migrate, django should create a new table wouldnt it?

    Traceback (most recent call last):
    File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
    File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
utility.execute()
    File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 359, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
   File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 294, in run_from_argv
self.execute(*args, **cmd_options)
   File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 342, in execute
self.check()
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 374, in check
include_deployment_checks=include_deployment_checks,
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 361, in _run_checks
return checks.run_checks(**kwargs)
  File "/usr/local/lib/python2.7/dist-packages/django/core/checks/registry.py", line 81, in run_checks
new_errors = check(app_configs=app_configs)
  File "/usr/local/lib/python2.7/dist-packages/django/core/checks/urls.py", line 14, in check_url_config
return check_resolver(resolver)
  File "/usr/local/lib/python2.7/dist-packages/django/core/checks/urls.py", line 24, in check_resolver
for pattern in resolver.url_patterns:
  File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
 File "/usr/local/lib/python2.7/dist-packages/django/urls/resolvers.py", line 313, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
 File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
 File "/usr/local/lib/python2.7/dist-packages/django/urls/resolvers.py", line 306, in urlconf_module
return import_module(self.urlconf_name)
 File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
 File "/home/hermes/Documents/Htweetprod2/Htweetprod2/urls.py", line 4, in <module>
from Htweets2 import views
 File "/home/hermes/Documents/Htweetprod2/Htweets2/views.py", line 24, in <module>
tweets.save()
 File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 796, in save
force_update=force_update, update_fields=update_fields)
 File "/usr/local/lib/python2.7/dist-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/dist-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/dist-packages/django/db/models/base.py", line 947, in _do_insert
using=using, raw=raw)
 File "/usr/local/lib/python2.7/dist-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/dist-packages/django/db/models/query.py", line 1045, in _insert
return query.get_compiler(using=using).execute_sql(return_id)
 File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/compiler.py", line 1054, in execute_sql
cursor.execute(sql, params)
 File "/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
 File "/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
 File "/usr/local/lib/python2.7/dist-packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
 File "/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
 File "/usr/local/lib/python2.7/dist-packages/django/db/backends/sqlite3/base.py", line 337, in execute
  return Database.Cursor.execute(self, query, params)
 django.db.utils.OperationalError: no such table: Htweets2_htweets2

I am certain that simply deleting pyc and py files from migrations folder was working, but after i have done it several times, it does not longer make me do makemigrations.

Upvotes: 1

Views: 83

Answers (1)

Alasdair
Alasdair

Reputation: 308949

Your code is loading the json and saving the tweets every time the module is imported. This causes an error when the database is first created, because migrate has not created the database tables yet. You should move this code into a function, for example:

def load_tweets():

    e = json.load(json_data)
    json_data.close()

    tweets = Htweets2()
    for x in e:
        tweets.tweet_timestamp = x['timestamp_ms']
        ...
        tweets.save()

Upvotes: 1

Related Questions