【问题标题】:Django programming error relation does not existdjango编程错误关系不存在
【发布时间】:2016-03-02 17:57:51
【问题描述】:

在我尝试从管理站点访问这些模型之前,我的应用程序中的一切似乎都运行顺利,那时我收到了我提到的错误,我安装了 Allauth 并进行了所有必要的迁移,但我一直得到相同的结果不管我做什么都会出错。

有什么想法吗?

以下是模型:

class Cuentas(models.Model):
    idCuenta = models.AutoField(primary_key=True, null=False, max_length=15)
    user = models.OneToOneField(User)
    Saldo = models.CharField(max_length=50)

    class Meta:
        managed = False
        db_table = 'cuentas'

class Depositos(models.Model):
    idDeposito = models.AutoField(primary_key=True, null=False, max_length=15)
    idCuenta = models.ForeignKey(Cuentas)
    Tipo = models.CharField(max_length=200)
    Monto = models.CharField(max_length=50)
    Fecha = models.DateField()

    class Meta:
        managed = False
        db_table = 'deposits'

追溯:

File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in get_response
  111.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Library/Python/2.7/site-packages/django/contrib/admin/options.py" in wrapper
  583.                 return self.admin_site.admin_view(view)(*args, **kwargs)
File "/Library/Python/2.7/site-packages/django/utils/decorators.py" in _wrapped_view
  105.                     response = view_func(request, *args, **kwargs)
File "/Library/Python/2.7/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
  52.         response = view_func(request, *args, **kwargs)
File "/Library/Python/2.7/site-packages/django/contrib/admin/sites.py" in inner
  206.             return view(request, *args, **kwargs)
File "/Library/Python/2.7/site-packages/django/utils/decorators.py" in _wrapper
  29.             return bound_func(*args, **kwargs)
File "/Library/Python/2.7/site-packages/django/utils/decorators.py" in _wrapped_view
  105.                     response = view_func(request, *args, **kwargs)
File "/Library/Python/2.7/site-packages/django/utils/decorators.py" in bound_func
  25.                 return func.__get__(self, type(self))(*args2, **kwargs2)
File "/Library/Python/2.7/site-packages/django/contrib/admin/options.py" in changelist_view
  1485.                 self.list_max_show_all, self.list_editable, self)
File "/Library/Python/2.7/site-packages/django/contrib/admin/views/main.py" in __init__
  110.         self.get_results(request)
File "/Library/Python/2.7/site-packages/django/contrib/admin/views/main.py" in get_results
  219.         result_count = paginator.count
File "/Library/Python/2.7/site-packages/django/core/paginator.py" in _get_count
  72.                 self._count = self.object_list.count()
File "/Library/Python/2.7/site-packages/django/db/models/query.py" in count
  338.         return self.query.get_count(using=self.db)
File "/Library/Python/2.7/site-packages/django/db/models/sql/query.py" in get_count
  436.         number = obj.get_aggregation(using=using)[None]
File "/Library/Python/2.7/site-packages/django/db/models/sql/query.py" in get_aggregation
  402.         result = query.get_compiler(using).execute_sql(SINGLE)
File "/Library/Python/2.7/site-packages/django/db/models/sql/compiler.py" in execute_sql
  786.             cursor.execute(sql, params)
File "/Library/Python/2.7/site-packages/django/db/backends/utils.py" in execute
  81.             return super(CursorDebugWrapper, self).execute(sql, params)
File "/Library/Python/2.7/site-packages/django/db/backends/utils.py" in execute
  65.                 return self.cursor.execute(sql, params)
File "/Library/Python/2.7/site-packages/django/db/utils.py" in __exit__
  94.                 six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/Library/Python/2.7/site-packages/django/db/backends/utils.py" in execute
  65.                 return self.cursor.execute(sql, params)

Exception Type: ProgrammingError at /admin/DraftFantasy/cuentas/
Exception Value: relation "cuentas" does not exist
LINE 1: SELECT COUNT(*) FROM "cuentas"

这是创建两个模型的初始迁移中的代码

class Migration(migrations.Migration):

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

operations = [
    migrations.CreateModel(
        name='Cuentas',
        fields=[
            ('idCuenta', models.AutoField(max_length=15, serialize=False, primary_key=True)),
            ('Saldo', models.CharField(max_length=50)),
            ('user', models.OneToOneField(to=settings.AUTH_USER_MODEL)),
        ],
        options={
        },
        bases=(models.Model,),
    ),
    migrations.CreateModel(
        name='Depositos',
        fields=[
            ('idDeposito', models.AutoField(max_length=15, serialize=False, primary_key=True)),
            ('Tipo', models.CharField(max_length=200)),
            ('Monto', models.CharField(max_length=50)),
            ('Fecha', models.DateField()),
            ('idCuenta', models.ForeignKey(to='DraftFantasy.Cuentas')),
        ],
        options={
        },
        bases=(models.Model,),
    ),

谢谢!

【问题讨论】:

  • 我能看到完整的回溯吗?
  • 如果你有managed = False,那么 Django 不会为这些模型创建任何迁移,你必须自己创建它们。
  • 我已经编辑了问题以包含完整的回溯,正如我所提到的,我已经创建并运行了所有必要的迁移
  • 你确定你有一个创建模型的迁移者吗?正如我之前所说,Django 不会为具有managed=False 的模型创建迁移。请显示您认为创建模型的迁移,以及./manage.py showmigrations 的输出。
  • 我已经添加了初始迁移中创建模型的代码

标签: django django-models


【解决方案1】:

我不确定这是否是同一个问题,但我只是通过捕获这个ProgrammingError 异常来解决我自己的类似问题 - 看看here

根据我从调查中了解到的情况,似乎 admin.py 的配置甚至在您第一次运行 python manage.py migrate 之前就已加载。这在开发过程中可能会很棘手,因为如果您首先创建一个模型,将其迁移到本地,然后在 admin.py 中开发一些依赖于新模型的更改,您可能不会注意到这个问题。

因此,请确保您的 admin.py 文件不依赖于模型,或者可以在迁移数据库之前先运行

【讨论】:

    猜你喜欢
    • 2021-10-26
    • 2015-06-22
    • 2016-07-05
    • 2014-11-07
    • 2013-12-21
    • 2018-03-05
    • 2019-12-10
    • 2020-11-23
    • 2019-08-05
    相关资源
    最近更新 更多