-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Fix ActiveRecord::StatementInvalid error when using UUID as primary key #894
Conversation
@@ -49,7 +49,7 @@ def related_tags_for(context, klass, options = {}) | |||
private | |||
|
|||
def exclude_self(klass, id) | |||
"#{klass.table_name}.#{klass.primary_key} != #{id} AND" if [self.class.base_class, self.class].include? klass | |||
"#{klass.table_name}.#{klass.primary_key} != '#{id}' AND" if [self.class.base_class, self.class].include? klass |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This fixed uuid PK but broke integer/bigint
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@seuros Thank you for pointing out my mistake.
How do you think if I use klass.columns_hash['id'].type
to check the PK type first, and modify the method like this:
def exclude_self(klass, id)
new_id = klass.columns_hash[klass.primary_key].type == :uuid ? "'#{id}'" : id if klass.primary_key
"#{klass.table_name}.#{klass.primary_key} != #{new_id} AND" if [self.class.base_class, self.class].include? klass
end
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should use Arel in this part of code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@seuros Thanks for your suggestion.
I'm not familiar with Arel, would you please confirm the following code is correct.
def exclude_self(klass, id)
"#{klass.arel_table[klass.primary_key].not_eq(id).to_sql} AND" if [self.class.base_class, self.class].include? klass
end
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, i think it should work.
Can you rebase your PR ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also please include a test case.
442b429
to
1c6ab08
Compare
1c6ab08
to
a605829
Compare
@seuros Due to the definition of And if I change the definition of |
Thank you |
Oh, I see. But I have no idea about how to modify the |
d7b4519
to
a605829
Compare
cherry-picked on #898 . |
If using UUID as primary key, the relationships feature raises
ActiveRecord::StatementInvalid
error when you try something like: