psycopg2.ProgrammingError:关系“WorldBank_projects”的“data”列不存在

2024-09-28 03:20:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在开发一个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...

我应该在代码中做些什么来解决这个错误? 回溯如下:

^{pr2}$

如何解决这个问题?在

这是我的代码模型.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']

                )

Tags: fromimportprojectiddatamodelscountrylength

热门问题