-
-
Notifications
You must be signed in to change notification settings - Fork 579
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
Foreign Key Smart Tags on Types #2346
Comments
Sounds like a bug. Which version? |
Damn you are fast (on a Sunday no less)! I updated the comment while you were responding. |
Separately, a composite type of |
Stuck waiting in the airport 😉 |
Please check if the same issue still exists in 4.14 |
You could write a book full of great advice like this. :) I'll try switching to that structure and see what I get. EDIT: Will try updating first |
I have a problem. Currently my function returns a query:
If I change the function's query, from
How can I
|
Drop the Don’t close this issue if this solves it, it still sounds like a bug but likely one that’s fixed in V5 (but I want to check). |
Awesome (learned something new!) Unfortunately, I still have an issue. I neglected to mention a detail: my function is doing a recursive query, so it has the same basic query multiple times. One of them looks like this (going back to
As you can see, I'm stuck on JOIN-ing on the Do I need to change my signature to |
I tried adding I'll report back if I run into anything else, but hopefully I can fix the recursion and call it a day. Thanks so much, as always! |
Re-opening because I just noticed:
|
I've attempted to reproduce this, but the Here's the SQL I've used against a fresh new database: create table foo (id serial primary key, name text);
insert into foo (name) values ('Caroline'), ('Bob'), ('Alice'), ('Dave');
create type indexed_foo as (
id int,
index int
);
comment on type public.indexed_foo is E'@foreignKey (id) references foo(id)';
create function indexed_foos() returns setof indexed_foo as $$
select
foo.id,
row_number() over (order by name asc)
from foo;
$$ language sql stable;
create function indexed_foo(id int) returns indexed_foo as $$
select *
from indexed_foos() t
where t.id = indexed_foo.id;
$$ language sql stable; And then I ran PostGraphile v4.14 against it using the command line I then executed this query: {
indexedFoos {
nodes {
...IndexedFoo
}
}
indexedFoo(id: 4) {
...IndexedFoo
}
}
fragment IndexedFoo on IndexedFoo {
id
index
fooById {
id
name
}
}
and received these results: {
"data": {
"indexedFoos": {
"nodes": [
{
"id": 3,
"index": 1,
"fooById": {
"id": 3,
"name": "Alice"
}
},
{
"id": 2,
"index": 2,
"fooById": {
"id": 2,
"name": "Bob"
}
},
{
"id": 1,
"index": 3,
"fooById": {
"id": 1,
"name": "Caroline"
}
},
{
"id": 4,
"index": 4,
"fooById": {
"id": 4,
"name": "Dave"
}
}
]
},
"indexedFoo": {
"id": 4,
"index": 4,
"fooById": {
"id": 4,
"name": "Dave"
}
}
}
} I've defined both a function that returns a single record ( |
Summary
I have a function that returns a
indexed_foo
type, which comes from afoo
table.indexed_foo
results are just aSETOF foo
with an extraindex
column (JOIN-ed from another table), so aSELECT * FROM indexed_foo(5)
returns:My function sort of works, but in the API the returned
indexed_foo
results only have thefoo
columns (eg.foo.baz_id
), not their relations (eg. notfoo.baz.qux
). This makes sense, because whilefoo
has those relations,indexed_foo
doesn't.The
@foreignKey
documentation mentions that types are supported, but ...I wasn't clear what that meant exactly: isn't
@foreignKey
always one direction, from the thing being tagged, to its foreign table?So, I experimented, and added a
@foreignKey
to connect myindexed_foo
type tofoo
:Since each
indexed_foo
has onefoo_id
, I expected an API structure of:However, I instead got:
That works (my client can extract the single record from the array), but it confused me. Each
indexed_foo
only has oneid
, so shouldn't that be a one-to-one relationship in the API, not a one-to-many?Two (hopefully quick) questions:
COMMENT ON TYPE public.indexed_foo IS E'@foreignKey (id) references foo(id);
result inindexed_foo
getting an array childfooById
(ie. one-to-many) when eachindexed_foo
only has one ID?fooById
into a single-child endpointfoo
?Additional context
Not that it matters, but
foo
is actually therecommendation_groups
table I mentioned in a previous question. I've reduced it tofoo
here to keep the irrelevant details as abstract as possible, and focus on the API/relationship question.OS: Linux (relatively recent version)
Node: Latest (or like a minor version behind)
PostgreSQL: 14.15
Postgraphile: 4.12.11 (I swear I'm going to upgrade to 5 soon ...)
The text was updated successfully, but these errors were encountered: