2016-03-02 87 views
2

在我的應用程序中,一切似乎都能順利運行,直到我嘗試從管理站點訪問這些模型時,那就是當我收到我提到的錯誤時,我安裝了Allauth並已做好了所有必要的準備遷移,但無論我做什麼,我都會收到同樣的錯誤。Django編程錯誤關係不存在

任何想法?

下面是型號:

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,), 
    ), 

感謝代碼!

+1

我可以看到完整的回溯? – arcegk

+0

如果您有'managed = False',那麼Django不會爲這些模型創建任何遷移,您將不得不自己創建它們。 – Alasdair

+0

我編輯了這個問題以包含完整的回溯,也正如我所提到的,我已經創建並運行了所有必要的遷移 –

回答

0

我不確定,不管這是否是同一個問題,但我只是通過捕獲這個ProgrammingError異常來修復我自己的類似問題 - 看看here

從我從調查中得知的情況來看,在第一次運行python manage.py migrate之前,似乎從admin.py的配置已加載。這在開發過程中可能會非常棘手,因爲如果您首先創建模型,然後在本地進行遷移並稍後在取決於新模型的admin.py中進行一些更改,則無法注意到此問題。

因此請確保您的admin.py文件不依賴於型號,或者在遷移數據庫之前首先運行

相關問題