关系不存在 - Django & Postgres
Posted
技术标签:
【中文标题】关系不存在 - Django & Postgres【英文标题】:Relation does not exist - Django & Postgres 【发布时间】:2019-08-02 17:15:26 【问题描述】:我在现有模型中添加了一些表,但是当我将项目推送到 heroku 时出现错误:
这发生在我在这个特定应用上添加的任何模型
/admin/dashboard/addressbook/ 处的编程错误 关系“dashboard_addressbook”不存在 第 1 行:从“dashboard_adressbook”中选择 COUNT(*) 作为“__count”
我的models.py
class AdressBook(models.Model):
author = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name="Nom de l'utilisateur")
text = models.CharField(max_length=200,verbose_name="Description")
number = models.CharField(max_length=20,verbose_name="Numéro de téléphone")
def __str__(self):
return self.text
def get_absolute_url(self):
return reverse('dashboard-home')
class Dashboard(models.Model):
author = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name="Nom de l'utilisateur")
content = RichTextField(max_length=500,verbose_name="Description")
text = models.TextField(verbose_name="Description supplémentaire",null=True, blank=True)
date_posted = models.DateTimeField(default=timezone.now)
def __str__(self):
return self.content
def get_absolute_url(self):
return reverse('dashboard-home')
class Links(models.Model):
author = models.ForeignKey(User, on_delete=models.CASCADE,verbose_name="Nom de l'utilisateur")
content = models.URLField(max_length=500, verbose_name="URL")
name = models.CharField(max_length=500, verbose_name="Description")
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse('dashboard-home')
class Todo(models.Model):
author = models.ForeignKey(User, on_delete=models.CASCADE,verbose_name="Nom de l'utilisateur")
text = models.CharField(max_length=150, verbose_name="Nom de la Todo")
content = RichTextField(verbose_name="Description supplémentaire",null=True, blank=True)
date_posted = models.DateTimeField(default=timezone.now)
complete = models.BooleanField(default=False, verbose_name="Statut de la Todo")
urgence = models.BooleanField(default=False,verbose_name="Tâche urgente")
def __str__(self):
return self.text
def get_absolute_url(self):
return reverse('dashboard-home')
class Phone(models.Model):
author = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name="Nom de l'utilisateur")
text = models.CharField(max_length=200,verbose_name="Description")
number = models.CharField(max_length=20,verbose_name="Numéro de téléphone")
def __str__(self):
return self.text
def get_absolute_url(self):
return reverse('dashboard-home')
追溯:
Django Version: 2.1.7
Python Version: 3.6.7
Installed Applications:
['blog.apps.BlogConfig',
'dashboard.apps.DashboardConfig',
'property.apps.PropertyConfig',
'users.apps.UsersConfig',
'crispy_forms',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'storages',
'cloudinary',
'ckeditor']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
'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']
Traceback:
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py" in _execute
85. return self.cursor.execute(sql, params)
The above exception (relation "dashboard_adressbook" does not exist
LINE 1: SELECT COUNT(*) AS "__count" FROM "dashboard_adressbook"
^
) was the direct cause of the following exception:
File "/app/.heroku/python/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
34. response = get_response(request)
File "/app/.heroku/python/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
126. response = self.process_exception_by_middleware(e, request)
File "/app/.heroku/python/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
124. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/admin/options.py" in wrapper
604. return self.admin_site.admin_view(view)(*args, **kwargs)
File "/app/.heroku/python/lib/python3.6/site-packages/django/utils/decorators.py" in _wrapped_view
142. response = view_func(request, *args, **kwargs)
File "/app/.heroku/python/lib/python3.6/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
44. response = view_func(request, *args, **kwargs)
File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/admin/sites.py" in inner
223. return view(request, *args, **kwargs)
File "/app/.heroku/python/lib/python3.6/site-packages/django/utils/decorators.py" in _wrapper
45. return bound_method(*args, **kwargs)
File "/app/.heroku/python/lib/python3.6/site-packages/django/utils/decorators.py" in _wrapped_view
142. response = view_func(request, *args, **kwargs)
File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/admin/options.py" in changelist_view
1675. cl = self.get_changelist_instance(request)
File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/admin/options.py" in get_changelist_instance
742. sortable_by,
File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/admin/views/main.py" in __init__
81. self.get_results(request)
File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/admin/views/main.py" in get_results
209. result_count = paginator.count
File "/app/.heroku/python/lib/python3.6/site-packages/django/utils/functional.py" in __get__
37. res = instance.__dict__[self.name] = self.func(instance)
File "/app/.heroku/python/lib/python3.6/site-packages/django/core/paginator.py" in count
87. return self.object_list.count()
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/query.py" in count
383. return self.query.get_count(using=self.db)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/sql/query.py" in get_count
498. number = obj.get_aggregation(using, ['__count'])['__count']
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/sql/query.py" in get_aggregation
483. result = compiler.execute_sql(SINGLE)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/sql/compiler.py" in execute_sql
1065. cursor.execute(sql, params)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py" in execute
100. return super().execute(sql, params)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py" in execute
68. return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py" in _execute_with_wrappers
77. return executor(sql, params, many, context)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py" in _execute
85. return self.cursor.execute(sql, params)
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/utils.py" in __exit__
89. raise dj_exc_value.with_traceback(traceback) from exc_value
File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py" in _execute
85. return self.cursor.execute(sql, params)
Exception Type: ProgrammingError at /admin/dashboard/adressbook/
Exception Value: relation "dashboard_adressbook" does not exist
LINE 1: SELECT COUNT(*) AS "__count" FROM "dashboard_adressbook"
^
我在本地机器上完成了所有迁移,但问题仍然没有消失。
我也在 Heroku 上进行了迁移。
谢谢
【问题讨论】:
但是您是否在 Heroku 上运行了迁移? 是的!我一遍又一遍地做,没有任何改变 请准确说明你做了什么。 这样的问题被称为“题外话:为什么这段代码不起作用?”在 SO 上。 @Chris 你好:正如我在下面所说,我开始干净,我意识到这是一个巨大的错误,并且会相应地编辑我的答案,所以没有人会这样做,因为没有解决方案跨度> 【参考方案1】:警告: 请不要犯同样的错误,不要在本地机器和生产中使用不同的引擎,一旦遇到问题,就无法修复
所以 4 天后,我通过从数据库中删除数据解决了这个问题。
我开始 clean 并确保在推送到 heroku 之前 迁移,我也一直在使用 same engine (postgres)在我的本地机器和 Heroku 上,到目前为止我还没有遇到任何错误。
所以要解决这个问题就得从头开始清理,唯一的办法就是删除数据库,如果你有有价值的数据的话就不方便了。
【讨论】:
【参考方案2】:这对我有用
-
heroku 运行 bash
python manage.py makemigrations
python manage.py 迁移
【讨论】:
【参考方案3】:在从 Heroku 仪表板重置我的数据库后,登陆从我的数据库获取数据的模板时,我遇到了同样的错误。
我通过这样做解决了它:
-
python manage.py makemigrations
python manage.py 迁移
git 添加。
git push -m "上传空数据库"
git push heroku master
【讨论】:
嗨!是的,要解决这个问题,我们必须从清理开始,但话又说回来,唯一的解决方案就是删除数据库,如果你有有价值的数据,这很不方便以上是关于关系不存在 - Django & Postgres的主要内容,如果未能解决你的问题,请参考以下文章
Django blog.models.Post.DoesNotExist:帖子匹配查询不存在
Django REST框架:POST请求:仅在数据不存在时如何保存数据