英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论