Python Django误用迁移

Python Django误用迁移,python,django,postgresql,psycopg2,django-migrations,Python,Django,Postgresql,Psycopg2,Django Migrations,我不确定我是做错了什么,还是我在项目中使用的其中一个部件有问题 基本上,我在模型中添加了一个字段,并尝试进行迁移 (app-web) user@user-desktop:~/sites/app-web/app$ python manage.py migrate video_uploader Operations to

我不确定我是做错了什么,还是我在项目中使用的其中一个部件有问题

基本上,我在模型中添加了一个字段,并尝试进行迁移

(app-web) user@user-desktop:~/sites/app-web/app$ python manage.py migrate video_uploader                                                                                          
Operations to perform:
Apply all migrations: video_uploader
Running migrations:
Applying video_uploader.0002_video_creation_date...Traceback (most recent call last):
File "/home/user/miniconda3/envs/app-web/lib/python3.7/site-packages/django/db/backends/ut
ils.py", line 85, in _execute                                                                         
return self.cursor.execute(sql, params)
psycopg2.errors.DuplicateColumn: column "creation_date" of relation "video_uploader_video" already exists           
这是模型。该字段是
海报
之一

class Video(models.Model):
    title=models.CharField(max_length=500)
    description=models.TextField(default="")
    creation_date=models.DateTimeField(default=timezone.now)
    videofile=models.FileField(upload_to='videos/', null=True, verbose_name="")
    poster=models.ImageField(upload_to='video/thumbnails', null=True, verbose_name="")
    tags = TaggableManager()

    actions = ['delete']

    def __str__(self):
        return self.title + ": " + str(self.videofile)
    ...
这是模型中唯一改变的东西。让我们进行迁移

(app-web) selfishman@user-desktop:~/sites/app-web/app$ python manage.py makemigrations                                                                                                  
Migrations for 'video_uploader':
video_uploader/migrations/0007_video_poster.py
- Add field poster to video
到目前为止,一切顺利。让我们尝试应用迁移

(app-web) user@user-desktop:~/sites/app-web/app$ python manage.py migrate video_uploader                                                                                          
Operations to perform:
Apply all migrations: video_uploader
Running migrations:
Applying video_uploader.0002_video_creation_date...Traceback (most recent call last):
File "/home/user/miniconda3/envs/app-web/lib/python3.7/site-packages/django/db/backends/ut
ils.py", line 85, in _execute                                                                         
return self.cursor.execute(sql, params)
psycopg2.errors.DuplicateColumn: column "creation_date" of relation "video_uploader_video" already exists           
还有剩余的回溯:

上述异常是以下异常的直接原因:

Traceback (most recent call last):
File "manage.py", line 20, in <module>
    execute_from_command_line(sys.argv)
    File "/home/user/miniconda3/envs/app-web/lib/python3.7/site-packages/django/core/managemen
    t/__init__.py", line 381, in execute_from_command_line                                                
    utility.execute()
    File "/home/user/miniconda3/envs/app-web/lib/python3.7/site-packages/django/core/managemen
    t/__init__.py", line 375, in execute                                                                  
    self.fetch_command(subcommand).run_from_argv(self.argv)
    File "/home/user/miniconda3/envs/app-web/lib/python3.7/site-packages/django/core/managemen
    t/base.py", line 316, in run_from_argv                                                                
    self.execute(*args, **cmd_options)
    File "/home/user/miniconda3/envs/app-web/lib/python3.7/site-packages/django/core/managemen
    t/base.py", line 353, in execute                                                                      
    output = self.handle(*args, **options)
    File "/home/user/miniconda3/envs/app-web/lib/python3.7/site-packages/django/core/managemen
    t/base.py", line 83, in wrapped                                                                       
    res = handle_func(*args, **kwargs)
    File "/home/user/miniconda3/envs/app-web/lib/python3.7/site-packages/django/core/managemen
    t/commands/migrate.py", line 203, in handle                                                           
    fake_initial=fake_initial,
    File "/home/user/miniconda3/envs/app-web/lib/python3.7/site-packages/django/db/migrations/
    executor.py", line 117, in migrate                                                                    
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
    File "/home/user/miniconda3/envs/app-web/lib/python3.7/site-packages/django/db/migrations/
    executor.py", line 147, in _migrate_all_forwards                                                      
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
    File "/home/user/miniconda3/envs/app-web/lib/python3.7/site-packages/django/db/migrations/
    executor.py", line 244, in apply_migration                                                            
    state = migration.apply(state, schema_editor)
    File "/home/user/miniconda3/envs/app-web/lib/python3.7/site-packages/django/db/migrations/
    migration.py", line 124, in apply                                                                     
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
    File "/home/user/miniconda3/envs/app-web/lib/python3.7/site-packages/django/db/migrations/
    operations/fields.py", line 84, in database_forwards                                                  
    field,
    File "/home/user/miniconda3/envs/app-web/lib/python3.7/site-packages/django/db/backends/ba
    se/schema.py", line 435, in add_field                                                                 
    self.execute(sql, params)
    File "/home/user/miniconda3/envs/app-web/lib/python3.7/site-packages/django/db/backends/ba
    se/schema.py", line 133, in execute                                                                   
    cursor.execute(sql, params)
    File "/home/user/miniconda3/envs/app-web/lib/python3.7/site-packages/django/db/backends/ut
    ils.py", line 100, in execute                                                                         
    return super().execute(sql, params)
    File "/home/user/miniconda3/envs/app-web/lib/python3.7/site-packages/django/db/backends/ut
    ils.py", line 68, in execute                                                                          
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
    File "/home/user/miniconda3/envs/app-web/lib/python3.7/site-packages/django/db/backends/ut
    ils.py", line 77, in _execute_with_wrappers                                                           
    return executor(sql, params, many, context)
    File "/home/user/miniconda3/envs/app-web/lib/python3.7/site-packages/django/db/backends/ut
    ils.py", line 85, in _execute                                                                         
    return self.cursor.execute(sql, params)
    File "/home/user/miniconda3/envs/app-web/lib/python3.7/site-packages/django/db/utils.py", 
    line 89, in __exit__                                                                                  
    raise dj_exc_value.with_traceback(traceback) from exc_value
    File "/home/user/miniconda3/envs/app-web/lib/python3.7/site-packages/django/db/backends/ut
    ils.py", line 85, in _execute                                                                         
    return self.cursor.execute(sql, params)
    django.db.utils.ProgrammingError: column "creation_date" of relation "video_uploader_video" already exists         
