Skip to content

Support foreign table relationships in PostgreSQL provider #1974

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
webb-ben opened this issue Mar 22, 2025 · 1 comment
Open

Support foreign table relationships in PostgreSQL provider #1974

webb-ben opened this issue Mar 22, 2025 · 1 comment
Labels
enhancement New feature or request

Comments

@webb-ben
Copy link
Member

webb-ben commented Mar 22, 2025

Is your feature request related to a problem? Please describe.
Currently, pygeoapi's PostgreSQL provider does not support querying across multiple tables using a configurable ForeignKey relationship. This limits users who need to join tables dynamically based on external configurations.

I propose adding support for external table relationships. At a minimum I would want this to support 1:1 and N:1 relationships. Use cases are an observations table with external relationships to a site table with geometry, a table providing additional parameter metadata, or context inclusion from a table of encompassed geometry.

Describe the solution you'd like
A user may want to join counties with states using counties.statefp = states.geoid. From some tinkering I am thinking the relationship could be expressed in the config.yml as follows:

    providers:
     - type: feature
        name: PostgreSQL
        data: ...
        id_field: geoid
        table: counties
        external_tables:
          states:
            foreign: statefp
            remote: geoid
            properties: [geoid, name, uri]

and provide a response like:

{
  "type": "Feature",
  "properties": {
    "countyfp": "001",
    "statefp": "01",
    "name": "Autauga",
    "census_profile": "https://data.census.gov/cedsci/profile?g=0500000US01001",
    "uri": "https://geoconnex.us/ref/counties/01001",
    "states": {
      "uri": "https://geoconnex.us/ref/states/01",
      "name": "Alabama",
      "geoid": "01"
    }
  },
  "id": "01001",
  "geometry": {},
  "prev": "01001",
  "next": "01003",
  "links": []
}

There are obviously a myriad of ways to format the configuration and the response but I would support something simple.

Describe alternatives you've considered
I am working with a Postgres database that will not support materialized views.

I understand that automap might be able to include these types of relationships for a postgres database with the table relation defined as a foreign key (https://www.postgresql.org/docs/current/tutorial-fk.html), but this type of relationship does not work well for N:1.

Additional context
Add any other context or screenshots about the feature request here.

@webb-ben webb-ben added the enhancement New feature or request label Mar 22, 2025
@Youssef-Harby
Copy link
Contributor

This will be super helpful.. thanks @webb-ben

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants