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

Time:10-24

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 '           
  • Related