2017-04-12 70 views
0

Tricky NULL约束错误。 以前工作时,我只是从迁移文件夹中删除py和pyc文件。现在,当我进行迁移时,它不再有效。Tricky NOT NULL错误django utils完整性错误

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

为什么你'默认= None'和'空= TRUE;为'CharField'? Django中的典型方法是使用'default ='''和'null = False',这样可以避免完整性错误。看起来好像你错过了一个允许'tweet_location'列上的空值的迁移,但是你还没有提供足够的信息来进一步调试。 – Alasdair

+0

嗨,我刚刚改变为默认=''和null = False。它仍然给出完整性错误。是因为Charfield吗? – johnobc

+1

更改值后,您必须创建迁移并运行它。您可能想要同时更改其他有'default = None'的字段。请注意,您不必在字段定义中包含'default ='',null = True',因为这些是默认值。你仍然需要'blank = True',否则Django管理员不会接受空值。 – Alasdair

回答

1

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

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

只是一个简单的问题,因为我删除了数据库我需要创建一个新的超级用户? – johnobc

+0

是的 - 任何旧用户的详细信息都在您删除的数据库中。 – Alasdair

+0

非常感谢你:D – johnobc