2016-07-25 20 views
2

OK, ich bin ratlos. Ich denke, ich habe alles versucht.django.db.utils.OperationalError egal was ich versuche

Ich erstellte ein Modell in einer App models.py und fügte auch einige weitere Felder zu einem vorhandenen Modell hinzu (alle mit Standardwerten). Dann lief ich makemigrations. Das Ergebnis:

$ python3 manage.py makemigrations 
Traceback (most recent call last): 
    File "/usr/local/lib/python3.5/dist-packages/django/db/backends/utils.py", line 64, in execute 
    return self.cursor.execute(sql, params) 
    File "/usr/local/lib/python3.5/dist-packages/django/db/backends/sqlite3/base.py", line 323, in execute 
    return Database.Cursor.execute(self, query, params) 
sqlite3.OperationalError: no such column: myapp_MyExistingModel.first_new_field 

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

Traceback (most recent call last): 
    File "manage.py", line 14, in <module> 
    execute_from_command_line(sys.argv) 
    File "/usr/local/lib/python3.5/dist-packages/django/core/management/__init__.py", line 353, in execute_from_command_line 
    utility.execute() 
    File "/usr/local/lib/python3.5/dist-packages/django/core/management/__init__.py", line 345, in execute 
    self.fetch_command(subcommand).run_from_argv(self.argv) 
    File "/usr/local/lib/python3.5/dist-packages/django/core/management/base.py", line 348, in run_from_argv 
    self.execute(*args, **cmd_options) 
    File "/usr/local/lib/python3.5/dist-packages/django/core/management/base.py", line 398, in execute 
    self.check() 
    File "/usr/local/lib/python3.5/dist-packages/django/core/management/base.py", line 426, in check 
    include_deployment_checks=include_deployment_checks, 
    File "/usr/local/lib/python3.5/dist-packages/django/core/checks/registry.py", line 75, in run_checks 
    new_errors = check(app_configs=app_configs) 
    File "/usr/local/lib/python3.5/dist-packages/django/core/checks/urls.py", line 13, in check_url_config 
    return check_resolver(resolver) 
    File "/usr/local/lib/python3.5/dist-packages/django/core/checks/urls.py", line 23, in check_resolver 
    for pattern in resolver.url_patterns: 
    File "/usr/local/lib/python3.5/dist-packages/django/utils/functional.py", line 33, in __get__ 
    res = instance.__dict__[self.name] = self.func(instance) 
    File "/usr/local/lib/python3.5/dist-packages/django/core/urlresolvers.py", line 417, in url_patterns 
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module) 
    File "/usr/local/lib/python3.5/dist-packages/django/utils/functional.py", line 33, in __get__ 
    res = instance.__dict__[self.name] = self.func(instance) 
    File "/usr/local/lib/python3.5/dist-packages/django/core/urlresolvers.py", line 410, in urlconf_module 
    return import_module(self.urlconf_name) 
    File "/usr/lib/python3.5/importlib/__init__.py", line 126, in import_module 
    return _bootstrap._gcd_import(name[level:], package, level) 
    File "<frozen importlib._bootstrap>", line 986, in _gcd_import 
    File "<frozen importlib._bootstrap>", line 969, in _find_and_load 
    File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked 
    File "<frozen importlib._bootstrap>", line 673, in _load_unlocked 
    File "<frozen importlib._bootstrap_external>", line 665, in exec_module 
    File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed 
    File "/path/to/myproject/myproject/urls.py", line 49, in <module> 
    url(r'^someurl/', include('myapp.urls')), 
    File "/usr/local/lib/python3.5/dist-packages/django/conf/urls/__init__.py", line 52, in include 
    urlconf_module = import_module(urlconf_module) 
    File "/usr/lib/python3.5/importlib/__init__.py", line 126, in import_module 
    return _bootstrap._gcd_import(name[level:], package, level) 
    File "<frozen importlib._bootstrap>", line 986, in _gcd_import 
    File "<frozen importlib._bootstrap>", line 969, in _find_and_load 
    File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked 
    File "<frozen importlib._bootstrap>", line 673, in _load_unlocked 
    File "<frozen importlib._bootstrap_external>", line 665, in exec_module 
    File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed 
    File "/path/to/myproject/myapp/urls.py", line 2, in <module> 
    from . import views 
    File "/path/to/myproject/myapp/views.py", line 8, in <module> 
    existing_model = MyExistingModel.objects.all()[0] 
    File "/usr/local/lib/python3.5/dist-packages/django/db/models/query.py", line 297, in __getitem__ 
    return list(qs)[0] 
    File "/usr/local/lib/python3.5/dist-packages/django/db/models/query.py", line 258, in __iter__ 
    self._fetch_all() 
    File "/usr/local/lib/python3.5/dist-packages/django/db/models/query.py", line 1074, in _fetch_all 
    self._result_cache = list(self.iterator()) 
    File "/usr/local/lib/python3.5/dist-packages/django/db/models/query.py", line 52, in __iter__ 
    results = compiler.execute_sql() 
    File "/usr/local/lib/python3.5/dist-packages/django/db/models/sql/compiler.py", line 848, in execute_sql 
    cursor.execute(sql, params) 
    File "/usr/local/lib/python3.5/dist-packages/django/db/backends/utils.py", line 79, in execute 
    return super(CursorDebugWrapper, self).execute(sql, params) 
    File "/usr/local/lib/python3.5/dist-packages/django/db/backends/utils.py", line 64, in execute 
    return self.cursor.execute(sql, params) 
    File "/usr/local/lib/python3.5/dist-packages/django/db/utils.py", line 95, in __exit__ 
    six.reraise(dj_exc_type, dj_exc_value, traceback) 
    File "/usr/local/lib/python3.5/dist-packages/django/utils/six.py", line 685, in reraise 
    raise value.with_traceback(tb) 
    File "/usr/local/lib/python3.5/dist-packages/django/db/backends/utils.py", line 64, in execute 
    return self.cursor.execute(sql, params) 
    File "/usr/local/lib/python3.5/dist-packages/django/db/backends/sqlite3/base.py", line 323, in execute 
     return Database.Cursor.execute(self, query, params) 
    django.db.utils.OperationalError: no such column: myapp_MyExistingModel.first_new_field 

