Django creates a migration that seems already reflected in original postgresql schema

I've modified the foreign key calendar as nullable in my Django model CalendarAssign. \

# ---------------------------------------------------------------------------- #
class Calendars(models.Model):
  id = models.CharField(primary_key=True, max_length=100)
  cms_id = models.CharField(max_length=100)
  default_program = models.ForeignKey(ControlPrograms, models.CASCADE, blank=True, null=True)
  timestamp = models.DateTimeField(auto_now_add=True)

  class Meta:
    managed = True
    db_table = 'calendars'

# ---------------------------------------------------------------------------- #
class CalendarAssign(models.Model):
  device_mac = models.ForeignKey(Device, models.CASCADE)
  calendar = models.ForeignKey(Calendars, models.CASCADE, null=True)
  timestamp = models.DateTimeField(auto_now_add=True)

  class Meta:
    managed = True
    db_table = 'calendar_assign'

When applying the migration generated by Django it gives me an error.

    operations = [
        migrations.AlterField(
            model_name='calendarassign',
            name='calendar',
            field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='smartbridge.Calendars'),
        )

Generated sql code uses unsupported feature 'WITH ORDINALITY'. It's because Django doesn't support the Postrges version we are using.
WITH ORDINALITY appears in psql 9.4 but we use version 9.1.
Both Postgres and Django cannot be upgraded right now. So I need to write the migration manually (without 'WITH ORDINALITY' feature).

        migrations.RunSQL("DO $$DECLARE r record;\
                            BEGIN\
                                FOR r IN SELECT table_name,constraint_name \
                                    FROM information_schema.constraint_table_usage \
                                    WHERE table_name IN ('calendars') AND constraint_name like '%calendar_assign_calendar_id%'\
                                LOOP\
                                    EXECUTE 'ALTER TABLE calendar_assign DROP CONSTRAINT '|| quote_ident(r.constraint_name) || ';';\
                                END LOOP;\
                            ALTER TABLE calendar_assign ALTER COLUMN calendar_id DROP NOT NULL; \
                            ALTER TABLE calendar_assign \
                            ADD CONSTRAINT calendar_assign_calendar_id_fk_calendars_id FOREIGN KEY (calendar_id) REFERENCES calendars(id);\
                            END$$;")

Migration seems to work fine. calendar is now nullable but Django still detect some difference.
If a ask Django to generate the migration corresponding to the difference it generates the same as before my manual migration.

I would like Django to see no difference after my migration.

Thanks

I think you will have to set managed = False for the time being, otherwise the makemigrations command will each time think it has not been made nullable yet.

The migration construction command looks to the previous migration files, and thus constructs a conceptual model how a table will look like in the database if all the previous migrations took place. Based on that model it will look for differences with your Django model that you constructed, and thus create a migration file for that.

As long as you thus do not migrate with the AlterField command, Django will think you did not make the field nullable. It can not parse SQL so even if you made it nullable over there, it will still assume that that the field is non-NULLable.

By setting it to managed=False [Django-doc], Django will no longer manage the migrations of that file. You can create an empty migration [Django-doc] with:

python3 manage.py makemigrations --empty

and use this to define SQL queries to perform on the table.

Back to Top