"Column 'model_id' cannot be null" Django Cascade

Asked

Viewed 104 times

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 command makemigrations and migrate after the amendments.

  • The variables of the models containing these ForeignKeys contains CASCADE in on_delete? models.ForeignKey('self', on_delete=models.CASCADE)? https://docs.djangoproject.com/en/1.11/ref/models/fields/

  • @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.

  • 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.

  • 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.

1 answer

-1

I identified the problem. I had the 2 Foreign key (Userphone and Useraddress) inside the Customer table as Foreign key. Even using the on_delete=models.CASCADE wasn’t working. I was just taking these 2 Foreign key and creating the key Foreign CUSTOMER inside each of the tables that worked. Deleting Customer, I delete the 2 cascading tables.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.