Skip to content

postgres connector: multiple relations between same tables#1770

Merged
tomhoule merged 1 commit intomainfrom
gb-6878-postgres-connector-implement-table-with-multiple-relations
Jun 13, 2024
Merged

postgres connector: multiple relations between same tables#1770
tomhoule merged 1 commit intomainfrom
gb-6878-postgres-connector-implement-table-with-multiple-relations

Conversation

@tomhoule
Copy link
Copy Markdown
Contributor

Given a table with two foreign keys referencing the same table, for example:

CREATE TABLE public.colors (
    id INT PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
    name VARCHAR(255) NOT NULL UNIQUE,
    rgb INT NOT NULL
);

CREATE TABLE public.users (
    id INT PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
    name VARCHAR(255) NOT NULL UNIQUE,
    favorite_color_id INT NOT NULL REFERENCES colors(id),
    least_favorite_color_id INT REFERENCES colors(id)
);

Previously, we would have only a colors field on the users type, using one of the two relations at random.

This commit changes the connector so we have two fields: colorsByFavoriteColorId and colorsByLeastFavoriteColorId. The fields on colors that walk the relation in the other direction are also adapted.

closes GB-6878

@linear
Copy link
Copy Markdown

linear bot commented Jun 12, 2024

/// True, if the referenced row is unique, this means there can only be at most one related row.
pub fn is_referenced_row_unique(self) -> bool {
/// True, if the referenced column(s) is (are) unique, this means there can only be at most one row on the other side of the relation.
pub fn is_other_side_one(self) -> bool {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renaming and rewriting the docstring because this is really not about the foreign key. Maybe other_side_is_one() would be a better name?

@tomhoule tomhoule force-pushed the gb-6878-postgres-connector-implement-table-with-multiple-relations branch from 689047d to 9ec09a8 Compare June 12, 2024 11:44
Given a table with two foreign keys referencing the same table, for example:

```sql
CREATE TABLE public.colors (
    id INT PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
    name VARCHAR(255) NOT NULL UNIQUE,
    rgb INT NOT NULL
);

CREATE TABLE public.users (
    id INT PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
    name VARCHAR(255) NOT NULL UNIQUE,
    favorite_color_id INT NOT NULL REFERENCES colors(id),
    least_favorite_color_id INT REFERENCES colors(id)
);
```

Previously, we would have only a `colors` field on the `users` type, using
one of the two relations at random.

This commit changes the connector so we have two fields:
`colorsByFavoriteColorId` and `colorsByLeastFavoriteColorId`. The fields
on `colors` that walk the relation in the other direction are also adapted.

closes GB-6878
@tomhoule tomhoule force-pushed the gb-6878-postgres-connector-implement-table-with-multiple-relations branch from 9ec09a8 to 8a0b192 Compare June 12, 2024 13:30
@tomhoule tomhoule marked this pull request as ready for review June 12, 2024 14:10
@tomhoule tomhoule requested a review from a team as a code owner June 12, 2024 14:10
@tomhoule tomhoule requested a review from hfgbarrigas June 12, 2024 14:10
@tomhoule tomhoule merged commit 4f12769 into main Jun 13, 2024
@tomhoule tomhoule deleted the gb-6878-postgres-connector-implement-table-with-multiple-relations branch June 13, 2024 12:08
tomhoule added a commit that referenced this pull request Jun 13, 2024
Features

- You can now filter on text columns with the LIKE operator on the postgres connector (https://www.postgresql.org/docs/current/functions-matching.html). #1764
- The postgres connector now uses a connection pool instead of a single connection in `grafbase start`. #1771
- The postgres connector now supports multiple relations between two tables. See the pull request (#1770) for an example.
- Fixed a bug with enum values sent to subgraphs in `grafbase dev` for federated graphs. #1766
@tomhoule tomhoule mentioned this pull request Jun 13, 2024
tomhoule added a commit that referenced this pull request Jun 13, 2024
Features

- You can now filter on text columns with the LIKE operator on the postgres connector (https://www.postgresql.org/docs/current/functions-matching.html). #1764
- The postgres connector now uses a connection pool instead of a single connection in `grafbase start`. #1771
- The postgres connector now supports multiple relations between two tables. See the pull request (#1770) for an example.
- Fixed a bug with enum values sent to subgraphs in `grafbase dev` for federated graphs. #1766
- Expand the scope of files watched for changes in `grafbase dev`. #1774
tomhoule added a commit that referenced this pull request Jun 13, 2024
Features

- You can now filter on text columns with the LIKE operator on the postgres connector (https://www.postgresql.org/docs/current/functions-matching.html). #1764
- The postgres connector now uses a connection pool instead of a single connection in `grafbase start`. #1771
- The postgres connector now supports multiple relations between two tables. See the pull request (#1770) for an example.
- Fixed a bug with enum values sent to subgraphs in `grafbase dev` for federated graphs. #1766
- Expand the scope of files watched for changes in `grafbase dev`. #1774
tomhoule added a commit that referenced this pull request Jun 13, 2024
Features

- You can now filter on text columns with the LIKE operator on the
postgres connector
(https://www.postgresql.org/docs/current/functions-matching.html). #1764
- The postgres connector now uses a connection pool instead of a single
connection in `grafbase start`. #1771
- The postgres connector now supports multiple relations between two
tables. See the pull request
(#1770) for an example.
- Fixed a bug with enum values sent to subgraphs in `grafbase dev` for
federated graphs. #1766
- Expand the scope of files watched for changes in `grafbase dev`. #1774
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants