1

Assuming all databases of a rails 7.0.3 application are on the same server (be they replicas or not)

  • Where primary database (app_development) has a table shop
  • a secondary database (appusers_development) has a table role
  • there is a third database (applocal_development) that will need to connect as well.

If a migration is created in the secondary database, where

t.references :shop, index: true, null: false, foreign_key: true
t.references :role, index: true, null: false, foreign_key: true

this migration will not pass, as postgresql has no knowledge of the connection to the other database: ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: relation "shops" does not exist

Different users exist for main database deploy_root and replicas deploy_readonly.

It does not appear that this is set out in rails functionalities, thus how can it be accomplished with postgresql

1 Answer 1

1

There's no way to have a foreign key constraint (or any constraint) between databases (not in postgresql, and not in any engine AFAIK). The whole idea of a relational database is that the relations are contained within a (single) database. Anything outside of that (arguably other than something like a read replica) needs to be handled outside of that.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.