我只是PythonDjango的新手。。。 我创建了一些这样的模型:
class Question(models.Model):
question_text=models.CharField(max_length=200)
pub_date=models.DateTimeField("DatePublished")
class Choice(models.Model):
question=models.ForeignKey(Question,on_delete=models.CASCADE )
choise_text=models.CharField(max_length=200)
votes=models.IntegerField(default=0)
class Question(models.Model):
question_text=models.CharField(max_length=200)
pub_date=models.DateTimeField("DatePublished")
class Choice(models.Model):
question=models.ForeignKey(Question,on_delete=models.CASCADE )
choise_text=models.CharField(max_length=200)
votes=models.IntegerField(default=0)
在pycharm终端上运行了两个命令:
python manage.py makemigrations polls
python manage.py sqlmigrate polls 0001
然后我看到一些这样的输出:“开始
--
-- Create model Choice
--
CREATE TABLE "polls_choice" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "choise_text" varchar(200) NOT NULL, "votes" integer N
OT NULL);
--
-- Create model Question
--
CREATE TABLE "polls_question" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "question_text" varchar(200) NOT NULL, "pub_date" da
tetime NOT NULL);
--
-- Add field question to choice
--
ALTER TABLE "polls_choice" RENAME TO "polls_choice__old";
CREATE TABLE "polls_choice" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "choise_text" varchar(200) NOT NULL, "votes" integer N
OT NULL, "question_id" integer NOT NULL REFERENCES "polls_question" ("id"));
INSERT INTO "polls_choice" ("choise_text", "id", "question_id", "votes") SELECT "choise_text", "id", NULL, "votes" FROM "polls_choic
e__old";
DROP TABLE "polls_choice__old";
CREATE INDEX "polls_choice_7aa0f6ee" ON "polls_choice" ("question_id");
COMMIT;
`
就像您看到的那样,它将名为polls_choice
的表重命名为polls_choice_old
!但为什么呢?
这意味着数据库中可能有同名的表!!好吧,但为什么Django把它改名了?有人能告诉我,为什么会有这样的逻辑吗?你知道吗
sqlite中的
ALTER TABLE
命令是有限的(more details here)。Django通过重命名表、创建新表、跨多个表复制数据,最后删除旧表来解决这个问题。你知道吗相关问题 更多 >
编程相关推荐