Django IntegretyError,尝试更改ManyToMany字段的值时

时间:2019-04-12 11:49:56

标签: python django django-models django-orm

models.py

class BaseMedia(AbstractObject):
    staff = models.ManyToManyField('main.Profession', blank=True)
    # etc

class Profession(Id):
    profession = models.CharField(max_length=15)
    creator = models.ForeignKey('main.Creator', on_delete=models.CASCADE)
    character = models.ForeignKey('main.Character', on_delete=models.CASCADE, null=True, blank=True)

forms.py

for profession, staff in staff.items():
    for person in staff:
        professions.append(Profession.objects.update_or_create(
            profession=profession,
            creator_id=person.get('creator_id'),
            character_id=person.get('character_id')
        )[0])

instance.staff.set(professions)

instance.staff.set(professions)来自 forms.py 加薪:

django.db.utils.IntegrityError: update or delete on table "main_profession" violates 
foreign key constraint "main_basemedia_staff_profession_id_075458d2_fk_main_prof" on 
table "main_basemedia_staff"

DETAIL:  Key (id)=(dd032d79-d458-4ead-b05b-da3b419b58a0) is still referenced from table 
"main_basemedia_staff".

我绝对不知道这是麻烦所在。

0 个答案:

没有答案