django.db.utils.ProgrammingError:关系“ account_program”不存在第1行:... ated_date”,“ account_program”。“ updated_date”来自“ account_p”

时间:2018-09-27 05:56:57

标签: python django

我正在使用Django 2.0.8和postgres DB。当我开始从事这个项目时,一切都很好。上传到heroku后,我遇到了这个问题。当我在本地数据库上运行代码,并且在heroku DB上运行相同的代码时,我看到此错误。

projectname / settings.py

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []
# Application definition

INSTALLED_APPS = [
    'account.apps.AccountConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.postgres',
    'wagtail.contrib.forms',
    'wagtail.contrib.redirects',
    'wagtail.embeds',
    'wagtail.sites',
    'wagtail.users',
    'wagtail.snippets',
    'wagtail.documents',
    'wagtail.images',
    'wagtail.search',
    'wagtail.admin',
    'wagtail.core',
    'modelcluster',
    'taggit',
    'empoweru',
]

MIDDLEWARE = [
    'whitenoise.middleware.WhiteNoiseMiddleware',
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'wagtail.core.middleware.SiteMiddleware',
    'wagtail.contrib.redirects.middleware.RedirectMiddleware',
]
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'dbanme',
        'USER': 'dbuser',
        'PASSWORD': 'dbpassword',
        'HOST': 'localhost',
        'PORT': '5432',
    }
}

错误

C:\Users\nikes\Desktop\Version - 6\empoweru>python manage.py makemigrations
Traceback (most recent call last):
  File "C:\Users\nikes\Anaconda3\lib\site-packages\django\db\backends\utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
psycopg2.ProgrammingError: relation "account_program" does not exist
LINE 1: ...ated_date", "account_program"."updated_date" FROM "account_p...
                                                             ^


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Users\nikes\Anaconda3\lib\site-packages\django\core\management\__init__.py", line 371, in execute_from_command_line
    utility.execute()
  File "C:\Users\nikes\Anaconda3\lib\site-packages\django\core\management\__init__.py", line 365, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\nikes\Anaconda3\lib\site-packages\django\core\management\base.py", line 288, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\nikes\Anaconda3\lib\site-packages\django\core\management\base.py", line 332, in execute
    self.check()
  File "C:\Users\nikes\Anaconda3\lib\site-packages\django\core\management\base.py", line 364, in check
    include_deployment_checks=include_deployment_checks,
  File "C:\Users\nikes\Anaconda3\lib\site-packages\django\core\management\base.py", line 351, in _run_checks
    return checks.run_checks(**kwargs)
  File "C:\Users\nikes\Anaconda3\lib\site-packages\django\core\checks\registry.py", line 73, in run_checks
    new_errors = check(app_configs=app_configs)
  File "C:\Users\nikes\Anaconda3\lib\site-packages\django\core\checks\urls.py", line 13, in check_url_config
    return check_resolver(resolver)
  File "C:\Users\nikes\Anaconda3\lib\site-packages\django\core\checks\urls.py", line 23, in check_resolver
    return check_method()
  File "C:\Users\nikes\Anaconda3\lib\site-packages\django\urls\resolvers.py", line 399, in check
    for pattern in self.url_patterns:
  File "C:\Users\nikes\Anaconda3\lib\site-packages\django\utils\functional.py", line 36, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Users\nikes\Anaconda3\lib\site-packages\django\urls\resolvers.py", line 540, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "C:\Users\nikes\Anaconda3\lib\site-packages\django\utils\functional.py", line 36, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Users\nikes\Anaconda3\lib\site-packages\django\urls\resolvers.py", line 533, in urlconf_module
    return import_module(self.urlconf_name)
  File "C:\Users\nikes\Anaconda3\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "C:\Users\nikes\Desktop\Version - 6\empoweru\empoweru\urls.py", line 26, in <module>
    path('account/', include('account.urls')),
  File "C:\Users\nikes\Anaconda3\lib\site-packages\django\urls\conf.py", line 34, in include
    urlconf_module = import_module(urlconf_module)
  File "C:\Users\nikes\Anaconda3\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "C:\Users\nikes\Desktop\Version - 6\empoweru\account\urls.py", line 3, in <module>
    from . import views
  File "C:\Users\nikes\Desktop\Version - 6\empoweru\account\views.py", line 5, in <module>
    from .forms import LoginForm, UserEditForm, ProfileEditForm, ProgramForm, UploadFileForm
  File "C:\Users\nikes\Desktop\Version - 6\empoweru\account\forms.py", line 14, in <module>
    class UploadFileForm(forms.Form):
  File "C:\Users\nikes\Desktop\Version - 6\empoweru\account\forms.py", line 18, in UploadFileForm
    for program in Program.objects.all().order_by('program_name'):
  File "C:\Users\nikes\Anaconda3\lib\site-packages\django\db\models\query.py", line 272, in __iter__
    self._fetch_all()
  File "C:\Users\nikes\Anaconda3\lib\site-packages\django\db\models\query.py", line 1179, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "C:\Users\nikes\Anaconda3\lib\site-packages\django\db\models\query.py", line 53, in __iter__
    results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
  File "C:\Users\nikes\Anaconda3\lib\site-packages\django\db\models\sql\compiler.py", line 1068, in execute_sql
    cursor.execute(sql, params)
  File "C:\Users\nikes\Anaconda3\lib\site-packages\django\db\backends\utils.py", line 100, in execute
    return super().execute(sql, params)
  File "C:\Users\nikes\Anaconda3\lib\site-packages\django\db\backends\utils.py", line 68, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "C:\Users\nikes\Anaconda3\lib\site-packages\django\db\backends\utils.py", line 77, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "C:\Users\nikes\Anaconda3\lib\site-packages\django\db\backends\utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Users\nikes\Anaconda3\lib\site-packages\django\db\utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "C:\Users\nikes\Anaconda3\lib\site-packages\django\db\backends\utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: relation "account_program" does not exist
