Skip to content

Views with multi-schema joins in postgres  #10

@mcls

Description

@mcls

I have an issue when defining views with joins between two postgres schemas (e.g. public and client_a).

Suppose we have this view definition:

CREATE VIEW my_view AS SELECT plans.name, clients.id
FROM clients 
JOIN public.plans ON plans.id = clients.plan_id

Then then the following SQL is stored in db/schema.rb (notice missing public prefix for plans):

SELECT plans.name, clients.id
FROM clients 
JOIN plans ON plans.id = clients.plan_id

I'm using this in combination with the apartment gem which loads schema.rb file for every tenant it creates. It also creates all tables for every schema/tenant, even if they are only used in the public schema.

The query to get the views:

SELECT pg_get_viewdef(oid)
FROM pg_class
WHERE relkind = 'v'
AND relname = '#{view_name}'

Not sure how to proceed yet. The views generated by migrations are correct.
Basically I want the "exluded model tables" from apartment to be prefixed with public in the db/schema.rb while the others use search_path.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions