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

Restoring has_one with recursive: true creates wrong query #185

Closed
srecnig opened this issue Nov 24, 2014 · 2 comments
Closed

Restoring has_one with recursive: true creates wrong query #185

srecnig opened this issue Nov 24, 2014 · 2 comments

Comments

@srecnig
Copy link
Contributor

srecnig commented Nov 24, 2014

hi!

i've already submitted pullrequest #171 for this problem, but got no response. since i had some time to look into this issue a bit more, i thought i'd open a issue as it a more appropriate place for discussion.

so, the problem is that recursively restoring a has_one relationship will create a wrong sql query, which in postgres will lead to PG::DatatypeMismatch: ERROR: argument of AND must be type boolean, not type integer ( and subsequently ActiveRecord::StatementInvalid) . when using sqlite, the query is still wrong but does not break.

the query built is (sqlite):

SELECT  "paranoid_model_with_belongs".* FROM "paranoid_model_with_belongs"  
WHERE ("paranoid_model_with_belongs"."deleted_at" IS NOT NULL) 
AND (paranoid_model_with_has_one_id)  
ORDER BY "paranoid_model_with_belongs"."id" ASC LIMIT 1

due to AND (paranoid_model_with_has_one_id) instead of AND "paranoid_model_with_belongs"."paranoid_model_with_has_one_id" = 1, in restore_associated_records(), the correct record to be restored is never selected. this might be what's happening in #163.

.where(association_foreign_key, self.id) should be .where(association_foreign_key => self.id). this leads to the desired query

SELECT  "paranoid_model_with_belongs".* FROM "paranoid_model_with_belongs"  
WHERE ("paranoid_model_with_belongs"."deleted_at" IS NOT NULL) 
AND "paranoid_model_with_belongs"."paranoid_model_with_has_one_id" = 1  
ORDER BY "paranoid_model_with_belongs"."id" ASC LIMIT 1

so, if you think my pullrequest is missing anything or if i'm missing something on the whole situation just say so, i'd be happy to comply.

cheers

@chaupt
Copy link

chaupt commented Nov 27, 2014

👍 Tested PR #171 out in our app and it is working nicely with this specific situation (we're on postgres too)

srecnig pushed a commit to srecnig/paranoia that referenced this issue Nov 27, 2014
jhawthorn pushed a commit that referenced this issue Nov 28, 2014
@jhawthorn
Copy link
Collaborator

Closing due to #171. Thanks for the diligence on this issue ⭐

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

3 participants