2011-12-07 46 views
7

我現在不是爲什麼,但是從一天到另一天,當我嘗試運行測試時,y開始出現問題。 我使用Django 1.1(客戶要求),當我運行測試蒙山:django:sqlite3.OperationalError:沒有這樣的表

python manage.py test --setting=settingsTest 

扔:

Traceback (most recent call last): 
    File "manage.py", line 11, in <module> 
    execute_manager(settings) 
    File "/usr/lib/pymodules/python2.6/django/core/management/__init__.py", line 362, in execute_manager 
    ... 
    File "/usr/lib/pymodules/python2.6/django/db/backends/sqlite3/base.py", line 193, in execute 
    return Database.Cursor.execute(self, query, params) 
sqlite3.OperationalError: no such table: programas_act_actividadesprograma 

,但我有settingsTest.py

INSTALLED_APPS = (
    'django.contrib.auth', 
    ... 

    'site.programas_act', 
    ... 
) 

和in /var/www/site/programas_act/models.py

class ActividadesPrograma(models.Model): 
    ... 

我刪除了settingsTest.DATABASE_NAME命名並運行該文件:

python manage.py syncdb --setting=settingsTest 

但還是失敗了。

如果我在Django shell_plus運行:

import settings 
print settings.INSTALLED_APPS 
for app in settings.INSTALLED_APPS: 
    print app 

我可以看到應用程序,但是當我運行:

from django.db import connection 
cursor = connection.cursor() 
qry="""SELECT name FROM sqlite_master 
WHERE type IN ('table','view') AND name NOT LIKE 'sqlite_%%' 
UNION ALL 
SELECT name FROM sqlite_temp_master 
WHERE type IN ('table','view') 
ORDER BY 1""" 
cursor.execute(qry) 
results = cursor.fetchall() 
for tablename in results: 
    print tablename 

我無法發現表名。

任何人都可以幫到我嗎?

感謝

+0

如果您的模型中的表格不是由您的遷移創建的,也會發生這種情況。請參閱:http://stackoverflow.com/a/39246325/1551116 –

回答

6

我也有這個問題。原來,我不得不在settings.py文件中添加一個TEST_NAME屬性來正確識別測試數據庫。它解決了我的問題:

if 'test' in sys.argv: 
    DATABASES = { 
     'default': { 
      'ENGINE': 'django.db.backends.sqlite3', 
      'NAME': os.path.join(os.path.dirname(__file__), 'test.db'), 
      'TEST_NAME': os.path.join(os.path.dirname(__file__), 'test.db'), 
     } 
    } 
+2

即使在對「NAME」和「TEST_NAME」使用這些設置後,我也會得到相同的錯誤 – nnyby

+0

我在settings.py DATABASES中得到了兩個條目。添加'TEST_NAME'爲我解決了它。一個可以是':記憶:'但不是兩個。嘗試使用':memory:?cache = shared'(每個SQLite文檔)不起作用。 Python 2.7.8/Django 1.6。 – JimB

+0

不要這樣做!從非內存數據庫運行測試將永遠耗盡! – Cerin

0

我覺得「manage.py測試」空數據庫啓動,所以沒有辦法「執行syncdb」如何能夠幫助您。

而且我不知道這是否是錯字只是在你的問題,但設置的參數被稱爲

--settings 

,而不是:

--setting 

BTW。如果你的代碼是importings設置,使用進口這樣:

from django.conf import settings 
4

難道這些Selenium測試,以任何機會呢? This page表示您需要爲您的數據庫定義一個TEST_NAME,因爲內存數據庫由於某種原因而損壞。

相關問題