Wenn es darauf ankommt, bin ich mit Mezzanine CMS und MyExistingModel ist eine Erweiterung seiner Page Klasse. Es hat nur eine Instanz.

Also hier ist, was ich versuchte weiter:

  1. manage.py migrate --fake
  2. manage.py flush
  3. meine Datenbank umbenennen so ein neues Geschäft würde

Alle diese die gleichen Zurückverfolgungs warfen erstellt werden. Ich ging in die letzte Migration für die App. Es enthielt eine AddModel für mein neues Modell und eine RemoveField für ein Feld in dem alten Modell ich umbenannt, aber keine AddField für die neuen Felder, die ich erstellt habe. Also habe ich sie manuell zur Migrationsdatei hinzugefügt und manage.py migrate --fake erneut versucht. Gleiche Rückverfolgung. Ich habe auch alle anderen Befehle ausprobiert. Gleiche Rückverfolgung.

Schließlich löschte ich alle Migrationen der App und ließ nur __init__.py. Ich habe die Datenbank auch umbenannt, damit sie neu erstellt wird. Ich habe flush, makemigrations und migrate (mit und ohne --fake) wiederholt. Alle diese in dieser Zurückverfolgungs Folge:

Traceback (most recent call last): 
    File "/usr/local/lib/python3.5/dist-packages/django/db/backends/utils.py", line 64, in execute 
    return self.cursor.execute(sql, params) 
    File "/usr/local/lib/python3.5/dist-packages/django/db/backends/sqlite3/base.py", line 323, in execute 
    return Database.Cursor.execute(self, query, params) 