有人能告诉我这里发生了什么事吗?我正在使用Postgres 12。当我运行测试,并且从头开始创建(SQLite)DB时,没有这样的错误

感谢您的帮助


另外,在Django移民和Postgres/Psycopg2方面,我们已经看到了相当多的不一致之处。不确定配置或版本/依赖项是否有问题

您创建了一个新迁移,并将其命名为

0007_video_poster
但是,当您运行迁移时,它正在运行

0002_video_creation_date
这是试图创建一个名为
creation\u date
的新列,但它已经存在了

您得到的结果不一致,因为django认为以前的迁移没有应用,因此它正在尝试应用它们

最简单的方法是使用
刷新
数据库(确保首先导出可能需要的任何数据)

这将重置数据库,然后您可以正常运行迁移,并且应该可以正常工作

否则,如果您要执行刚刚创建的迁移,即
0007\u video\u poster

你可以运行这个

python manage.py migrate video\u uploader 0007\u video\u poster

在我发布问题之前,我确实试过最后一行。它产生相同的精确误差。我的推理是,Django以某种方式向上(向下)遍历依赖链,直到它到达
0002
,这使得它出错。请注意,
0007
取决于
0006
,依此类推。当涉及到你答案的第一部分时,是否期望我每次调整模型时都这样做?也就是说,这看起来像是脖子上的巨大疼痛/试试看:删除迁移文件夹中的所有文件(init文件除外),然后进行makemigrations,然后进行迁移。不,你不应该这样做。我也在用Django和PostgreSQL做一个项目。。。我也曾经遇到过这个问题。如果没有任何效果,请使用psql命令行手动删除该表,然后按照上述注释中提到的步骤操作。是否尝试只运行
python manage.py migrate
,而不指定要运行的迁移?Django应该能够为您处理这个问题。您能将代码还原到该点并在迁移之前运行迁移吗?如果是这样的话-在那之后添加代码并进行迁移。看起来好像有些东西出了问题。希望你能使用一些源代码控制(比如git)。我希望您在更改特定模型之前,有一些分支/标记/提交可以工作。如果是,请创建新的django安装,使用此“时间点”,运行迁移(以更新所有数据库结构),确保一切正常,然后在模型中进行更改并再次运行迁移。大多数情况下,在不还原的情况下意外运行迁移/db更改,然后创建另一个迁移并运行它。当您在分支/标记之间移动并运行迁移时,经常会发生这种情况,但在移动到其他分支之前不会还原它们。自从我上次使用django以来已经有一段时间了,所以您需要检查“迁移”是否也为您回滚。如果不是,则需要手动处理(在切换出具有特定迁移的分支之前回滚)。
python manage.py flush