Home > Software design >  How do I preserve previous data when deleting new columns or deleting exists table in Django models?
How do I preserve previous data when deleting new columns or deleting exists table in Django models?

Time:11-03

I am developing an ERP with groups of developers and we need to preserve customers data when deleting existing columns or table for Dajngo models and DB.

For Example:

I added a column named columns1 and I gave the customer a release product of the System and then, but a week later I had to delete that new column but the customer have data stores in the column1 column, here how can I preserve data or solve this situation.

Another Example:

I have a new column name column2 with unique attr but here the customer have data, but I can not add new column with out allowed it to store the null data, but in this situation I do not want to allow the null data in column column2 and ether I can't put default attr because it has unique attr.

How to solve these things in Django.

CodePudding user response:

i think you need to add one Boolean field in the table field name delete if you want to delete the column don't delete it put delete field value to true. when you query add a filter with condition delete = false. i think this will work for 1st condition

CodePudding user response:

Data migrations will help you solve these. You will have to write scripts in migrations to ensure the data is in the desired state before enforcing constraints e.g check column2 to ensure the data is unique and proceed to add the unique constraint. Refer to this tutorial for more

CodePudding user response:

Usually in these case, you would need to backup your database to an sql file for example https://dev.mysql.com/doc/mysql-backup-excerpt/5.7/en/mysqldump-sql-format.html.

After the backup you can change database structure however you want without worrying about the loss of data(because you saved in the backup sql). If you want to rollback the old version you can just apply the old DB(remember to backup your latest version of database too!). Or if you want to reapply deleted column just check the backup db data.

  • Related