sqlite3.OperationalError: no such table: myapp_MyExistingModel 

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

Traceback (most recent call last): 
    File "manage.py", line 14, in <module> 
    execute_from_command_line(sys.argv) 
    File "/usr/local/lib/python3.5/dist-packages/django/core/management/__init__.py", line 353, in execute_from_command_line 
    utility.execute() 
    File "/usr/local/lib/python3.5/dist-packages/django/core/management/__init__.py", line 345, in execute 
    self.fetch_command(subcommand).run_from_argv(self.argv) 
    File "/usr/local/lib/python3.5/dist-packages/django/core/management/base.py", line 348, in run_from_argv 
    self.execute(*args, **cmd_options) 
    File "/usr/local/lib/python3.5/dist-packages/django/core/management/base.py", line 398, in execute 
    self.check() 
    File "/usr/local/lib/python3.5/dist-packages/django/core/management/base.py", line 426, in check 
    include_deployment_checks=include_deployment_checks, 
    File "/usr/local/lib/python3.5/dist-packages/django/core/checks/registry.py", line 75, in run_checks 
    new_errors = check(app_configs=app_configs) 
    File "/usr/local/lib/python3.5/dist-packages/django/core/checks/urls.py", line 13, in check_url_config 
    return check_resolver(resolver) 
    File "/usr/local/lib/python3.5/dist-packages/django/core/checks/urls.py", line 23, in check_resolver 
    for pattern in resolver.url_patterns: 
    File "/usr/local/lib/python3.5/dist-packages/django/utils/functional.py", line 33, in __get__ 
    res = instance.__dict__[self.name] = self.func(instance) 
    File "/usr/local/lib/python3.5/dist-packages/django/core/urlresolvers.py", line 417, in url_patterns 
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module) 
    File "/usr/local/lib/python3.5/dist-packages/django/utils/functional.py", line 33, in __get__ 
    res = instance.__dict__[self.name] = self.func(instance) 
    File "/usr/local/lib/python3.5/dist-packages/django/core/urlresolvers.py", line 410, in urlconf_module 
    return import_module(self.urlconf_name) 
    File "/usr/lib/python3.5/importlib/__init__.py", line 126, in import_module 
    return _bootstrap._gcd_import(name[level:], package, level) 
    File "<frozen importlib._bootstrap>", line 986, in _gcd_import 
    File "<frozen importlib._bootstrap>", line 969, in _find_and_load 
    File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked 
    File "<frozen importlib._bootstrap>", line 673, in _load_unlocked 
    File "<frozen importlib._bootstrap_external>", line 665, in exec_module 
    File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed 
    File "/path/to/myproject/myproject/urls.py", line 49, in <module> 
    url(r'^someurl/', include('myapp.urls')), 
    File "/usr/local/lib/python3.5/dist-packages/django/conf/urls/__init__.py", line 52, in include 
    urlconf_module = import_module(urlconf_module) 
    File "/usr/lib/python3.5/importlib/__init__.py", line 126, in import_module 
    return _bootstrap._gcd_import(name[level:], package, level) 
    File "<frozen importlib._bootstrap>", line 986, in _gcd_import 
    File "<frozen importlib._bootstrap>", line 969, in _find_and_load 
    File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked 
    File "<frozen importlib._bootstrap>", line 673, in _load_unlocked 
    File "<frozen importlib._bootstrap_external>", line 665, in exec_module 
    File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed 
    File "/path/to/myproject/myapp/urls.py", line 2, in <module> 
    from . import views 
    File "/path/to/myproject/myapp/views.py", line 8, in <module> 
    existing_model = MyExistingModel.objects.all()[0] 
    File "/usr/local/lib/python3.5/dist-packages/django/db/models/query.py", line 297, in __getitem__ 
    return list(qs)[0] 
    File "/usr/local/lib/python3.5/dist-packages/django/db/models/query.py", line 258, in __iter__ 
    self._fetch_all() 
    File "/usr/local/lib/python3.5/dist-packages/django/db/models/query.py", line 1074, in _fetch_all 
    self._result_cache = list(self.iterator()) 
    File "/usr/local/lib/python3.5/dist-packages/django/db/models/query.py", line 52, in __iter__ 
    results = compiler.execute_sql() 
    File "/usr/local/lib/python3.5/dist-packages/django/db/models/sql/compiler.py", line 848, in execute_sql 
    cursor.execute(sql, params) 
    File "/usr/local/lib/python3.5/dist-packages/django/db/backends/utils.py", line 79, in execute 
    return super(CursorDebugWrapper, self).execute(sql, params) 
    File "/usr/local/lib/python3.5/dist-packages/django/db/backends/utils.py", line 64, in execute 
    return self.cursor.execute(sql, params) 
    File "/usr/local/lib/python3.5/dist-packages/django/db/utils.py", line 95, in __exit__ 
    six.reraise(dj_exc_type, dj_exc_value, traceback) 
    File "/usr/local/lib/python3.5/dist-packages/django/utils/six.py", line 685, in reraise 
    raise value.with_traceback(tb) 
    File "/usr/local/lib/python3.5/dist-packages/django/db/backends/utils.py", line 64, in execute 
    return self.cursor.execute(sql, params) 
    File "/usr/local/lib/python3.5/dist-packages/django/db/backends/sqlite3/base.py", line 323, in execute 
    return Database.Cursor.execute(self, query, params) 
