Changing Django database backend from MySql to Pos

2020-05-29 03:09发布

I use Django 1.2 and 1.3 and MySql backends.

Once in while a get an error message when migrating my MySql database with South:

! Error found during real run of migration! Aborting.

! Since you have a database that does not support running
! schema-altering statements in transactions, we have had 
! to leave it in an interim state between migrations.
...
! The South developers regret this has happened, and would
! like to gently persuade you to consider a slightly
! easier-to-deal-with DBMS

I was wondering if migrating my databases from MySql to PostgreSQL might prevent this error. Secondly, would migrating from MySql to PostgreSQL be more involved dan doing a dumpdata on the MySql dbase, change the settings to point to PostgreSQL and the loaddata on the new backend?

I saw this stackoverflow question, but it talks about his database being too big. I don't think that will be the case with my databases. I don't have any custom SQL commands in my Django projects.

1条回答
不美不萌又怎样
2楼-- · 2020-05-29 03:32

I got tired of seeing this error using South and yes, switching to PostgreSQL has banished it!

The mysql2postgres app, written in Ruby, suggested in comments above didn't work for me (it would run, output some details to screen but not copy any rows of data, for me). Not sure why. But gladly there's a Python rewrite of it that worked flawlessly (for me, eventually):
http://pypi.python.org/pypi/py-mysql2pgsql

The only gotcha I found was:

Initially I thought it would be safest to set up the tables in the PostgreSQL db via a syncdb and then migrate the data only. I tried this, but the tables are migrated across in alphabetical order and this violates the foreign key constraints for some tables (rows relate to rows in tables not yet imported).

I next tried a structure+data migration. This migrated fine, but I encountered some problems in Django afterwards, especially the admin site. It seemed like the migration script had created some different table constraints from what Django would have.

I solved this by hacking the mysql2pgsql script to respect the order of tables given in the yaml config only_tables property... and then doing a syncdb + data-only migration. By trial and error I shuffled around the ordering of the tables for my migration until they all imported successfully.

UPDATE:
My pull request for the hack described above got accepted so you can do this now from the main version:
https://github.com/philipsoutham/py-mysql2pgsql

查看更多
登录 后发表回答