问题描述
为什么这行
users_favorited_by = models.ManyToManyField('auth.User')
或这个导致同样的错误:
or this one, which causes the same error:
users_favorited_by = models.ManyToManyField(User)
in this model
class Album(models.Model):
OFFICIALITY = (
('J', 'Major studio release'),
('I', 'Non-major official release'),
('U', 'Unofficial'),
)
title = models.CharField(max_length=70)
description = models.TextField(max_length=500, default="", null=True, blank=True)
pub_date = models.DateField('release date')
officiality = models.CharField(max_length=1, choices=OFFICIALITY)
is_concert = models.BooleanField(default=False)
main_info_url = models.URLField(blank=False)
thumbnail = models.FileField(upload_to=get_upload_file_name, blank=True, null=True)
#virtual field to skip over the through table.
songs = models.ManyToManyField("Song", through="AlbumSong")
users_favorited_by = models.ManyToManyField('auth.User')
#Alternative to Meta.ordering [2/2]:
#objects = AlbumAscPubDateManager()
def __str__(self):
return self.title
class Meta:
#Default ordering is by release date, ascending.
ordering = ['pub_date']
在管理界面导致此错误,点击某个专辑?
resulting in this error in the admin interface, when clicking on a specific album?
ProgrammingError at /admin/billyjoel/album/1/
relation "billyjoel_album_users_favorited_by" does not exist
LINE 1: ...LECT "auth_user"."id" FROM "auth_user" INNER JOIN "billyjoel...
^
管理界面工作正常,直到您点击相册查看其详细信息。
The admin interface works fine, until you click on an album to see its detail.
评论 ManyToManyField
out,运行 makemigrations
然后迁移
,并且管理界面工作正常 - 您可以看到相册的详细信息。
Comment that ManyToManyField
out, run makemigrations
then migrate
, and the admin interface works fine--you can see the album's detail.
根据,错误意味着该列不存在在
According to this comment, the error implies that the column does not exist in the database.
那么为什么,当 - 注销 ManyToManyField
(再次运行 makemigrations
然后 migrate
),没有创建数据库表?在我的中,前两个回覆者两者都声明应该自动创建通过表。那么它在哪里?
So why, when un-commenting out the ManyToManyField
(and again running makemigrations
then migrate
), is no database table being created? As in my previous question, the first two answerers both state that an automatically-created through table should be made. So where is it?
jdb=# \dt public.*
List of relations
Schema | Name | Type | Owner
--------+----------------------------+-------+--------
public | auth_group | table | pguser
public | auth_group_permissions | table | pguser
public | auth_permission | table | pguser
public | auth_user | table | pguser
public | auth_user_groups | table | pguser
public | auth_user_user_permissions | table | pguser
public | billyjoel_album | table | pguser
public | billyjoel_albumsong | table | pguser
public | billyjoel_song | table | pguser
public | django_admin_log | table | pguser
public | django_content_type | table | pguser
public | django_migrations | table | pguser
public | django_session | table | pguser
(13 rows)
追溯:
Traceback:
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8001/admin/billyjoel/album/1/
Django Version: 1.7c2
Python Version: 3.4.0
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'billyjoel')
Installed Middleware:
('django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware')
Traceback:
File "/home/jeffy/django_files/django_test_venv/lib/python3.4/site-packages/django/core/handlers/base.py" in get_response
111. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/jeffy/django_files/django_test_venv/lib/python3.4/site-packages/django/contrib/admin/options.py" in wrapper
546. return self.admin_site.admin_view(view)(*args, **kwargs)
File "/home/jeffy/django_files/django_test_venv/lib/python3.4/site-packages/django/utils/decorators.py" in _wrapped_view
105. response = view_func(request, *args, **kwargs)
File "/home/jeffy/django_files/django_test_venv/lib/python3.4/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
52. response = view_func(request, *args, **kwargs)
File "/home/jeffy/django_files/django_test_venv/lib/python3.4/site-packages/django/contrib/admin/sites.py" in inner
204. return view(request, *args, **kwargs)
File "/home/jeffy/django_files/django_test_venv/lib/python3.4/site-packages/django/contrib/admin/options.py" in change_view
1416. return self.changeform_view(request, object_id, form_url, extra_context)
File "/home/jeffy/django_files/django_test_venv/lib/python3.4/site-packages/django/utils/decorators.py" in _wrapper
29. return bound_func(*args, **kwargs)
File "/home/jeffy/django_files/django_test_venv/lib/python3.4/site-packages/django/utils/decorators.py" in _wrapped_view
105. response = view_func(request, *args, **kwargs)
File "/home/jeffy/django_files/django_test_venv/lib/python3.4/site-packages/django/utils/decorators.py" in bound_func
25. return func.__get__(self, type(self))(*args2, **kwargs2)
File "/home/jeffy/django_files/django_test_venv/lib/python3.4/site-packages/django/db/transaction.py" in inner
394. return func(*args, **kwargs)
File "/home/jeffy/django_files/django_test_venv/lib/python3.4/site-packages/django/contrib/admin/options.py" in changeform_view
1378. form = ModelForm(instance=obj)
File "/home/jeffy/django_files/django_test_venv/lib/python3.4/site-packages/django/forms/models.py" in __init__
319. object_data = model_to_dict(instance, opts.fields, opts.exclude)
File "/home/jeffy/django_files/django_test_venv/lib/python3.4/site-packages/django/forms/models.py" in model_to_dict
149. data[f.name] = list(qs.values_list('pk', flat=True))
File "/home/jeffy/django_files/django_test_venv/lib/python3.4/site-packages/django/db/models/query.py" in __iter__
141. self._fetch_all()
File "/home/jeffy/django_files/django_test_venv/lib/python3.4/site-packages/django/db/models/query.py" in _fetch_all
964. self._result_cache = list(self.iterator())
File "/home/jeffy/django_files/django_test_venv/lib/python3.4/site-packages/django/db/models/query.py" in iterator
1200. for row in self.query.get_compiler(self.db).results_iter():
File "/home/jeffy/django_files/django_test_venv/lib/python3.4/site-packages/django/db/models/sql/compiler.py" in results_iter
699. for rows in self.execute_sql(MULTI):
File "/home/jeffy/django_files/django_test_venv/lib/python3.4/site-packages/django/db/models/sql/compiler.py" in execute_sql
785. cursor.execute(sql, params)
File "/home/jeffy/django_files/django_test_venv/lib/python3.4/site-packages/django/db/backends/utils.py" in execute
81. return super(CursorDebugWrapper, self).execute(sql, params)
File "/home/jeffy/django_files/django_test_venv/lib/python3.4/site-packages/django/db/backends/utils.py" in execute
65. return self.cursor.execute(sql, params)
File "/home/jeffy/django_files/django_test_venv/lib/python3.4/site-packages/django/db/utils.py" in __exit__
94. six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/home/jeffy/django_files/django_test_venv/lib/python3.4/site-packages/django/utils/six.py" in reraise
549. raise value.with_traceback(tb)
File "/home/jeffy/django_files/django_test_venv/lib/python3.4/site-packages/django/db/backends/utils.py" in execute
65. return self.cursor.execute(sql, params)
Exception Type: ProgrammingError at /admin/billyjoel/album/1/
Exception Value: relation "billyjoel_album_users_favorited_by" does not exist
LINE 1: ...LECT "auth_user"."id" FROM "auth_user" INNER JOIN "billyjoel...
^
推荐答案
我认为你的数据库处于不一致的状态门庭/迁移/取消注释。尝试完全删除数据库,删除迁移文件,并运行makemigrations并重新迁移。
I think your database is in an inconsistent state through all this commenting/migrating/uncommenting. Try dropping your db completely, deleting your migration files, and running makemigrations and migrate again.
这篇关于为什么Django ManyToManyField导致管理界面崩溃?为什么没有通过表创建?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!