我正在研究一个Django应用程序,该应用程序从API获取JSON数据并将其存储在PostgreSQL数据库中。但在迁移应用程序时,我遇到了这些错误:
psycopg2.ProgrammingError: column "data" of relation "WorldBank_projects" does not exist
LINE 1: INSERT INTO "WorldBank_projects" ("data", "project_id", "pro...
我应该在代码中更改哪些内容来解决此错误? 这是追溯:
Traceback (most recent call last):
File "/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
psycopg2.ProgrammingError: column "data" of relation "WorldBank_projects" does not exist
LINE 1: INSERT INTO "WorldBank_projects" ("data", "project_id", "pro...
^
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 363, in execute_from_command_line
utility.execute()
File "/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 355, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/python/lib/python3.6/site-packages/django/core/management/base.py", line 283, in run_from_argv
self.execute(*args, **cmd_options)
File "/python/lib/python3.6/site-packages/django/core/management/base.py", line 330, in execute
output = self.handle(*args, **options)
File "/app/aggregator/WorldBank/management/commands/fetch_wb.py", line 53, in handle
project_abstract = data['project_abstract']
File "/python/lib/python3.6/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/python/lib/python3.6/site-packages/django/db/models/query.py", line 394, in create
obj.save(force_insert=True, using=self.db)
File "/python/lib/python3.6/site-packages/django/db/models/base.py", line 807, in save
force_update=force_update, update_fields=update_fields)
File "/python/lib/python3.6/site-packages/django/db/models/base.py", line 837, in save_base
updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "/python/lib/python3.6/site-packages/django/db/models/base.py", line 923, in _save_table
result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "/python/lib/python3.6/site-packages/django/db/models/base.py", line 962, in _do_insert
using=using, raw=raw)
File "/python/lib/python3.6/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/python/lib/python3.6/site-packages/django/db/models/query.py", line 1076, in _insert
return query.get_compiler(using=using).execute_sql(return_id)
File "/python/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1107, in execute_sql
cursor.execute(sql, params)
File "/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 80, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
File "/python/lib/python3.6/site-packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/python/lib/python3.6/site-packages/django/utils/six.py", line 685, in reraise
raise value.with_traceback(tb)
File "/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: column "data" of relation "WorldBank_projects" does not exist
LINE 1: INSERT INTO "WorldBank_projects" ("data", "project_id", "pro...
如何解决这个问题?
这是models.py的代码:
from django.db import models
from django.contrib.postgres.fields import JSONField
class Projects(models.Model):
data = JSONField(null=False)
project_id=models.CharField(max_length=100)
project_name=models.CharField(max_length=100)
status=models.CharField(max_length=10)
country=models.CharField(max_length=100)
locations=JSONField()
mjtheme=models.CharField(max_length=50)
project_docs=JSONField()
source=models.CharField(max_length=10)
mjtheme_namecode=models.CharField(max_length=10)
docty=models.TextField()
countryname=models.CharField(max_length=100)
countrycode=models.CharField(max_length=10)
themecode=models.CharField(max_length=100)
theme_namecode=models.CharField(max_length=100)
project_url=models.TextField()
totalcommamt=models.CharField(max_length=100)
mjthemecode=models.CharField(max_length=100)
sector1=models.CharField(max_length=10)
theme1=models.CharField(max_length=10)
theme2=models.CharField(max_length=10)
theme3=models.CharField(max_length=10)
projectinfo=models.TextField()
country_namecode=models.CharField(max_length=5)
p2a_updated_date=models.CharField(max_length=100)
p2a_flag=models.CharField(max_length=5)
project_abstract=JSONField()
这是fetch.py文件的代码,它存储在/management/commands/fetch.py下:
import requests
from django.core.management.base import BaseCommand
from aggregator.WorldBank.models import Projects
class Command(BaseCommand):
def handle(self, **options):
response = requests.get("https://search.worldbank.org/api/v2/projects?format=json&countryshortname_exact=India&source=IBRD&kw=N&rows=7")
data = response.json()
projects = data['projects']
for project in projects:
print(projects[project])
print("\n\n")
data = projects[project]
Projects.objects.create(
project_id = data['id'],
project_name = data['project_name'],
status = data['status'],
country = data['countryshortname'],
locations = data['locations'],
mjtheme = data['mjtheme'],
project_docs = data['projectdocs'],
source = data['source'],
mjtheme_namecode = data['mjtheme_namecode'],
docty = data['docty'],
countryname = data['countryname'],
countrycode = data['countrycode'],
themecode = data['themecode'],
theme_namecode = data['theme_namecode'],
project_url = data['url'],
totalcommamt = data['totalcommamt'],
mjthemecode = data['mjthemecode'],
sector1 = data['sector1'],
theme1 = data['theme1'],
theme2 = data['theme2'],
theme3 = data['theme3'],
projectinfo = data['projectinfo'],
country_namecode = ['country_namecode'],
p2a_updated_date = data['p2a_updated_date'],
p2a_flag = data['p2a_flag'],
project_abstract = data['project_abstract']
)