I keep getting the following error django.db.utils.ProgrammingError: column "name" of relation "blog_post" already exists in my django app
I keep getting the following error django.db.utils.ProgrammingError: column "name" of relation "blog_post" already exists in my django app
当我尝试迁移时出现以下错误
django.db.utils.ProgrammingError: column "name" of relation "blog_post" already exists
现在我假设该消息意味着我正在尝试创建一个名为 "name" 的列,而同名的列已经存在。所以我查看了我的模型,以确保一个不存在并且它不存在。我不明白这是什么问题。这是我的模型。哦,在我 运行 迁移之前,我先 运行 迁移。所以这不是问题
class Post(models.Model):
STATUS_CHOICES = (
('draft', 'Draft'),
('published', 'Published'),
)
title = models.CharField(max_length=250, unique=True)
slug = models.SlugField(max_length=250,
unique_for_date='publish')
image = models.ImageField(upload_to=upload_location,
null=True,
blank=True,
height_field='height_field',
width_field='width_field')
image_url = models.CharField(max_length=500,
null=True,
blank=True,
)
height_field = models.IntegerField(default=0,
null=True,
blank=True,
)
width_field = models.IntegerField(default=0,
null=True,
blank=True,
)
author = models.ForeignKey(User,
related_name='blog_posts',
null=True,
blank=True,)
body = models.TextField(null=True, blank=True,)
publish = models.DateTimeField(default=timezone.now)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
status = models.CharField(max_length=10,
choices=STATUS_CHOICES,
default='draft')
video = models.BooleanField(default=False)
video_path = models.CharField(max_length=320,
null=True,
blank=True,)
url = models.CharField(max_length=320,
null=True,
blank=True, )
name = models.CharField(max_length=320,
null=True,
blank=True, )
class Meta:
ordering = ('-publish',)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('blog:post_detail', kwargs={"slug": self.slug})
objects = models.Manager() # The default manager.
published = PublishedManager() # Our custom manager.
tags = TaggableManager(blank=True)
这是我的 views.py
def post_list(request, tag_slug=None):
if request.user.is_staff or request.user.is_superuser:
object_list = Post.objects.all().order_by('-id')
else:
object_list = Post.published.all().order_by('-id')
articles = Article.objects.all()[:8]
# latest_article = Post.published.latest('created')
query = request.GET.get("q")
if query:
object_list = object_list.filter(
Q(title__icontains=query) |
Q(body__icontains=query) |
Q(tags__name__in=[query])
).distinct()
tag = None
if tag_slug:
tag = get_object_or_404(Tag, slug=tag_slug)
object_list = object_list.filter(tags__in=[tag])
paginator = Paginator(object_list, 15) # 3 posts in each page
page = request.GET.get('page')
try:
posts = paginator.page(page)
except PageNotAnInteger:
# If page is not an integer deliver the first page
posts = paginator.page(1)
except EmptyPage:
# If page is out of range deliver last page of results
posts = paginator.page(paginator.num_pages)
css_left = 'col-xs-12 col-lg-12 col-centered'
# css_right = 'col-xs-12 col-lg-3'
template = 'blog/post/list.html'
title = 'home'
page_num = paginator.num_pages
local = os.getenv('_system_name')
context = {
'page': page,
'posts': posts,
'tag': tag,
'title': title,
'articles': articles,
'css_left': css_left,
'paginator': paginator,
'page_num': page_num,
'local': local,
'query': query
# 'css_right': css_right
# 'latest': latest_article
}
return render(request, template, context)
这是回溯
Running migrations:
Rendering model states... DONE
Applying blog.0008_auto_20160625_1708...Traceback (most recent call last):
File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
psycopg2.ProgrammingError: column "name" of relation "blog_post" already exists
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "./manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
utility.execute()
File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/core/management/__init__.py", line 345, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/core/management/base.py", line 348, in run_from_argv
self.execute(*args, **cmd_options)
File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/core/management/base.py", line 399, in execute
output = self.handle(*args, **options)
File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/core/management/commands/migrate.py", line 200, in handle
executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/db/migrations/executor.py", line 92, in migrate
self._migrate_all_forwards(plan, full_plan, fake=fake, fake_initial=fake_initial)
File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/db/migrations/executor.py", line 121, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/db/migrations/executor.py", line 198, in apply_migration
state = migration.apply(state, schema_editor)
File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/db/migrations/migration.py", line 123, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/db/migrations/operations/fields.py", line 62, in database_forwards
field,
File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 396, in add_field
self.execute(sql, params)
File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 110, in execute
cursor.execute(sql, params)
File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/db/backends/utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/db/utils.py", line 95, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/utils/six.py", line 685, in reraise
raise value.with_traceback(tb)
File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: column "name" of relation "blog_post" already exists
我遇到了类似的问题。这对我有用
./manage.py migrate --fake
然后:
./manage.py makemigrations
只是为了确定然后 运行
./manage.py migrate
当我尝试迁移时出现以下错误
django.db.utils.ProgrammingError: column "name" of relation "blog_post" already exists
现在我假设该消息意味着我正在尝试创建一个名为 "name" 的列,而同名的列已经存在。所以我查看了我的模型,以确保一个不存在并且它不存在。我不明白这是什么问题。这是我的模型。哦,在我 运行 迁移之前,我先 运行 迁移。所以这不是问题
class Post(models.Model):
STATUS_CHOICES = (
('draft', 'Draft'),
('published', 'Published'),
)
title = models.CharField(max_length=250, unique=True)
slug = models.SlugField(max_length=250,
unique_for_date='publish')
image = models.ImageField(upload_to=upload_location,
null=True,
blank=True,
height_field='height_field',
width_field='width_field')
image_url = models.CharField(max_length=500,
null=True,
blank=True,
)
height_field = models.IntegerField(default=0,
null=True,
blank=True,
)
width_field = models.IntegerField(default=0,
null=True,
blank=True,
)
author = models.ForeignKey(User,
related_name='blog_posts',
null=True,
blank=True,)
body = models.TextField(null=True, blank=True,)
publish = models.DateTimeField(default=timezone.now)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
status = models.CharField(max_length=10,
choices=STATUS_CHOICES,
default='draft')
video = models.BooleanField(default=False)
video_path = models.CharField(max_length=320,
null=True,
blank=True,)
url = models.CharField(max_length=320,
null=True,
blank=True, )
name = models.CharField(max_length=320,
null=True,
blank=True, )
class Meta:
ordering = ('-publish',)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('blog:post_detail', kwargs={"slug": self.slug})
objects = models.Manager() # The default manager.
published = PublishedManager() # Our custom manager.
tags = TaggableManager(blank=True)
这是我的 views.py
def post_list(request, tag_slug=None):
if request.user.is_staff or request.user.is_superuser:
object_list = Post.objects.all().order_by('-id')
else:
object_list = Post.published.all().order_by('-id')
articles = Article.objects.all()[:8]
# latest_article = Post.published.latest('created')
query = request.GET.get("q")
if query:
object_list = object_list.filter(
Q(title__icontains=query) |
Q(body__icontains=query) |
Q(tags__name__in=[query])
).distinct()
tag = None
if tag_slug:
tag = get_object_or_404(Tag, slug=tag_slug)
object_list = object_list.filter(tags__in=[tag])
paginator = Paginator(object_list, 15) # 3 posts in each page
page = request.GET.get('page')
try:
posts = paginator.page(page)
except PageNotAnInteger:
# If page is not an integer deliver the first page
posts = paginator.page(1)
except EmptyPage:
# If page is out of range deliver last page of results
posts = paginator.page(paginator.num_pages)
css_left = 'col-xs-12 col-lg-12 col-centered'
# css_right = 'col-xs-12 col-lg-3'
template = 'blog/post/list.html'
title = 'home'
page_num = paginator.num_pages
local = os.getenv('_system_name')
context = {
'page': page,
'posts': posts,
'tag': tag,
'title': title,
'articles': articles,
'css_left': css_left,
'paginator': paginator,
'page_num': page_num,
'local': local,
'query': query
# 'css_right': css_right
# 'latest': latest_article
}
return render(request, template, context)
这是回溯
Running migrations:
Rendering model states... DONE
Applying blog.0008_auto_20160625_1708...Traceback (most recent call last):
File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
psycopg2.ProgrammingError: column "name" of relation "blog_post" already exists
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "./manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
utility.execute()
File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/core/management/__init__.py", line 345, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/core/management/base.py", line 348, in run_from_argv
self.execute(*args, **cmd_options)
File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/core/management/base.py", line 399, in execute
output = self.handle(*args, **options)
File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/core/management/commands/migrate.py", line 200, in handle
executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/db/migrations/executor.py", line 92, in migrate
self._migrate_all_forwards(plan, full_plan, fake=fake, fake_initial=fake_initial)
File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/db/migrations/executor.py", line 121, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/db/migrations/executor.py", line 198, in apply_migration
state = migration.apply(state, schema_editor)
File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/db/migrations/migration.py", line 123, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/db/migrations/operations/fields.py", line 62, in database_forwards
field,
File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 396, in add_field
self.execute(sql, params)
File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 110, in execute
cursor.execute(sql, params)
File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/db/backends/utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/db/utils.py", line 95, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/utils/six.py", line 685, in reraise
raise value.with_traceback(tb)
File "/Users/ray/Desktop/myheroku/practice/lib/python3.5/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: column "name" of relation "blog_post" already exists
我遇到了类似的问题。这对我有用
./manage.py migrate --fake
然后:
./manage.py makemigrations
只是为了确定然后 运行
./manage.py migrate