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。
爲什麼你'默認= None'和'空= TRUE;爲'CharField'? Django中的典型方法是使用'default ='''和'null = False',這樣可以避免完整性錯誤。看起來好像你錯過了一個允許'tweet_location'列上的空值的遷移,但是你還沒有提供足夠的信息來進一步調試。 – Alasdair
嗨,我剛剛改變爲默認=''和null = False。它仍然給出完整性錯誤。是因爲Charfield嗎? – johnobc
更改值後,您必須創建遷移並運行它。您可能想要同時更改其他有'default = None'的字段。請注意,您不必在字段定義中包含'default ='',null = True',因爲這些是默認值。你仍然需要'blank = True',否則Django管理員不會接受空值。 – Alasdair