Opened 2 months ago

Last modified 8 weeks ago

#35787 new Bug

CharField migration with preserve_default=False keeps the DB default on Oracle and MySQL

Reported by: Václav Řehák Owned by:
Component: Migrations Version: 5.1
Severity: Normal Keywords: Oracle
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I have a following migration:

migrations.AlterField(
    model_name="account",
    name="aggregation_group",
    field=models.CharField(
        blank=True,
        default="",
        max_length=40,
    ),
    preserve_default=False,
)

for a CharField which was defined as null=True before. When I run sqlmigrate with Oracle backend, I get the following SQL:

ALTER TABLE "CORE_ACCOUNT" MODIFY "AGGREGATION_GROUP" DEFAULT '';
UPDATE "CORE_ACCOUNT" SET "AGGREGATION_GROUP" = '' WHERE "AGGREGATION_GROUP" IS NULL;
ALTER TABLE "CORE_ACCOUNT" MODIFY "AGGREGATION_GROUP" DEFAULT NULL;

Note the last command sets a DB default which I don't want.

When I run the same migration on Postgres backend, I get this SQL:

ALTER TABLE "core_account" ALTER COLUMN "aggregation_group" SET DEFAULT '';
UPDATE "core_account" SET "aggregation_group" = '' WHERE "aggregation_group" IS NULL; SET CONSTRAINTS ALL IMMEDIATE;
ALTER TABLE "core_account" ALTER COLUMN "aggregation_group" SET NOT NULL;
ALTER TABLE "core_account" ALTER COLUMN "aggregation_group" DROP DEFAULT;

with the default being properly dropped.

Change History (1)

comment:1 by Sarah Boyce, 8 weeks ago

Summary: CharField migration with preserve_default=False keeps the DB default on OracleCharField migration with preserve_default=False keeps the DB default on Oracle and MySQL
Triage Stage: UnreviewedAccepted

Thank you Václav
I think both Oracle and MySQL need investigating

Note: See TracTickets for help on using tickets.
Back to Top