我的模型包含created_at
字段models.DateTimeField
。我想将它迁移到models.DateField
,因为不再需要时间了。我更改模式,运行makemigrations
,并将其应用到我的数据库。我愚蠢地认为数据会被迁移。不是这样,因为所有created_at
字段现在都是None
。
如何转换现有数据以使日期仍然可用?
以下是生成的迁移文件:
# -*- coding: utf-8 -*-
# Generated by Django 1.10.5 on 2017-03-01 15:07
from __future__ import unicode_literals
import datetime
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('aion', '0003_auto_20160125_0948'),
]
operations = [
migrations.AlterField(
model_name='assembly',
name='created_at',
field=models.DateField(default=datetime.date.today, verbose_name='date created'),
),
]
数据库是在SQLite上运行的测试数据库。真正的一个在MySQL上运行......
$ python manage.py sqlmigrate aion 0004_auto_20170301_1507
BEGIN;
--
-- Alter field created_at on assembly
--
ALTER TABLE "aion_assembly" RENAME TO "aion_assembly__old";
CREATE TABLE "aion_assembly" ("created_at" date NOT NULL, "description" text NOT NULL, "serial_number" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "status" varchar(32) NOT NULL, "manufacturer_serial_number" varchar(128) NULL, "supplier_reference" varchar(128) NULL, "sap" varchar(128) NULL, "location_number_field" varchar(128) NULL, "customer_equipment_reference" varchar(128) NULL, "configurator_id" integer NULL REFERENCES "aion_configurator" ("id"), "created_by_id" integer NOT NULL REFERENCES "auth_user" ("id"), "customer_id" integer NULL REFERENCES "organisations_customer" ("id"), "locality_id" integer NULL REFERENCES "aion_location" ("id"), "project_code_id" integer NULL REFERENCES "project_codes_projectcode" ("id"), "supplier_id" integer NULL REFERENCES "organisations_supplier" ("id"), "contained_in_id" integer NULL REFERENCES "aion_assembly" ("serial_number"));
INSERT INTO "aion_assembly" ("status", "customer_equipment_reference", "manufacturer_serial_number", "description", "supplier_reference", "created_at", "created_by_id", "project_code_id", "contained_in_id", "locality_id", "supplier_id", "location_number_field", "customer_id", "serial_number", "configurator_id", "sap") SELECT "status", "customer_equipment_reference", "manufacturer_serial_number", "description", "supplier_reference", "created_at", "created_by_id", "project_code_id", "contained_in_id", "locality_id", "supplier_id", "location_number_field", "customer_id", "serial_number", "configurator_id", "sap" FROM "aion_assembly__old";
DROP TABLE "aion_assembly__old";
CREATE INDEX "aion_assembly_cc6edc4a" ON "aion_assembly" ("configurator_id");
CREATE INDEX "aion_assembly_e93cb7eb" ON "aion_assembly" ("created_by_id");
CREATE INDEX "aion_assembly_cb24373b" ON "aion_assembly" ("customer_id");
CREATE INDEX "aion_assembly_7e3ea948" ON "aion_assembly" ("locality_id");
CREATE INDEX "aion_assembly_f56bb38b" ON "aion_assembly" ("project_code_id");
CREATE INDEX "aion_assembly_c5bcd634" ON "aion_assembly" ("supplier_id");
CREATE INDEX "aion_assembly_e71ddfe4" ON "aion_assembly" ("contained_in_id");
COMMIT;
答案 0 :(得分:1)
SQLite很棒,但它的行为并不总是与其他数据库相同。关于SQLite的可能相关事实包括:
正如您所看到的,在使用SQLite时,Django实际上会创建一个新表,并在您修改列时复制旧表中的数据。
我不确定SQLite在你的情况下会中断,但由于你在生产中使用MySQL,我建议在开发中使用MySQL。我们已经看到这种迁移在MySQL下可以正常工作。