dimanche 29 mars 2015

Update database schema while preserving data (and relationships)

I have a simple database schema containing 5 tables and a few foreign key relationships. Now I want to update the schema without losing the relationships.


SQLite does not support the operation I want to perform. I want to mark a column AUTOINCREMENT. So I searched and found that I need to recreate the tables.


In all the relationships, foreign key is the auto-generated row ID. This will change when I re-insert the data (right?). And the best case scenario would be having mismatched relationships. The worst case (I think) would be having foreign keys that correspond to nothing. How do I avoid this?


One way that I can think of doing this is through dumping all data into a special model designed for this very exercise. This special model will encapsulate all the relationships. Then I can start creating new tables in order of ascending dependence, from least to most. Table that has no foreign keys (but other tables use its ID) goes in first. And on inserting rows, I update the model instance.


Is there a better way of doing this? Thank you for reading.


Aucun commentaire:

Enregistrer un commentaire