© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
SupabaseS
Supabase•3y ago•
119 replies
KrleDano

How do I join tables containing multiple complex relation?

I have two tables:
CREATE TYPE transaction_type_enum as ENUM ('expense', 'income');

DROP TABLE IF EXISTS category;
CREATE TABLE category (
  id SERIAL PRIMARY KEY,
  name VARCHAR(100) UNIQUE NOT NULL,
  type transaction_type_enum DEFAULT 'expense' NOT NULL,

  CONSTRAINT unique_category_type UNIQUE (id, type)
);

DROP TABLE IF EXISTS transaction;
CREATE TABLE transaction (
  id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
  label VARCHAR(128),
  amount INT NOT NULL,
  categoryid INT REFERENCES category(id),
  type transaction_type_enum DEFAULT 'expense' NOT NULL,
  
  FOREIGN KEY (categoryid, type) REFERENCES category(id, type)
    DEFERRABLE INITIALLY DEFERRED
);
CREATE TYPE transaction_type_enum as ENUM ('expense', 'income');

DROP TABLE IF EXISTS category;
CREATE TABLE category (
  id SERIAL PRIMARY KEY,
  name VARCHAR(100) UNIQUE NOT NULL,
  type transaction_type_enum DEFAULT 'expense' NOT NULL,

  CONSTRAINT unique_category_type UNIQUE (id, type)
);

DROP TABLE IF EXISTS transaction;
CREATE TABLE transaction (
  id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
  label VARCHAR(128),
  amount INT NOT NULL,
  categoryid INT REFERENCES category(id),
  type transaction_type_enum DEFAULT 'expense' NOT NULL,
  
  FOREIGN KEY (categoryid, type) REFERENCES category(id, type)
    DEFERRABLE INITIALLY DEFERRED
);

How do I join these 2 tables using Javascript SDK?

Notice: there are two relation
transaction.categoryid
transaction.categoryid
-->
category.id
category.id
and
(transaction.categoryid, transaction.type)
(transaction.categoryid, transaction.type)
-->
(category.id, category.type)
(category.id, category.type)


I have attached the data with the this this as an image.
image.png
Supabase banner
SupabaseJoin
Supabase gives you the tools, documentation, and community that makes managing databases, authentication, and backend infrastructure a lot less overwhelming.
45,816Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements
Next page

Similar Threads

Model Complex Join Tables
SupabaseSSupabase / help-and-questions
3y ago
How do I join tables that don't have a foreign key relationship with JS
SupabaseSSupabase / help-and-questions
3y ago
How do I write a simple join for these tables in v2
SupabaseSSupabase / help-and-questions
4y ago
How can I import multiple CSV tables into Supabase while maintaining the relational fields?
SupabaseSSupabase / help-and-questions
14mo ago