0

Initially, I had a model of Credit. But after adding models of Hypothec and AutoCredit with similar functionality, I realized that I needed to make a base model and inherit from it.

When I tried makemigrations, I received a question:

You are trying to add a non-nullable field 'abstractbaseproduct_ptr' to `credit` without a default; we can't do that (the database needs something to populate existing rows).
Please select a fix:
 1) Provide a one-off default now (will be set on all existing rows)
 2) Quit, and let me add a default in models.py

*I entered 1; 1. Then I got this question:

It is not clear what the 'abstractbaseproduct_ptr' field is?

Then i got

You are trying to add a non-nullable field abstractbaseaction_ptr to creditaction without a default; we can't do that (the database needs something to populate existing rows).

Again introduced 1; 1.

When I try to migrate, I get

django.db.utils.IntegrityError: could not create unique index "credits_credit_pkey" DETAIL: Key (abstractbaseproduct_ptr_id) = (1) is duplicated.

Such questions arose only with the Credit model. Apparently, because there is already data in this table...

How should I fix this?

class AbstractBaseProduct(models.Model):
    bank = models.ForeignKey('banks.Bank', verbose_name=_('bank'))
    #other fields


class AbstractBaseAction(models.Model):
    name = models.CharField(_('name'), max_length=255)
    short_description = models.CharField(_('short description'), max_length=255)
    full_description = models.TextField(_('full description'), blank=True, null=True)


class Credit(AbstractBaseProduct):
    class Meta:
        verbose_name = _('credit')
        verbose_name_plural = _('Credits')


class CreditAction(AbstractBaseAction):
    credit = models.ForeignKey(Credit, verbose_name=_('credit'))

migration

migrations.AddField(
            model_name='credit',
            name='abstractbaseproduct_ptr',
            field=models.OneToOneField(auto_created=True, default=1, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='credits.AbstractBaseProduct'),
            preserve_default=False,
        ),
migrations.AddField(
            model_name='creditaction',
            name='abstractbaseaction_ptr',
            field=models.OneToOneField(auto_created=True, default=1, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='credits.AbstractBaseAction'),
            preserve_default=False,
        ),
Stevy
  • 3,228
  • 7
  • 22
  • 38
unknown
  • 252
  • 3
  • 12
  • 37

1 Answers1

1

You have named your model AbstractBaseProduct, but you haven't added abstract = True to the model's Meta class, so Django thinks you want multi-table inheritance instead of abstract base classes.

See the docs on model inheritance for more info.

Alasdair
  • 298,606
  • 55
  • 578
  • 516