【发布时间】:2019-12-04 23:58:06
【问题描述】:
最初,我有一个Credit 的模型。但是在添加了Hypothec 和AutoCredit 具有类似功能的模型之后,我意识到我需要制作一个基本模型并从中继承。
当我尝试 makemigrations 时,我收到了一个问题:
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
*我输入了 1; 1.然后我得到了这个问题:
不清楚'abstractbaseproduct_ptr'字段是什么?
然后我得到了
您正在尝试在没有默认值的情况下将不可为空的字段 abstractbaseaction_ptr 添加到 creditaction;我们不能这样做(数据库需要一些东西来填充现有的行)。
再次介绍1; 1.
当我尝试迁移时,我得到了
django.db.utils.IntegrityError:无法创建唯一索引“credits_credit_pkey” 详细信息:键 (abstractbaseproduct_ptr_id) = (1) 重复。
此类问题仅出现在 Credit 模型中。显然是因为有already data in this table...
我应该如何解决这个问题?
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'))
迁移
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,
),
【问题讨论】:
-
为此列设置
default=1 -
@shafik 代表哪一栏?
-
@bdoubleu 不起作用
-
abstractbaseproduct_ptr_id运行makemigrations时您收到了确认消息的哪一列
标签: django postgresql django-models