棘手的NOT NULL错误django utils完整性错误

时间:2017-04-12 09:24:50

标签: python django django-models

Tricky NULL Constraint错误。 以前工作时我只是从迁移文件夹中删除py和pyc文件。现在,当我进行迁移时,它已不再有效。

 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

这是我的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)

从我的views.py,我试图保存到一个不存在的表

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()

但是当我进行迁移时,django应该创建一个新的表吗?

    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

我确信只是从迁移文件夹中删除pyc和py文件就可以了,但是在我完成它几次之后,它不再能让我做makemigrations。

1 个答案:

答案 0 :(得分:1)

您的代码正在加载json并在导入模块的每个时间保存推文。这在首次创建数据库时会导致错误,因为migrate尚未创建数据库表。您应该将此代码移动到一个函数中,例如:

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()