Python: Using filters

Filters allow you to only return rows that match certain conditions.

Filters can be used on select(), update(), upsert(), and delete() queries.

If a Postgres function returns a table response, you can also apply filters.

Examples

Applying Filters

# Correct
response = (
    supabase.table("instruments")
    .select("name, section_id")
    .eq("name", "flute")
    .execute()
)

# Incorrect
response = (
    supabase.table("instruments")
    .eq("name", "flute")
    .select("name, section_id")
    .execute()
)

Chaining

response = (
    supabase.table("instruments")
    .select("name, section_id")
    .gte("octave_range", 3)
    .lt("octave_range", 7)
    .execute()
)

Conditional chaining

filterByName = None
filterOctaveLow = 3
filterOctaveHigh = 7

query = supabase.table("instruments").select("name, section_id")

if filterByName:
    query = query.eq("name", filterByName)

if filterAgeLow:
    query = query.gte("octave_range", filterOctaveLow)

if filterAgeHigh:
    query = query.lt("octave_range", filterOctaveHigh)

response = query.execute()

Filter by values within JSON column

response = (
    supabase.table("users")
    .select("*")
    .eq("address->postcode", 90210)
    .execute()
)

Filter Foreign Tables

response = (
    supabase.table("orchestral_sections")
    .select("name, instruments!inner(name)")
    .eq("instruments.name", "flute")
    .execute()
)