简体   繁体   中英

Django.db.utils.ProgrammingError: constraint does not exist

I'm trying to run Django migration in my project, but something is not working fine, and I couldn't figure out what could be happening.

The AuditableModelMixin entity is extended by almost all entities in my project and provides a couple of fields that are used for audit purpose. For all of than, the migrations is runing fine. Although, for the Contract model, the migration is creating the following two separated files:

0001_initial.py

class Migration(migrations.Migration):

    initial = True

    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
            name='Contract',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False, verbose_name='identifier')),
                ('created_at', models.DateTimeField(auto_now_add=True, verbose_name='created at')),
                ('updated_at', models.DateTimeField(auto_now=True, verbose_name='updated at')),
                ('name', models.CharField(max_length=100, unique=True, verbose_name='name')),
                ('description', models.CharField(blank=True, max_length=1024, null=True, verbose_name='description')),
                ('created_by', models.ForeignKey(blank=True, db_column='created_by', null=True, on_delete=django.db.models.deletion.PROTECT, related_name='contract_created_by', to=settings.AUTH_USER_MODEL, verbose_name='created by')),
            ],
            options={
                'verbose_name': 'contract',
                'verbose_name_plural': 'contracts',
                'db_table': 'sacv"."contract',
            },
        ),
    ]

0002_auto_20200524_1912.py (the sequence will change, that's ok)

class Migration(migrations.Migration):

    initial = True

    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        ('contract', '0001_initial'),
        ('jurisdiction', '0001_initial'),
        ('state', '0001_initial'),
    ]

    operations = [
        migrations.AddField(
            model_name='contract',
            name='jurisdictions',
            field=models.ManyToManyField(related_name='contracts', through='jurisdiction.Jurisdiction', to='state.State'),
        ),
        migrations.AddField(
            model_name='contract',
            name='updated_by',
            field=models.ForeignKey(blank=True, db_column='updated_by', null=True, on_delete=django.db.models.deletion.PROTECT, related_name='contract_updated_by', to=settings.AUTH_USER_MODEL, verbose_name='updated by'),
        ),
    ]

I don't know why, it is putting the updated_by into a separated file. It start happening after I have added the ManyToMany relationship into Contract model. With this new approach, when I run the python manage.py migrate , I recieve the folling error:

