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

Resource embedding is not working on join tables using composite keys #1592

Closed
goteguru opened this issue Sep 25, 2020 · 1 comment
Closed
Labels
bug embedding resource embedding

Comments

@goteguru
Copy link
Contributor

Environment

  • PostgreSQL version: 12.4
  • PostgREST version: 7.0.1
  • Operating system: Debian 10.5

Description of issue

Expected: composite foreign keys and single column foreign are working consistently.
Actual: No resource embedding happens across join tables when the are composite keys in the join table.

Steps to reproduce:

CREATE SCHEMA api;

CREATE TABLE IF NOT EXISTS api.test_a(
 id1 int NOT NULL,
 id2 int NOT NULL,
 a VARCHAR(64) NOT NULL,
 PRIMARY KEY (id1, id2)
);

CREATE TABLE IF NOT EXISTS api.test_b(
 id1 int NOT NULL,
 id2 int NOT NULL,
 b VARCHAR(64) NOT NULL,
 PRIMARY KEY (id1, id2)
);

CREATE TABLE IF NOT EXISTS api.test_lnk (
 a1 INT NOT NULL,
 a2 INT NOT NULL,
 b1 INT NOT NULL,
 b2 INT NOT NULL,
 CONSTRAINT fk_test_a
   FOREIGN KEY (a1,a2)
   REFERENCES api.test_a(id1,id2),
 CONSTRAINT fk_test_b
   FOREIGN KEY (b1,b2)
   REFERENCES api.test_b(id1,id2)
) ;

In postgrest.conf:

db-uri = "postgres://system:mypasswordhere@localhost:5432/databasename"
db-schema = "api"
db-anon-role = "system"
db-pool = 10
db-pool-timeout = 10
server-host = "!4"
server-port = 3000

Running the resource embedding query fails:

curl 'http://localhost:3000/test_a?select=test_b(*)'
{"message":"Could not find foreign keys between these entities. No relationship found between test_a and test_b"}

However all of the following is working as expected:

curl 'http://localhost:3000/test_a?select=test_lnk(*)'
curl 'http://localhost:3000/test_b?select=test_lnk(*)'
curl 'http://localhost:3000/test_lnk?select=test_a(*)'
curl 'http://localhost:3000/test_lnk?select=test_b(*)'

Therefore it seems one level of resource embedding is working fine, but any indirect embedding fails.

If I change the primary keys and constraint from composite to single valued keys, everything works as expected.

@goteguru
Copy link
Contributor Author

Missing composite keys are pretty much showstopper for my project, therefore I've scanned the source and added a pull request #1593 to fix this issue. All tests are fine, and it seems to be working. Somebody more familiar with the project may comment why was it restricted in the first place.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug embedding resource embedding
Development

No branches or pull requests

2 participants