如何从一个表为另一个表创建两个外键?

时间:2019-05-21 01:32:14

标签: mysql django-models

我想为表a创建两个引用相同表b的外键。请帮助。

我正在使用Django 2.1.7:

class Subject_matter (models.Model):
    subject_matter_id = models.AutoField(primary_key=True)
    name_subject_matter = models.CharField(max_length=255)
    universitycareer = models.ForeignKey(Item_category, on_delete = models.CASCADE)

class Pre_requirements (models.Model):
    pre_requirements_id = models.AutoField(primary_key=True)
    subject_matter_id_1 = models.ForeignKey(Subject_matter, on_delete=models.CASCADE, null=True, related_name='subject_matter_id_1')
    subject_matter_requeriment_id = models.ForeignKey(Subject_matter, on_delete=models.CASCADE, null=True, related_name='subject_matter_requeriment_id')

终端中的消息错误:

django.db.utils.IntegrityError: (1215, 'Cannot add foreign key constraint')

0 个答案:

没有答案