如何在 Django Rest 中的 instectdb 之后迁移已编辑的模型?

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

我对 Django Rest 完全陌生。我正在尝试构建一个小型 API 来学习。在我的本地数据库中,我有一个名为“api_task”的表,其结构如下:

api_task: {
   task_id[PK],
   task_title varchar(15),
   task_description varchar(150)
}

我运行命令

python manage.py inspectdb api task > models.py
将该表包含在 Django 项目中。我添加了一个字段并对模型进行了一些更改:

class ApiTask(models.Model):
    task_id = models.AutoField(primary_key=True)
    title = models.CharField(max_length=100, blank=True)
    description = models.TextField(max_length=250, blank=True)
    created_at = models.DateTimeField(auto_now_add=True)

   def __str__(self):
       return self.title

   class Meta:
       db_table = 'api_task'

之后,我使用

python manage.py makemigrations api
创建了迁移,其中“api”只是应用程序名称。执行迁移时会抛出错误:
jango.db.utils.ProgrammingError: relationship already exists
。我正在查看有关此主题的信息并尝试运行命令
python manage.py makemigrations api --fake
,但模型更改未反映在数据库中。我也尝试运行命令
python manage.py migrate api --run-syncdb
,但它引发了另一个错误:
Cannot use run_syncdb with application 'api' as it has migrations.

我知道 Django 说你不能更改字段名称或它们的值以防止信息丢失我认为,但我根本不是这样,我只是想用新字段和新字段选项更新表.看起来很简单,但我真的不知道问题出在哪里。

python-3.x django django-models django-rest-framework django-migrations
© www.soinside.com 2019 - 2024. All rights reserved.