如何在Django Rest中使用instectdb后迁移编辑过的模型?

huangapple go评论55阅读模式
英文:

How to migrate an edited model after instectdb in Django Rest?

问题

I am completely new to Django Rest. I'm trying to build a small API to learn. In my local database I have a table named "api_task" with the following structure:

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

I ran the command python manage.py inspectdb api task > models.py to include that table in the Django project. I added a field and made some changes to the model:

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'

After that, I created the migration with python manage.py makemigrations api, where "api" is just the application name. When executing the migration it throws an error: jango.db.utils.ProgrammingError: relationship already exists. I was looking through the information on this topic and tried to run the command python manage.py makemigrations api --fake but the model changes are not reflected in the database. I also tried running the command python manage.py migrate api --run-syncdb, but it throws another error: Cannot use run_syncdb with application 'api' as it has migrations.

I know Django says you can't change field names or their values ​​to prevent loss of information I think, but that's not the case for me at all, I just want to update the table with the new field and new field options. Seem like something really simple to do but I don't really know what's the problem.

英文:

I am completely new to Django Rest. I'm trying to build a small API to learn. In my local database I have a table named "api_task" with the following structure:

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

I ran the command python manage.py inspectdb api task > models.py to include that table in the Django project. I added a field and made some changes to the model:

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'

After that, I created the migration with python manage.py makemigrations api, where "api" is just the application name. When executing the migration it throws an error: jango.db.utils.ProgrammingError: relationship already exists. I was looking through the information on this topic and tried to run the command python manage.py makemigrations api --fake but the model changes are not reflected in the database. I also tried running the command python manage.py migrate api --run-syncdb, but it throws another error: Cannot use run_syncdb with application 'api' as it has migrations.

I know Django says you can't change field names or their values ​​to prevent loss of information I think, but that's not the case for me at all, I just want to update the table with the new field and new field options. Seem like something really simple to do but I don't really know what's the problem.

答案1

得分: 1

你可以尝试删除所有迁移文件,然后运行以下两个命令:

python manage.py makemigrations
python manage.py migrate
英文:

You can try deleting all the migrations files once and run these two commands

python manage.py makemigrations
python manage.py migrate

huangapple
  • 本文由 发表于 2023年5月7日 14:17:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76192460.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定