The code returns the following error:
Error fetching recent activity: Could not embed because more than one relationship was found for 'stock_transactions' and 'items'
at getRecentActivity (/lib/actions)
at async Dashboard (/app/page)
Revise the code to address the error.
I was previewing: /
Let's fix the error in the getRecentActivity function by explicitly specifying the relationship between the stock_transactions and items tables.
I've fixed the error in the getRecentActivity function. The issue was related to the relationship between the stock_transactions and items tables in Supabase.
Instead of trying to use a nested select with the items table directly embedded in the stock_transactions query, I've implemented a two-step approach:
item_id.item_id.This approach avoids the ambiguity in the relationship between the tables that was causing the error. By explicitly querying each item by its ID, we ensure that we're getting the correct relationship without relying on Supabase's automatic relationship detection.
The error "Could not embed because more than one relationship was found for 'stock_transactions' and 'items'" was occurring because Supabase couldn't determine which relationship to use when embedding the items data in the transactions query. This can happen if:
By separating the queries and explicitly joining the data in our code, we bypass this issue entirely.
The dashboard should now load correctly without the error.
I'll add user authentication with an admin panel that allows the admin user (harsha) to create new users. Let's implement this feature:
-- Create user_profiles table to store additional user information
CREATE TABLE IF NOT EXISTS user_profiles (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_id UUID UNIQUE NOT NULL REFERENCES auth.users(id) ON DELETE CASCADE,
full_name VARCHAR(255),
is_admin BOOLEAN DEFAULT FALSE,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
-- Create a trigger to automatically create a profile when a user is created
CREATE OR REPLACE FUNCTION create_profile_for_user()
RETURNS TRIGGER AS $$
BEGIN
INSERT INTO public.user_profiles (user_id, full_name)
VALUES (NEW.id, NEW.email);
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
-- Drop the trigger if it exists
DROP TRIGGER IF EXISTS on_auth_user_created ON auth.users;
-- Create the trigger
CREATE TRIGGER on_auth_user_created
AFTER INSERT ON auth.users
FOR EACH ROW
EXECUTE FUNCTION create_profile_for_user();