0

So I am querying a profile table that has fk relations to 'recommendations' and 'playlist'. I want to get the profile info, recommendations of a certain type, and playlist entries marked active.

However, unless there are items that that fulfill both queries, the query will return zero rows. If there are recommendations of type 'rock', but no active playlist entries then I get zero rows.

What I want returned:

[{
name: 'Joe',
email: '[email protected]',
recommendations: [{id: 1, title: 'some title', ...}, {id: 2, title: 'other title', ...}],
playlist:[]
}]
const { data, error } = await supabase
 .from('profile')
 .select('''
    name,
    email,
    recommendations!inner(
        id,
        title,
        url,
        date,
        type
    ),
    playlist!inner(
      id,
      active,
      played,
      played_date
    )
    '''
    )
    .eq('playlist.active', true)
    .eq('recommendations.type', 'rock')

I looked at 'or' queries, but these are for queries filtering the same table.

2 Answers 2

0

or across related tables work on postgREST latest prelease, which is available on the supabase CLI. Try it like this:

const { data, error } = await supabase
 .from('profile')
 .select('''
    name,
    email,
    recommendations(
        id,
        title,
        url,
        date,
        type
    ),
    playlist(
      id,
      active,
      played,
      played_date
    )
    '''
    )
    .eq('playlist.active', true)
    .eq('recommendations.type', 'rock')
    .or('(playlist.not.is.null, recommendations.not.is.null)')
Sign up to request clarification or add additional context in comments.

Comments

0

Search will be done on the relative table column by adding the inner joining !inner on relative tables

const { data, error } = await supabase
 .from('profile')
 .select('''
    name,
    email,
    recommendations!inner(
        id,
        title,
        url,
        date,
        type
    ),
    playlist!inner(
      id,
      active,
      played,
      played_date
    )
    '''
    )
    .eq('playlist.active', true)
    .eq('recommendations.type', 'rock')
    .or('(playlist.not.is.null, recommendations.not.is.null)')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.