-1

I have some models and fk on them to others.

models.py

class ElementMessages(models.Model)
    element = models.ForeignKey(Element, on_delete=models.CASCADE)
    sender = models.ForeignKey(UserAccount, on_delete=models.SET_NULL, null=True)
    text = models.TextField(max_length=512, null=True)
    send_time = models.DateTimeField(auto_now_add=True)
    type = models.CharField(max_length=16, choices=MESSAGE_TYPES, default=SIMPLE)
    type_dialog = models.CharField(max_length=10, choices=DIALOG_TYPE, default=DIALOG_TALK)
    request = models.ForeignKey(ChatRequest, null=True, default=None, on_delete=models.CASCADE)
    post_work = models.ForeignKey(PostWork, null=True, default=None, on_delete=models.CASCADE)
    files = models.BooleanField(default=False)

class Element(models.Model):
    id = models.UUIDField(default=uuid.uuid4, editable=False, unique=True, primary_key=True)
    artist = models.ForeignKey(Artist, on_delete=models.CASCADE, related_name='chat_element', null=True, blank=True)
    customer = models.ForeignKey(Customer, on_delete=models.CASCADE, related_name='chat_element', null=True, blank=True)
    element = models.ForeignKey('projects.Element', null=False, on_delete=models.CASCADE, related_name='chat_element')

When I try to delete Element object, it raises this:

django.db.utils.IntegrityError: insert or update on table "chat_elementmessages" violates foreign key constraint "chat_elementmessages_element_id_672e2ba2_fk_chat_element_id"
DETAIL:  Key (element_id)=(87cdd8d7-47f0-4264-8aa7-ae21a8246fd8) is not present in table "chat_element".

But when I look at table in db, this key exists. How to fix that?

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
Ingwar
  • 98
  • 2
  • 7
  • Place your element model above the Element Messages model And i think the element field in Element is a foreign key to same model.Is it?,If yes. give models.ForeignKey('self',null=False,on_delete=models.CASCADE) – Mohit Harshan Mar 04 '19 at 05:05
  • 1
    You have the same related_name for 3 fields, can you give a unique name to them all and check again – Paritosh Wadhavane Mar 04 '19 at 05:12
  • 1
    Also try migrating in case you made an update to your db schema – Hybrid Mar 04 '19 at 05:23
  • @MohitHarshan Element model already above ElementMessages model. Here i placed like that just because I have many other models in this file. And no, in Element model field `element` it's not self fk. – Ingwar Mar 04 '19 at 06:10
  • @ParitoshWadhavane unfortunately, it doesn't work – Ingwar Mar 04 '19 at 06:14
  • @Hybrid if everything was so simple, i wouldn't write here. :) I do migrations every time i updating db schema. – Ingwar Mar 04 '19 at 06:16
  • 1
    Please show your code that performs insertion. And describe what `doesn't work` means in case of **fixing** duplicate `related_name`s to the **correct** ones. – Ivan Starostin Mar 04 '19 at 07:44

1 Answers1

0

As it turned out, problems were at Django pre_delete andpost_delete signals. They tried to refer to a non-existing object, that I'm try to delete. Fixed with simple check on the existence of the object.

Ingwar
  • 98
  • 2
  • 7