10

I have the following models:

class User(models.Model):
      user_question = models.ForeignKey(UserQuestion)

class Question(models.Model):
      text = models.CharField(max_length=255)

class UserQuestion(models.Model):
      answer = models.CharField(max_length=255)
      question = models.ForeignKey(Question)
      user = models.ForeignKey(User, related_name='questions')

When I run the query below the user model is also deleted

user.questions.all().delete()

Is there any way to delete the questions without deleting the user?

I tried iterating over the questions and that didn't work

questions = user.questions.all()
for an in questions:
     answer.delete()

I thought the queryset was lazy so maybe I needed to evaluate it before deleting so I printed it and this did not work.

print questions
questions.delete()

I know that making the ForeignKey nullable would provide me with methods like clear and remove but I did not want to do this because I did not want any orphaned user questions.

I updated the ForeignKey as follows

class UserQuestion(models.Model):
  answer = models.CharField(max_length=255)
  user = models.ForeignKey(User, related_name='questions', null=True, on_delete=models.SET_NULL)

I ran makemigrations and migrate but when I ran the query below The question model was still deleted.

user.questions.all().delete()
Crystal
  • 5,960
  • 4
  • 21
  • 27

1 Answers1

5
question = models.ForeignKey(Question, related_name='answers', on_delete=models.SET_NULL, null=True)

untested, but should work.

Your issue is a DB related issue, when deleting a foreignkey the DB will try to delete the related row.

Read more on the other options on this great answer.

Community
  • 1
  • 1
Or Duan
  • 13,142
  • 6
  • 60
  • 65
  • I tried this and it didn't work for me. I think models.SET_NULL requires the field to be nullable and on_delete only seems to refer to one side of the relationship. So when I delete a question I can keep the answers but my problem is that when I delete the answers I want to keep the question. – Crystal Feb 09 '17 at 22:28
  • deleting the question and keeping the answer is the default behavior, I forgot that the field need a `null=True`, updated the answer. This make sense because, when deleting an answer the foreign key field should be null – Or Duan Feb 10 '17 at 06:56
  • Also don't forget to run `makemigration` and `migrate` – Or Duan Feb 10 '17 at 06:57
  • I really didnt want to set the field to be nullable because I didn't like the idea of orphaned answers (because answers mean nothing without a question) but I set null=True and on_delete=models.SET_NULL. The result was still that the question was deleted. I made sure to run makemigrations and migrate – Crystal Feb 10 '17 at 20:06
  • Your answer worked for me. I wasn't working with question and answer models those were just an analogy for more complex model that I was working on. I was looking at the wrong relationship and thats why my models were getting deleted. – Crystal Feb 20 '17 at 16:00