Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Model.limit(n).delete_all & Model.limit(n).update_all generates incorrect query #195

Open
mintuhouse opened this issue May 22, 2023 · 6 comments
Assignees

Comments

@mintuhouse
Copy link
Collaborator

mintuhouse commented May 22, 2023

Sharing the queries so it is clear what is happening

MultiTenant.with(Account.find(1)) { Project.limit(1).delete_all } generates a SQL query

DELETE FROM "projects" WHERE "projects"."id" IN (SELECT "projects"."id" FROM "projects" WHERE "projects"."account_id" = 1 AND "projects"."account_id" = 1 LIMIT 1)

As you can see subquery has account_id condition added correctly but once it returns ids, top level query doesnt have account_id condition.

Ideally it should have generated

DELETE FROM "projects" WHERE "projects"."id" IN (SELECT "projects"."id" FROM "projects" WHERE "projects"."account_id" = 1 LIMIT 1) AND "projects"."account_id" = 1

Environment
Rails 6.1
Ruby 3.1
Citus 10.2

@serprex serprex assigned serprex and gurkanindibay and unassigned serprex May 22, 2023
@gurkanindibay
Copy link
Contributor

@mintuhouse checking now

@gurkanindibay
Copy link
Contributor

gurkanindibay commented May 29, 2023

@mintuhouse I agree with including account_id in the outer query. However, removing account_id causes no problems because project_id is unique in this situation. Is there any functional impact on your application as a result of this?

@mintuhouse
Copy link
Collaborator Author

mintuhouse commented May 29, 2023 via email

@gurkanindibay
Copy link
Contributor

@mintuhouse I tried to implement but Arel::DeleteManager did not allow me to add tenant parameter into delete statement.
Here is the draft PR. Feel free to continue working and adding logic to find a solution
#200

@Amit909Singh
Copy link
Contributor

@mintuhouse I tried to implement but Arel::DeleteManager did not allow me to add tenant parameter into delete statement. Here is the draft PR. Feel free to continue working and adding logic to find a solution #200

@gurkanindibay I am trying to fix the tenant scoping issue on your draft PR. Please review: #219

@Amit909Singh
Copy link
Contributor

@gurkanindibay Please review PR

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

No branches or pull requests

4 participants