gpt4 book ai didi

python - 从 DateTimeField 迁移到 DateField

转载 作者:太空宇宙 更新时间:2023-11-03 15:35:44 26 4
gpt4 key购买 nike

我有一个模型,其中包含 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;

最佳答案

SQLite 很神奇,但它的行为方式并不总是与其他数据库相同。关于 SQLite 的可能相关事实包括:

正如您所见,在使用 SQLite 时,Django 实际上会创建一个新表,并在修改列时从旧表复制数据。

我不太确定 SQLite 在您的情况下会在哪里出现问题,但由于您在生产中使用 MySQL,所以我建议在开发中也使用 MySQL。我们已经看到此迁移在 MySQL 下按预期工作。

关于python - 从 DateTimeField 迁移到 DateField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42534875/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com