Skip to content
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

feat: add one to one relationship for resource embedding #2439

Merged
merged 1 commit into from
Aug 19, 2022

Conversation

steve-chavez
Copy link
Member

@steve-chavez steve-chavez commented Aug 19, 2022

Closes #1984. One to one relationships are detected when:

  • There's a foreign key which is also a primary key:
-- one-to-one rel between students and students_info
CREATE TABLE test.students(
  id int
, code text
, name text
, primary key(id, code)
);

CREATE TABLE test.students_info(
  id int
, code text
, address text
, primary key(id, code)
, foreign key (id, code) references test.students(id, code)
);
  • There's a foreign key with an unique constraint
-- one-to-one rel between country and capital
CREATE TABLE test.country(
  id int primary key
, name text
);

CREATE TABLE test.capital(
  id int primary key
, name text
, country_id int unique
, foreign key (country_id) references test.country(id)
);

(There's a blog post by Vertabelo that makes this clearer)

This works across the board(tables, views, rpc, mutations, computed rels can override them). O2O rels are a refinement of M2O rels, so no news rels are added and there is no potential for new disambiguation conflicts.

BREAKING CHANGE: For the cases where one to one relationships are
detected, json objects will be returned instead of json arrays of length
1.

If you wish to override this behavior, you can use computed
relationships to return arrays again.

-- | Gets many-to-one relationships and one-to-one(O2O) relationships, which are a refinement of the many-to-one's
allM2OandO2ORels :: PgVersion -> Bool -> SQL.Statement () [Relationship]
allM2OandO2ORels pgVer =
Copy link
Member

Choose a reason for hiding this comment

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

Just to comment that this query runs on pg 15 beta 3 and builds correctly. Did not check the code, though.

@steve-chavez steve-chavez merged commit c45e85c into PostgREST:main Aug 19, 2022
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.

Detect one-to-one mappings in joins to avoid sub-arrays
2 participants