LINE 1: ...ated_date", "account_program"."updated_date" FROM "account_p...


                                                        ^

                                                             ^

account / model.py

from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
from django.conf import settings
from django.utils.translation import ugettext_lazy as _

# Create your models here.

class Program(models.Model):
    #program_id = models.AutoField(null=False, primary_key=True)
    program_name = models.CharField(max_length=20, null=False)
    program_start_date = models.DateField(null=False, blank=False)
    program_end_date = models.DateField(null=False, blank=False)
    created_date = models.DateTimeField(default=timezone.now, blank=True)
    updated_date = models.DateTimeField(auto_now_add=True, null=True)

    def created(self):
        self.created_date = timezone.now()
        self.save()

    def updated(self):
        self.updated_date = timezone.now()
        self.save()

    def __str__(self):
        return str(self.program_name)

2 个答案:

答案 0 :(得分:1)

这是因为您必须将帐户应用程序添加到已安装的应用程序列表中才能创建迁移,只需将该帐户添加到已安装的应用程序中并运行python manage.py makemigrations创建帐户应用程序的迁移,然后通过以下方式将其迁移到数据库正在运行python manage.py migrate

答案 1 :(得分:0)

问题出在我的迁移文件上,删除了迁移文件后,我尝试迁移,但仍然遇到相同的问题,因此我创建了初始迁移文件,并且可以正常工作。

初始(0001_initial.py)文件如下所示。

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
import django.utils.timezone


class Migration(migrations.Migration):

    initial = True

    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
        name='Program',
        fields=[
            ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
            ('program_name', models.CharField(max_length=20)),
            ('program_start_date', models.DateField()),
            ('program_end_date', models.DateField()),
            ('created_date', models.DateTimeField(blank=True, default=django.utils.timezone.now)),
            ('updated_date', models.DateTimeField(auto_now_add=True, null=True)),
        ],
    ),
]