I had some concerns about breaking Rails table relationships (:has_many,
:has_and_belongs_to_many, :belongs_to, :acts_as_*, etc) when moving
multiple
databases (using the same application) into one.
I seem to be stuck when trying to wrap my head around a good database
design
for what I would like to do or if I am just completely not thinking
straight. I guess this isn’t related specifically to Rails but to overal
DB
design.
So, for example I have two sites on two different databases - same
application. I want to consolidate and move Site B into Site A’s
database.
When I move it, the primary key column could get messed up and break
relationships with other tables. I think.
SITE A)
table “pages”
id, name, account_id
1,“Page One”, 55
2,“Page Two”, 55
3,“Page Three”, 55
SITE B)
table “pages”
id, name, account_id
1,“My First Page”, 453
2,“My Second Page”, 453
3,“My Third Page”, 453
When I import the rows from Site B to Site A, I get the following:
1,“Page One”, 55
2,“Page Two”, 55
3,“Page Three”, 55
4,“My First Page”, 453
5,“My Second Page”, 453
6,“My Third Page”, 453
The problem being the ids 4,5 and 6 which just pick up the
autoincrementing
column and if this pages table :has_many “files” then in that “files”
table’s “page_id” column will be pointing to the wrong row in the
“pages”
table.
Another thing to throw into the mix is that I build my URLs based on the
ID
column. Now, I heard on a podcast somewhere that it’s generally not a
good
idea to expose the IDs of your tables but I didn’t fully understand why
So, if site B had a public URL such as http://www.siteA.com/pages/view/1
and
the database moves to the new location than it won’t find “My First
Page”
… it will find Site A’s “Page One”.
Any best practices I am missing out on? Basically I am trying to
consolidate
my sites into using one database. But taking and restoring backups seems
to
be a nightmare.
Jeff