Mysql2::Error: Unknown column '6' in 'where clause': SELECT - mysql

I am upgrading my Rails app from Rails 2.3 to 3.2.
and I am getting this weird error
Mysql2::Error: Unknown column '6' in 'where clause':
above error is due to `(backtick) which is applied to data in the where clause along with the column names see the following query.
Mysql2::Error: Unknown column '6' in 'where clause': SELECT `users`.`username`,`users`.`password` FROM `users` WHERE `id` IN (`6`)
see the 6 even though it is data activerecord is applying backtick to it.because of that mysql is raising exception.
NOTE:This is usually seen while calling related object(one to one,many to many)
eg:
u = User.where(:active =>true).each{|u|
//some code
u.user_role #error will raise in this line
}
if I reinitiate the object it will work fine.
u = User.where(:active =>true).each{|u|
//some code
u.user_role #error will raise in this line
u1 = User.find(u.id)
u1.user_role #works fine.
}
This is happening throughout the application and with other models as well,above code snippet is just one instance.
Environment details:
OS: Ubuntu 13.10,Ruby 1.9.3-p545,Rails 3.2.17 mysql 5.5
Anyone knows what going on here ? is it because of gem incompatibility ?
Please let me know if you need some more details.

This issue is due to gem incompatibility.I was using 'slim_scrooge' gem to optimize queries in Rails 2.X doesn't gem well with Rails 3 or higher.

I am not sure why this may be coming, but I think you can avoid this and an extra query to sql while accessing user_role by using include in your query. This will eager load user_role in the first query itself and will not make extra query in each loop.
Code will be something like following:
u = User.where(:active =>true).includes(:user_roles).each{|u|
//some code
u.user_role #error will raise in this line
}
More details on include is here: http://apidock.com/rails/ActiveRecord/QueryMethods/includes

Related

Turbogears nostests results in OperationalError when using SQLAlchemy-FullText-Search

I recently installed SQLAlchemy-FullText-Search dependency in (https://github.com/mengzhuo/sqlalchemy-fulltext-search), but since that I get unexpected results. When I run
nosetests -v
An OperationalError: (OperationalError) near "(": syntax error u'ALTER TABLE opportunity ADD FULLTEXT (title, content, requirements)' () comes out.
I'm defining fulltext_columns:
__fulltext_columns__ = ('title', 'content', 'requirements')
It looks like SQLAlchemy-FullText-Search does only work on MySQL databases - but the default test suite of a TurboGears 2 webapp is using an SQLite in-memory database, so that might be the problem.

Rails: MySQL2 Error when hitting the finder of a model

with Rails I hit this:
User.find(:all, :conditions => ["character = ?", character])
character is a Fixnum, as you can see by the way it is translated for the sql. A Fixnum is expected.
Then I get this error:
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 '= 5)'
at line 1: SELECT `users`.* FROM `users` WHERE (character = 5)
I'm somewhat confused and absolutely do not get what might be wrong with this line of sql.
Please help.
Yours
Joern
The problem is that character is a keyword in mysql. If you escape it in backticks it should work, eg
User.find(:all, :conditions => ["`character` = ?", character])
When you do a rails find like
User.where(:character => character)
as Rich Peck suggests, then rails automatically escapes the names of all fields to prevent this problem: you can see it do this in your log.
EDIT: you might find it less hassle in the long run to change the name of your column.
ActiveRecord
If using Rails 4, you should use this to look up multiple records:
User.where character: character
If you want to load a specific record, you can just use .find like this:
User.find_by character: character
--
Specific
I think your error is more an ActiveRecord issue than a Fixnum one - the fact you're using that data to look up the records shouldn't have any bearing.
I could be wrong, but I think the bottom line is your use of the ActiveRecord methods you've defined.

ActiveRecord::StatementInvalid: Mysql2::Error when using association name in .where method instead of foreign key

We just updated our Rails App from 3.0.13 to 3.2.6 and have encountered a minor problem when using the .where method of the Active Record Query Interface with mysql2.
What used to work before:
client = Client.first
User.where(:client => client)
now leads to:
ActiveRecord::StatementInvalid: Mysql2::Error: Unknown column
'users.client' in 'where clause': SELECT users.* FROM users WHERE
users.client = 1
But when explicitly using the column name of the foreign key, it seems to work just fine:
client = Client.first
User.where(:client_id => client.id)
#=> Relation of users with the given client_id
The associations have not changed (users belong to client, client has many users). This now seems to be the problem with every association of this kind.
Do we now really have to change all these where queries so that they use the foreign_key or is there any other way?
rails version:
gem "rails", "~> 3.2.6"
mysql2 version:
gem "mysql2", "~> 0.3.11"
We just found out that this functionality was provided by a gem called meta_where, which is deprecated in Rails 3.1+.
There is an alternative, squeel, which unfortunately doesn't provide the exact same syntax. (Or we just haven't found out yet...)
Investigating now...
Thanks #zsquare for pointing out.

CakePHP 2 application works fine locally, but comes back with database errors when live?

I've been working on a CakePHP application over the last few months and have finally put it live on a test server on the internet. It has been fairly easy to put it up and live, and it works to a point.
There is a database connection to the server and the app can retrieve data from the database. However, some pages are now returning SQL errors along the lines of:
Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'personel.companyid' in 'where clause'
I thought it might be an error with the application so I rechecked my local copy and the local copy works fine. I then checked the database schema on the test server to see if the column actually exists in all of the cases where I've received these errors. The columns that are apparently unknown are there and include a carbon copy of my local database.
I'm confused about why this is occurring. The only thing that is different between my local machine and my test server is that I developed the site locally on a Windows computer under an XAMPP set up and the test server runs on a Linux (CentOS) VPS.
Is there anything else that could be causing this problem?
EDIT:*
These are two of the queries that are failing:
Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'personel.companyid' in 'where clause'
SQL Query: SELECT `Personel`.`id`, `Personel`.`firstname`, `Personel`.`surname`, `Personel`.`email`, `Personel`.`companyid`, `Personel`.`accesslevel`, `Personel`.`poc`, `Personel`.`password`, `Personel`.`telephone`, `Accesslevel`.`id`, `Accesslevel`.`acclevname` FROM `personel` AS `Personel` LEFT JOIN `accesslevels` AS `Accesslevel` ON (`Personel`.`accesslevel` = `Accesslevel`.`id`) WHERE `personel`.`companyid` = 1
And this one:
Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'personel.companyid' in 'where clause'
SQL Query: SELECT `Personel`.`id`, `Personel`.`firstname`, `Personel`.`surname`, `Personel`.`email`, `Personel`.`companyid`, `Personel`.`accesslevel`, `Personel`.`poc`, `Personel`.`password`, `Personel`.`telephone`, `Accesslevel`.`id`, `Accesslevel`.`acclevname` FROM `personel` AS `Personel` LEFT JOIN `accesslevels` AS `Accesslevel` ON (`Personel`.`accesslevel` = `Accesslevel`.`id`) WHERE `personel`.`companyid` = 1
I think the query is case-sensitive. Try using upper-case Personel everywhere.
Refer to http://dev.mysql.com/doc/refman/5.0/en/identifier-case-sensitivity.html. The first paragraph will explain that MySQL is case-sensitive on Unix and not on Windows.

Bind parameters in Rails mySQL empty

Using Rails 3.1.1, I'm getting occasional errors in production where it seems like the bind parameters on a mysql query are not there for some reason. The error looks like this:
A ActiveRecord::StatementInvalid occurred in events#show:
Mysql::Error: : SELECT `events`.* FROM `events` WHERE `events`.`id` = ? LIMIT 1
activerecord (3.1.1) lib/active_record/connection_adapters/mysql_adapter.rb:890:in `execute
It's not consistent on any insert or select, so I'm having trouble tracking it down. Does anybody have any suggestions?
Edit: updated with simpler example.
#events_controller.rb
def show
#event = Event.find(params[:id])
...
end
#called with parameters: {"action"=>"show", "controller"=>"events", "id"=>"26"}
The probable reason
Check your database driver installation here.
This seems to have gone away after upgrading to the latest rails - uncertain as to what it was.