0
I’m having a problem deleting 2 Foreign Keys from a Foreign key. I’ll explain further:
This is the hierarchy of the credit card model I have:
- Creditcard
- Customer
- Address
- Phone
I’m trying to delete the address and phone, right after Customer and creditcard. You’re saying I can’t change to null a Foreign key address and phone of the model Customer.
(1048, "Column 'phone_id' cannot be null")
He is trying to update the phone_id and address_id column by setting it to NULL. The error was this, the operation is not allowed. Then assign null=True, blank=True
in the model of Customer and the error continues.
I tried that way:
_credit_card.customer.phone.delete()
_credit_card.customer.address.delete()
_credit_card.customer.delete()
_credit_card.delete()
Python 3, Django 1.10
Edit your question and put here your code
models.py
for these tables. Check if you ran the commandmakemigrations
andmigrate
after the amendments.– Paulo
The variables of the models containing these
ForeignKey
s contains CASCADE in on_delete?models.ForeignKey('self', on_delete=models.CASCADE)
? https://docs.djangoproject.com/en/1.11/ref/models/fields/– Leonardo Pessoa
@Orion made the makemigrations and makemigrations and migrate yes. The models are exactly as shown in the hierarchy. Two models.Foreignkey of Useraddress and Userphone. This all within the Customer model which is a Foreign key of Creditcard.
– Guilherme IA
You have not entered the full model code. If you have not placed the
on_delete=models.CASCADE
on your model’s FK, it will not call the cascade function and may generate your error.– Leonardo Pessoa
I put the
on_delete=models.CASCADE
. I’ve already set the error. The real problem is that I was linking the address and phone inside the system. In fact I needed to have a Costomer inside these 2 tables. Oh yes, deleting Customer, I would delete the 2 tables too.– Guilherme IA