Django错误地期望id列 [英] Django incorrectly expecting id column

查看:83
本文介绍了Django错误地期望id列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Django中的现有SQL数据库.出于某种原因,这些表从未被赋予主键,因此,我仅要进行遍历并为其分配键.在这些模型之一中,我使用primary_key = True将现有的唯一索引更改为主索引.然后,我运行了./manage.py makemigrations (app_name); ./manage.py migrate.我遇到此错误:(1091,无法删除'id';请检查列/键是否存在").似乎Django认为该模型中有一个id字段,但不是因为我能够使用phpMyAdmin创建一个id字段,而当我重新运行迁移时,它成功了.虽然我能够解决我的问题,但我怀疑这是解决此问题的最佳方法.解决此问题的正确方法是什么?

I'm working with an existing SQL database in Django. These tables for some reason were never given primary keys so I'm just going through and assigning them ones. In one of these models, I changed an existing unique index to a primary index using primary_key = True. I then ran ./manage.py makemigrations (app_name); ./manage.py migrate. I came across this error: (1091, "Can't DROP 'id'; check that column/key exists"). It seems as though Django assumed that there was an id field for the model when there wasn't because I was able to use phpMyAdmin to make an id field, and when I re-ran the migration it succeeded. While I was able to fix my problem I doubt that this is the best way to go about it. What is the correct way to deal with this problem?

推荐答案

您可以伪造一个迁移仅包括在迁移时使用--fake选项删除"id"字段,因此django认为它在实际上不存在时已删除了id字段.

You could fake a migration only including removing the "id" field using the --fake option when migrating, so django thinks that it had deleted the id field when really it never existed.

这篇关于Django错误地期望id列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