django.db.utils.OperationalError: no such table: myapp_MyExistingModel 

Zu diesem Zeitpunkt habe ich keine Ahnung, was noch außer start over nächste, zu versuchen. Irgendwelche Vorschläge werden geschätzt.

+0

Warum migrieren Sie --fake? Das würde dazu führen, dass die Tabellen nicht erstellt werden, was wahrscheinlich der Grund dafür ist, dass Sie das sehen. – e4c5

+0

Das war der Zaubertrick, der mir das letzte Mal, als ich Inkonsistenzen zwischen der Datenbank und den Migrationen hatte, erspart hat. Ich glaube nicht, dass "migrieren" vor "Makemigrationen" funktionieren könnte, außer nach der manuellen Bearbeitung der Migrationsdatei. Ich habe gerade 'migrieren' ohne' --fake' versucht, aber das hat auch nicht funktioniert. Ich werde die Frage bearbeiten, um das zu reflektieren. – Keith

+0

Hier habe ich mich durch Fälschungen gerettet: https://groups.google.com/d/msg/mezzanine-users/wp7TAK2L_Dg/OZXKfHRPAwAJ – Keith

Antwort

2

Ich habe es herausgefunden! Lektion gelernt: Lesen Sie den gesamten StackTrace, nicht nur die letzte Zeile. In diesem Fall war die Offensive Line in meinem views.py:

File "/path/to/myproject/myapp/views.py", line 8, in <module> 
    existing_model = MyExistingModel.objects.all()[0] 

Wie ich schon sagte, MyExistingModel nur eine Instanz hat, und diese Linie festgelegt, dass die Instanz auf die lokale Variable existing_model für den Einsatz im views.py Code.

Ich verstehe immer noch nicht, warum makemigrations würde views.py ausführen müssen. Aber es tut, anscheinend! Dabei wurde MyExistingModel von models.py, einschließlich die neuen Felder importiert. Aber diese Felder waren noch nicht migriert worden. Daher OperationalError.

+1

_ "Kommentare sehr geschätzt" _ - Ich schlage vor, Sie öffnen eine neue Frage, da Kommentare nicht dafür sind ausgedehnte Diskussionen. –

+0

Das stimmt.OK, ich habe die Diskussion zu [this post on reddit] verschoben (https://www.reddit.com/r/django/comments/4ut03i/makemigrations_sticking_its_nose_in_my_views/). – Keith