Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
499 views
in Technique[技术] by (71.8m points)

postgresql - Rails and Existing Postgres Database

What are the best practices in case you want to build a Rails project that uses an existing Database?

in my case the postgres database living on a remote machine that runs a docker instance with postgres has a database with a table stake_address.

Now I have created a model in Rail:

class StakeAddress < ApplicationRecord
end

in rails console:

2.6.1 :001 > StakeAddress.all
Traceback (most recent call last):
ActiveRecord::StatementInvalid (PG::UndefinedTable: ERROR:  relation "stake_addresses" does not exist)
LINE 1: SELECT "stake_addresses".* FROM "stake_addresses" LIMIT $1
                                        ^

is there a way to avoid Rails to look for a pluralised table? I suppose there are going to be many other problems like this, so I was wondering if this is common practice, or if is something out of the world that is highly recommended to avoid?

Also I guess I don't need migrations, because the database is already created. Is that going to cause problems down the line?

My idea is that I will be able to connect to two databases, one read-only already existing and the other one where I can put my own models as Users. But will they be able to create association cross databases?

Perhaps Rails isn't the best framework for this type of work? Is what I am learning during a bootcamp, but perhaps I should switch to more flexible environments?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If there are not too many tables, I would also recommend what @eyeslandic suggests and manually override the table names in the models that need it.

self.table_name = 'stake_address'

If there are many tables, you’d be better off in the long run if you renamed them. You can do this safely in production with something like LHM.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...