SQL data migration (mysql->postgres) - surrogate keys and references

  • Thread starter Deleted member 43669
  • Start date
Status
Not open for further replies.
D

Deleted member 43669

Guest
Hi all,

We are rewriting a large Django application which uses a MySQL database, and the database schema needs to undergo major modifications. We're also porting the app to use PostgreSQL, for a couple of good reasons.

I've written a small script that allows me to dump the old MySQL tables into a PostgreSQL database, prepending their name with oldapp_*

We are starting to write some data migration scripts which are basically:

Code:
insert into foobar(c1, c2, ..., cn)
select c1, c2, ..., cn from oldapp_foobar where ...

Now, this app uses surrogate keys extensively (Django doesn't allow composite primary keys and encourages the use of surrogates heavily). For the first migration scripts I've tentatively skipped the surrogate keys from the insert/select, and thus PG generates new ids automatically.

This works, of course, but when migrating foreign keys this is *not* a good idea. I'm pondering several approaches, but I'd appreciate some input here.

Cheers,

Álex
 

S. Carton

Ars Scholae Palatinae
937
I've always found the natural_key() / get_by_natural_key(*args) Django serialization way of doing this to be clean and principled.

https://docs.djangoproject.com/en/dev/t ... tural-keys

It's basically a way to turn surrogate pks to into (possibly composite) primary keys on serialization, plus facilities to easily read them back into another Django installation.

So you add these methods to your models (taking care to preserve the semantics of your data model), and voilà, your data's representation is database-independent.
 
Status
Not open for further replies.