Skip to content

Conversation

mariohd
Copy link
Contributor

@mariohd mariohd commented Aug 11, 2025

Problem

You can't filter by any plan attribute and have a search term.

Steps to reproduce

Add a filter to plan/subscription and a search term.
SubscriptionsQuery.call(organization:, filters: {overriden: true}, search_term: "Mario")

This would produce this query

Subscription Count (0.5ms)  SELECT COUNT(*) FROM (SELECT 1 AS one FROM "subscriptions" INNER JOIN "customers" ON "customers"."id" = "subscriptions"."customer_id" INNER JOIN "plans" "plan" ON "plan"."id" = "subscriptions"."plan_id" WHERE "subscriptions"."organization_id" = $1 AND ((((((((("subscriptions"."id"::varchar ILIKE '%Mario%' OR "subscriptions"."name" ILIKE '%Mario%') OR "subscriptions"."external_id" ILIKE '%Mario%') OR "plans"."name" ILIKE '%Mario%') OR "plans"."code" ILIKE '%Mario%') OR "customers"."name" ILIKE '%Mario%') OR "customers"."firstname" ILIKE '%Mario%') OR "customers"."lastname" ILIKE '%Mario%') OR "customers"."external_id" ILIKE '%Mario%') OR "customers"."email" ILIKE '%Mario%') AND "plan"."parent_id" IS NULL LIMIT $2 OFFSET $3) subquery_for_count  [["organization_id", "034de852-b25c-41c5-9f58-a8f07bdcd2bd"], ["LIMIT", 25], ["OFFSET", 0]]

but fails with

(lago-api):8:in '<main>': PG::UndefinedTable: ERROR:  invalid reference to FROM-clause entry for table "plans" (ActiveRecord::StatementInvalid)
LINE 1: ..."subscriptions"."external_id" ILIKE '%Mario%') OR "plans"."n...
                                                             ^
HINT:  Perhaps you meant to reference the table alias "plan".

Ramsack calls the table plans as plans and in our query, we are calling plan.

Fix

Call the plans table as plans just like Ramsack, without an alias.

SELECT COUNT(*) FROM (SELECT 1 AS one FROM "subscriptions" INNER JOIN "customers" ON "customers"."id" = "subscriptions"."customer_id" INNER JOIN "plans" ON "plans"."id" = "subscriptions"."plan_id" WHERE "subscriptions"."organization_id" = $1 AND ((((((((("subscriptions"."id"::varchar ILIKE '%Mario%' OR "subscriptions"."name" ILIKE '%Mario%') OR "subscriptions"."external_id" ILIKE '%Mario%') OR "plans"."name" ILIKE '%Mario%') OR "plans"."code" ILIKE '%Mario%') OR "customers"."name" ILIKE '%Mario%') OR "customers"."firstname" ILIKE '%Mario%') OR "customers"."lastname" ILIKE '%Mario%') OR "customers"."external_id" ILIKE '%Mario%') OR "customers"."email" ILIKE '%Mario%') AND "plans"."parent_id" IS NULL LIMIT $2 OFFSET $3) subquery_for_count  [["organization_id", "034de852-b25c-41c5-9f58-a8f07bdcd2bd"], ["LIMIT", 25], ["OFFSET", 0]]

@mariohd mariohd requested a review from annvelents August 12, 2025 13:26
@mariohd mariohd merged commit 5e234e5 into main Aug 12, 2025
14 checks passed
@mariohd mariohd deleted the fix/use-plans-table-at-subscritpion-query branch August 12, 2025 14:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants