Django错误应用迁移

我不确定我是在做错什么,还是我在该项目中使用的一件作品出了问题。

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

Here is the model. The field is the poster one.

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         

这是创建的迁移:

from django.db import migrations, models


    class Migration(migrations.Migration):

        dependencies = [
            ('video_uploader', '0006_video_description'),
        ]

        operations = [
            migrations.AddField(
                model_name='video',
                name='poster',
                field=models.ImageField(null=True, upload_to='video/thumbnails', verbose_name=''),
            ),
        ]

有人可以告诉我这是怎么回事吗?我正在使用Postgres12。当我运行测试并从头开始创建(SQLite)DB时,没有这样的错误。

任何帮助表示赞赏。

附言在Django迁移和Postgres / Psycopg2方面,我们已经看到很多不一致之处。不知道配置或版本/相关性是否有问题。

评论
  • 小妖小姐
    小妖小姐 回复

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

    0007_video_poster
    

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

    0002_video_creation_date
    

    And this is trying to create a new column named creation_date however that's already there.

    由于django认为未应用先前的迁移,因此您正在获得不一致的结果,因此它正在尝试应用它们。

    The easiest way would be to flush the database (make sure you first export any data you might need) using

    python manage.py flush
    

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

    Otherwise if you want to execute the migration you just created i.e. 0007_video_poster

    你可以运行这个

    python manage.py migrate video_uploader 0007_video_poster