在我的项目中,我有自定义用户模型。当我尝试为用户模型创建外键时,迁移失败并带有
pymysql.err.InternalError: (1005, 'Can\'t create table `coins`.`#sql-165_8f` (errno: 150 "Foreign key constraint is incorrectly formed")')
直到这一刻,一切都很好。
现在这个模型代码不起作用
class CoQuestion(models.Model):
id = models.AutoField(primary_key=True)
text = models.TextField(verbose_name=_('text'))
co_user = models.ForeignKey('custom_auth.CoinUser', verbose_name=_('user'))
class Meta:
managed = True
db_table = 'co_question'
verbose_name = _('question')
verbose_name_plural = _('questions')
但是这是工作
class CoBets(models.Model):
"""
Describes a model of bets
"""
id = models.AutoField(primary_key=True)
co_user = models.ForeignKey('custom_auth.CoinUser', verbose_name=_('user'))
co_lot = models.ForeignKey('CoLot', verbose_name=_('lot'), related_name='bets')
size = models.DecimalField(max_digits=10, decimal_places=2, verbose_name=_('size'),
validators=[MinValueValidator(Decimal('0.01'))],
default=0.01)
dt = models.DateTimeField(verbose_name=_('date'),
default=datetime(2014, 12, 12, 15, 44, 37, 856544))
co_rejection = models.ForeignKey('CoRejection', verbose_name=_('rejection'),
null=True, blank=True)
class Meta:
managed = True
db_table = 'co_bets'
verbose_name = _('bet')
verbose_name_plural = _('bets')
这是创建第一个模型的迁移
migrations.CreateModel(
name='CoQuestion',
fields=[
('id', models.AutoField(serialize=False, primary_key=True)),
('text', models.TextField(verbose_name='text')),
('co_user', models.ForeignKey(verbose_name='user', to=settings.AUTH_USER_MODEL)),
],
options={
'verbose_name': 'question',
'db_table': 'co_question',
'verbose_name_plural': 'questions',
'managed': True,
},
bases=(models.Model,),
),
这是第二个模型的添加字段 migrations.AddField( MODEL_NAME =' cobets&#39 ;, 名称=' co_user&#39 ;, 字段= models.ForeignKey(至= settings.AUTH_USER_MODEL), preserve_default =真, ),