PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, April 13, 2022

[FIXED] how to migrate django database after model change?

 April 13, 2022     django, migration, model     No comments   

Issue

I have model M1. it had a field F1 of type IntegerField. I created a number of items and filled their F1 with numbers.

Then I created new model M2 and changed F1 to be ForeignKey to M2. makemigrations worked fine. But migrate doesn't work - since it can't find M2 items corresponding to old F1 int values. Moreover I can't delete M1 items through shell - attempt to get Objects.all() result in same error.

How can I delete "problematic" items of M1 without flushing database?


Solution

How can I delete "problematic" items of M1 without flushing database?

I would advise to do this as part of the migration, using the RunPython operation. I.e. you should manually edit the migration that Django generated and insert a RunPython operation that deletes these "problematic" objects, before F1 changes field type 🙂



Answered By - Dabble
Answer Checked By - Timothy Miller (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing