I am building an app which uses a mysql and a postgres database. I’m using postgres because of postgis as this app is location based. Postgres only has one database Places whereas mysql contains all the others. During development I set up a few associations amongst various models, including the Place model. But as I realized, because of the different databases, the Join queries for associations failed.
My idea is to move all my tables in the Postgres DB only. As such I don’t have any particular need for MySQL.
But migration to Postgres from MySQL is my last option. I was wondering if there is any way to make association amongst models sitting in different databases work seamlessly ?
Thanks
As a direct answer to your question, you can use the PostgreSQL Foreign Data Wrapper (FDW) capabilities to reference tables from other databases, including MySQL.
http://www.postgresql.org/docs/current/interactive/ddl-foreign-data.html
http://wiki.postgresql.org/wiki/Foreign_data_wrappers
http://www.postgresql.org/docs/current/interactive/sql-createserver.html
http://www.postgresql.org/docs/current/interactive/sql-createforeigntable.html
That said, the planner’s ability to optimize a query which joins a foreign table are severely limited. While simple queries against one foreign table should perform pretty well, if you try to exercise the real power of SQL with a complex declarative query, it could easily be orders of magnitude slower than if it were all on PostgreSQL.
My advice would be to move it all over to PostgreSQL if practicable. If that would throw off time-lines too badly, you might consider using FDWs as a temporary “bridge” to get up and running quickly, and move the tables over as you have opportunity or need, to optimize performance.