Traceback (most recent call last):
  File "/home/rodrigo/Workspace/fgv/sacv-p/src/backend/sacv/manage.py", line 21, in <module>
    main()
  File "/home/rodrigo/Workspace/fgv/sacv-p/src/backend/sacv/manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "/home/rodrigo/Workspace/fgv/sacv-p/src/backend/venv/lib/python3.8/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "/home/rodrigo/Workspace/fgv/sacv-p/src/backend/venv/lib/python3.8/site-packages/django/core/management/__init__.py", line 395, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/rodrigo/Workspace/fgv/sacv-p/src/backend/venv/lib/python3.8/site-packages/django/core/management/base.py", line 328, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/rodrigo/Workspace/fgv/sacv-p/src/backend/venv/lib/python3.8/site-packages/django/core/management/base.py", line 369, in execute
    output = self.handle(*args, **options)
  File "/home/rodrigo/Workspace/fgv/sacv-p/src/backend/venv/lib/python3.8/site-packages/django/core/management/base.py", line 83, in wrapped
    res = handle_func(*args, **kwargs)
  File "/home/rodrigo/Workspace/fgv/sacv-p/src/backend/venv/lib/python3.8/site-packages/django/core/management/commands/migrate.py", line 231, in handle
    post_migrate_state = executor.migrate(
  File "/home/rodrigo/Workspace/fgv/sacv-p/src/backend/venv/lib/python3.8/site-packages/django/db/migrations/executor.py", line 117, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "/home/rodrigo/Workspace/fgv/sacv-p/src/backend/venv/lib/python3.8/site-packages/django/db/migrations/executor.py", line 147, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "/home/rodrigo/Workspace/fgv/sacv-p/src/backend/venv/lib/python3.8/site-packages/django/db/migrations/executor.py", line 245, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/home/rodrigo/Workspace/fgv/sacv-p/src/backend/venv/lib/python3.8/site-packages/django/db/migrations/migration.py", line 124, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/home/rodrigo/Workspace/fgv/sacv-p/src/backend/venv/lib/python3.8/site-packages/django/db/migrations/operations/fields.py", line 110, in database_forwards
    schema_editor.add_field(
  File "/home/rodrigo/Workspace/fgv/sacv-p/src/backend/venv/lib/python3.8/site-packages/django/db/backends/base/schema.py", line 480, in add_field
    self.execute(sql, params)
  File "/home/rodrigo/Workspace/fgv/sacv-p/src/backend/venv/lib/python3.8/site-packages/django/db/backends/base/schema.py", line 142, in execute
    cursor.execute(sql, params)
  File "/home/rodrigo/Workspace/fgv/sacv-p/src/backend/venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 100, in execute
    return super().execute(sql, params)
  File "/home/rodrigo/Workspace/fgv/sacv-p/src/backend/venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 68, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/home/rodrigo/Workspace/fgv/sacv-p/src/backend/venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/home/rodrigo/Workspace/fgv/sacv-p/src/backend/venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 86, in _execute
    return self.cursor.execute(sql, params)
  File "/home/rodrigo/Workspace/fgv/sacv-p/src/backend/venv/lib/python3.8/site-packages/django/db/utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/home/rodrigo/Workspace/fgv/sacv-p/src/backend/venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 86, in _execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: constraint "contract_updated_by_4a62e48c_fk_account_id" does not exist

Error when trying create and run Django migrations!

Failed! The script execution has finished with errors!

If I change the migration file and put the updated_by into the first file, everything goes fine, but I don't believe that is the correct approach. Does someone has any clue about this problem?

The following code are the classes related with the problem:

Audit Class

class AuditableModelMixin(models.Model):
    created_at = models.DateTimeField(
        _('created at'),
        auto_now_add=True,
        editable=False
    )
    created_by = models.ForeignKey(
        'account.Account',
        verbose_name=_('created by'),
        db_column='created_by',
        related_name='%(class)s_created_by',
        null=True,
        blank=True,
        on_delete=models.PROTECT,
    )
    updated_at = models.DateTimeField(
        _('updated at'),
        auto_now=True,
    )
    updated_by = models.ForeignKey(
        'account.Account',
        verbose_name=_('updated by'),
        db_column='updated_by',
        related_name='%(class)s_updated_by',
        null=True,
        blank=True,
        on_delete=models.PROTECT,
    )

    class Meta:
        abstract = True
        ordering = ['-created_at']

Contract Class

class Contract(
    UUIDModelMixin,
    AuditableModelMixin
):
    jurisdictions = models.ManyToManyField(
        State,
        related_name='contracts',
        through='jurisdiction.Jurisdiction',
        through_fields=('contract', 'state')
    )
    name = models.CharField(_('name'), max_length=100, unique=True)
    description = models.CharField(
        _('description'),
        max_length=1024,
        blank=True,
        null=True
    )

    class Meta:
        db_table = 'sacv\".\"contract'
        verbose_name = _('contract')
        verbose_name_plural = _('contracts')

    def __str__(self):
        return self.description

Jurisdiction Class

class JurisdictionManager(models.Manager):
    def get_queryset(self):
        return super().get_queryset().filter(
            active=True, ended_at__isnull=True
        )


class Jurisdiction(
    UUIDModelMixin,
    EligibilityModelMixin,
    AuditableModelMixin
):
    contract = models.ForeignKey(
        Contract,
        verbose_name=_('contract'),
        on_delete=models.PROTECT
    )
    state = models.ForeignKey(
        State,
        verbose_name=_('state'),
        related_name="+",  # Disable the reverse relation for State
        on_delete=models.PROTECT
    )

    objects = JurisdictionManager()

    class Meta:
        db_table = 'sacv\".\"jurisdiction'
        verbose_name = 'jurisdiction'
        verbose_name_plural = 'jurisdictions'

    def __str__(self):
        return f"{self.contract.name}: {self.state}"

This problem was happening due a problem where the schema was not being propagated when creating the constraints. This has been fixed and now it works like a charm. For further information, please check the bug #31735

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM