Django错误应用迁移

问题描述 投票:0回答:1

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

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

下面是模型。这个字段是 poster 一。

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=''),
            ),
        ]

谁能告诉我这是怎么回事?我使用的是Postgres 12。当我运行测试,并从头开始创建一个(SQLite)DB时,没有出现这样的错误。

感谢任何帮助。

P.S.当涉及到Django迁移和PostgresPsycopg2时,我们已经看到了不少不一致的地方。不知道是配置的问题还是版本依赖的问题。

python django postgresql psycopg2 django-migrations
1个回答
1
投票

你创建了一个新的迁移,它被命名为

0007_video_poster

但当你运行迁移时,它却在运行以下内容

0002_video_creation_date

而这是在尝试创建一个新的列,名为 creation_date 然而这已经存在了。

你得到了不一致的结果,因为django认为之前的迁移没有被应用,所以它试图应用它们。

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

python manage.py flush

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

否则,如果你想执行你刚刚创建的迁移,即。0007_video_poster

你可以运行以下命令

python manage.py migrate video_uploader 0007_video_poster
© www.soinside.com 2019 - 2024. All rights reserved.