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

[Mysql] small update to quote table names. Mysql 8 has keywords that… #512

Merged
merged 1 commit into from
Mar 23, 2022

Conversation

Kyle-Ferrara
Copy link
Contributor

@Kyle-Ferrara Kyle-Ferrara commented Sep 2, 2021

… might match table names which cause an exception when selecting with the deleted/deleted at columns.

To reproduce the issue:
We have Rails model Group with its table name as 'groups'. It is related to another model/table Content/contents

We use mysql 8
Rails 6 (latest)
When we try to restore a record:

Content.only_deleted.last.restore(recursive: true, recovery_window: 2.minutes)

We get this error:

mnt/f0ef7dec-1a57-4dbb-b044-478d728be42f/.gem/ruby/3.0.2/gems/mysql2-0.5.3/lib/mysql2/client.rb:131:in `_query': Mysql2::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'groups.`deleted` IS NULL OR groups.`deleted` != FALSE)' at line 1 (ActiveRecord::StatementInvalid)
/mnt/f0ef7dec-1a57-4dbb-b044-478d728be42f/.gem/ruby/3.0.2/gems/mysql2-0.5.3/lib/mysql2/client.rb:131:in `_query': You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'groups.`deleted` IS NULL OR groups.`deleted` != FALSE)' at line 1 (Mysql2::Error)

The issue with mysql 8 is 'groups' is now a reserved word. This is clashing with our table name. To overcome this the table name needs to quoted just like is done with the column name.

We are actually just expecting the record to be restored with its associations.

So this:

groups.`deleted` IS NULL OR groups.`deleted` != FALSE) should be this `groups`.`deleted` IS NULL OR `groups`.`deleted`

Here is a gist of the Gemfile.lock: https://gist.github.com/Kyle-Ferrara/593116e6af8170a2d99598946d459dec

There might be a better solution to this issue. This is what I could see. Feel free to take it or leave it lol.
I hope this will help guys using mysql 8.

… might match table names which cause an exception when selecting with the dleted/deleted at columns.
Copy link
Collaborator

@mathieujobin mathieujobin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice fix, although I would recommend everyone NOT to have a table named groups, etc.

@Kyle-Ferrara
Copy link
Contributor Author

Agreed lol xD

@mathieujobin mathieujobin merged commit cc19cef into rubysherpas:core Mar 23, 2022
karunkumar1ly pushed a commit to edcast/paranoia that referenced this pull request Feb 6, 2024
… might match table names which cause an exception when selecting with the dleted/deleted at columns. (rubysherpas#512)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants