Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add check for making an existing column NOT NULL #13

Open
eiriklied opened this issue Feb 16, 2023 · 0 comments
Open

Add check for making an existing column NOT NULL #13

eiriklied opened this issue Feb 16, 2023 · 0 comments

Comments

@eiriklied
Copy link

eiriklied commented Feb 16, 2023

In Postgres, setting NOT NULL on an existing column blocks reads and writes while every row is checked, which is problematic on large tables.

For reference, have a look here.

This might be a bit hard to check as the migration file will look something like below. But perhaps it makes sense to look at the generated SQL?

class Migration(migrations.Migration):

    operations = [
        migrations.AlterField(
            model_name='foo',
            name='field_name',
            field=models.ForeignKey(blank=True, on_delete=django.db.models.deletion.PROTECT, related_name='related_field_name', to='bar'),
        ),
    ]

While the generated SQL becomes like below (the interesting part is ALTER TABLE "foo" ALTER COLUMN "field_name_id" SET NOT NULL;)

BEGIN;
SET CONSTRAINTS "foo_field_name__f89f691c_fk_bar" IMMEDIATE; ALTER TABLE "foo" DROP CONSTRAINT "foo_field_name__f89f691c_fk_bar";
ALTER TABLE "foo" ALTER COLUMN "field_name_id" SET NOT NULL;
ALTER TABLE "foo" ADD CONSTRAINT "foo_field_name__f89f691c_fk_bar" FOREIGN KEY ("field_name_id") REFERENCES "bar" ("id") DEFERRABLE INITIALLY DEFERRED;
COMMIT;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant