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("cities")
    .select("name, country_id")
    .eq("name", "Bali")
    .execute()
)

# Incorrect
response = (
    supabase.table("cities")
    .eq("name", "Bali")
    .select("name, country_id")
    .execute()
)

Chaining

response = (
    supabase.table("cities")
    .select("name, country_id")
    .gte("population", 1000)
    .lt("population", 10000)
    .execute()
)

Conditional chaining

filterByName = None
filterPopLow = 1000
filterPopHigh = 10000

query = supabase.table("cities").select("name, country_id")

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

if filterPopLow:
    query = query.gte("population", filterPopLow)

if filterPopHigh:
    query = query.lt("population", filterPopHigh)

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("countries")
    .select("name, cities!inner(name)")
    .eq("cities.name", "Bali")
    .execute()
)