Closed dns-developers closed 10 months ago
I wasn't sure on the direction your foreign key was supposed to go because the data in your example doesn't align fully in either direction but I think you're looking for
create table table_b (
color text not null,
size int not null,
description text not null,
primary key (color, size)
);
create table table_a (
id int primary key,
color text not null,
"size" int not null,
"type" text not null,
foreign key (color, "size") references table_b (color, "size")
);
insert into table_b(color, "size", "description")
values
('red', 1, 'aaaa'),
('red', 99, 'bbb'),
('blue', 15, 'ccc'),
('red', 12, 'ddd'),
('blue', 1, 'eee'),
('green', 99, 'fff');
insert into table_a(id, color, "size", "type")
values
(1, 'red', 12, '1111'),
(2, 'blue', 15, '2222'),
(3, 'green', 99, '3333'),
(4, 'blue', 15, '4444');
Which will allow you to use the default tableACollection
to perform your join.
{
tableACollection(first: 1) {
edges {
node {
id
color
size
type
tableB {
description
}
}
}
}
}
which returns
{
"data": {
"tableACollection": {
"edges": [
{
"node": {
"id": 1,
"size": 12,
"type": "1111",
"color": "red",
"tableB": {
"description": "ddd"
}
}
}
]
}
}
}
I'd recommend using ^ approach but if you need the data to be flat for whatever reason, you can use a view to do any arbitrary join logic and expose it in your API https://supabase.github.io/pg_graphql/views/
you can experiment with both approaches using GraphiQL built into Supabse Studio. See the docs for how to view that for your project
closing due to inactivity
I've got a situation where I'd like to be able to perform a join across two collections/tables where they match on two separate columns. I would like to query
table_a
joining ontable_b
and do not need to perform this join in the other direction. It's also worth noting perhaps that intable_b
, the two columns we are trying to match on are not individually unique but work together as a composite primary key.The SQL to perform this:
How can I go about sorting this type of query via GraphQL? Any tips?