ranked-model icon indicating copy to clipboard operation
ranked-model copied to clipboard

Invalid SQL in `relative_rank` when order column value is nil

Open mattvague opened this issue 4 years ago • 1 comments

If you have a model with nil as the position in the database, calling model. branch_order_rank will raise an error due to invalid SQL, eg:

ActiveRecord::StatementInvalid Exception: PG::SyntaxError: ERROR:  syntax error at or near ")"
LINE 1: ... AND "relationship_nodes"."id" != $3 AND ("branch_order" < )

I think I've narrowed down this issue to this method.I know that ranked-model attempts to always populate the column on save but it's possible if you have an existing dataset, bypass callbacks, that you could still have nil values making this an issue.

Not sure what the right solution is, but I feel like this situation should be gracefully handled somehow. Happy to open a PR with a little guidance. Thanks!

mattvague avatar Jun 01 '21 18:06 mattvague

Hi @mattvague, I'm not too sure what to do about this. It looks like you're explicit calling branch_order_rank on this item? Perhaps you could check if the branch_order is nil first?

If that's not the code path then I'd be keen to know what it is to see if we can cope.

My suggestion would be to add mechanisms on your end to keep your data clean (if it has a tendency to get out of order). If you don't bypass the callbacks and ensure any existing data is fixed up before using ranked-model you should have a better time :)

brendon avatar Jun 01 '21 21:06 brendon

No further feedback, so closing this issue.

brendon avatar Jun 04 '24 08:06 brendon