开发者

Django Two foreign key

I have two models: UserProfile (extended from user) and Cv. I created another model that have two foreign key that come from theses models.

class cv(models.Model):
    user = models.ForeignKey(User, unique=True)
    cv_d= models.TextField(max_length=1100)
    ...
class cvv(models.Model):
    user = models.ForeignKey(User)    
    cv= models.ForeignKey(cv)    
    date = models.DateTimeField(auto_now=True)

In my view, I am trying to insert value on cvv:

...
obj = cv.objects.get(pk=id,active=True)
add=cvv(user=request.user, c开发者_如何学Gov=obj)    
add.save()

But, I am getting the following error: (1452, 'Cannot add or update a child row: a foreign key constraint fails

How can I insert theses 2 foreign key on my model?


Welcome to one of the many reasons why you shouldn't use MySQL. This happens most often when you have one table that is MyISAM and one table that is InnoDB. Since myISAM doesn't support FK constraints all hell breaks loose when django creates a FK between the tables.

The fix is to either make both tables InnoDB or MyISAM and not to mix them. Or even better drop the bad RDMS for something not MySQL.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