While setting up Sphinx on my production server, this strange error came up when trying to index
ERROR: index 'benefit_core': sql_range_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 '' at line 1
This doesn't happen on my local machine. And yes, that is an empty string.
Does anyone seen this type of issue before?
benefit.rb
define_index do
# Fields
indexes category
indexes title
indexes tags
indexes description
indexes brief_description
indexes brand
indexes short_description
indexes long_description
indexes benefit_description
indexes address.city
indexes address.state
indexes address.street_1
where sanitize_sql(["active = true and expiration > ?", Time.now])
set_property :field_weights => {
:title => 15,
:tags => 10,
:brand => 10,
:description => 3
}
end
Thinking-Sphinx - 1.4.4
Sphinx - 0.9.9
Thank you!
Be sure you are running the most recent version of Thinking Sphinx, 3.0.4 or so. Looks like there has been some issues with sanitize_sql
https://github.com/freelancing-god/thinking-sphinx/issues/213
Also try re-writing the line to be
where sanitize_sql(["active = ? and expiration > ?", true, Time.now])
Also try commenting out all the lines and gradually add them back to determine exactly where the error is occurring.
The problem ended up being with the sanitize_sql method. I replaced that line with:
where "active = true AND expiration > \"#{Time.now.to_formatted_s(:db)}\""
Thanks for the help!
Related
I am using gem rails~> 5.2 and gem mysql2 >= 0.3.13, < 0.5.
I have a model Lawer, which has an array column lawer_filed [sic].
# Model lawer.rb
serialize :lawer_field, Array
Then I created a Lawer, and I can get the lawer_field value as follows:
=> Lawer.first.lawer_field
=> ["2", "3", "5"]
Now, I want to find one Lawer with a query using lawer_field. I tried:
#lawer = Lawer.where("lawer_field && ARRAY[?]", "2")
which raised an error like this:
ActiveRecord::StatementInvalid (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 '['2']) LIMIT 11' at line 1: SELECT `lawers`.* FROM `lawers` WHERE (lawer_field && ARRAY['2']) LIMIT 11)
There is a mistake in my SQL syntax, but I don't how to fix it. Can anyone help?
MySQL, unlike PostgreSQL, does not support arrays in database. Therefore you needed to add this line:
serialize :lawer_field, Array
This means that you have a string field in your database, but whenever ActiveRecord is unpacking results returned by the database, it maps them directly to an instance of Ruby Array. What this means is that your only option to filter the results in the database is with any MySQL string comparison functions, LIKE, etc.
Your options are to either use LIKE or perform some other String functions (which will not perform well as you will be unable to use indices) or build another table, add a has_many association to it and use MySQL the way it was supposed to be used. You could also, of course, migrate to PostgreSQL, but that seems to be the most extreme option.
EDIT: you could also consider using MySQL`s JSON, which has been added recently. That depends on your version of MySQL though.
I didn't try this answer because I don't have any Rails project ready for testing, but I think the problem is in the syntax.
I think it should be something like this:
Lawer.where("lawer_field IN (?)", "2")
I just created a table called group and generated the skeleton files referenced to this table.
I realized that this name enter in conflict with MySQL Reserved Words, because cakephp3.0 generates queries like that:
SELECT
Group.group_id AS `Group__group_id`,
Group.name AS `Group__name`,
Group.created_at AS `Group__created_at`
FROM
group Group
LIMIT
20 OFFSET 0
That throws this error:
Error: SQLSTATE[42000]: Syntax error or access violation: 1064 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 'group Group
LIMIT 20 OFFSET 0' at line 1
Is there a way to avoid this kind of error?
Indeed you can enable the quoteItendifiers but that comes with a performance hit as it says in the comment above it.
I use a different solution to this issue, by customizing the Table class for the problematic db_table like so:
Note the table alias being renamed and also the table name I have escaped manually
class GroupTable extends Table
{
public function initialize(array $config)
{
parent::initialize($config); // TODO: Change the autogenerated stub
$this->setAlias('MyGroup');
$this->setTable("`group`");
}
}
This will generate a query looking like this:
SELECT
MyGroup.id AS `MyGroup__id`,
MyGroup.filed1 AS `MyGroup__filed1`
FROM
`group` MyGroup
With CakePHP 3.6 $Group->find()->all() runs successfully.
I'm using CakePHP 4, and to solve this problem, I just added quoteIdentifiers => true, inside config -> app_local -> datasources
Datasources' => [
'default' => [
'quoteIdentifiers' => true,
'host' => '127.0.0.1',
quoteIdentifiers
Set to true if you are using reserved words or special characters in your table or column names. Enabling this setting will result in queries built using the Query Builder having identifiers quoted when creating SQL. It should be noted that this decreases performance because each query needs to be traversed and manipulated before being executed.
See more in: https://book.cakephp.org/4/en/orm/database-basics.html
I just found the solution. The solution is to change the value of 'quoteIdentifiers' to true in your Datasource configuration. May you need to clear the cache.
Source: https://book.cakephp.org/3.0/en/orm/database-basics.html#configuration
i tried the CakePHP 3.x "Bookmaker Tutorial" and i followed the instruction step by step. Unfortunately, at the end of the first chapter i get the attached error:
Error: SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #8 of SELECT list
is not in GROUP BY clause and contains nonaggregated column 'wis.Tags.id' which is not
functionally dependent on columns in GROUP BY clause; this is incompatible with
sql_mode=only_full_group_by
Furthermore, i get the information to check my "BookmarksTags" table but i do not have to creat one before. I little bit confused.
Please try correcting the issue for the following table aliases:
BookmarksTags
I already google my problem and i found information to update the "my.cnf" with a extra line. i already try but nothing happed. I also check the spelling and downloaded the "bookmarker-tutorial" from github but i still get this error above.
I use MySQL 5.7.11 and PHP 5.6.14.
This is a new thing in MySQL 5.7 and is a warning that your query is ambiguous.
Consider the following table:
id | name | age | points
--------------------------------------------
1 Bob 21 1
2 James 14 1
3 Bob 21 3
4 James 14 2
5 Casey 17 3
If you did the following query:
SELECT name, age, SUM(points) FROM scores GROUP BY name
Then the name column is used for grouping. Note that age may have multiple values, so it's "non-aggregated". You need to do something to collapse down those values.
The behaviour in 5.6 and previous was to just pick the first one depending on sort order, though this was sometimes unpredictable and would fail. In 5.7 they're preventing you from doing it in the first place.
The solution here is to group on that as well, or to apply an aggregate operator like MIN() to it.
I'm using Laravel 5.4 and facing same problem.
I try to set strict into false on config/database.php and it works.
'connections' => [
'mysql' => [
'strict' => false,
]
]
However it's better to edit sql query than suppress it's warning.
This may be the issue related to newer version of MySQL as you said you are using MySQL 5.7.11
So to solve this issue for all your sites see this
https://craftcms.stackexchange.com/questions/12084/getting-this-sql-error-group-by-incompatible-with-sql-mode-only-full-group-by/13662#13662
the answer by community to add the line below
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
in /etc/mysql/my.cnf file & saving it; and after restarting the server
sudo service mysql restart
Would do the work
Just stumbled on the same problem.
A simple way to make the tutorial "work" without changing the query could be to reset sql_mode in config/app.php :
'Datasources' => [
'default' => [
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Mysql',
'persistent' => false,
'host' => 'localhost',
...
'init' => ["SET sql_mode = ''"],
],
While this should makes things work, it is probably not recommended in real life cases.
In addition to tadman's comment, for tutorial you are following, you should distinct your sql select query by both Bookmarks.id and BookmarksTags.tag_id, instead of just Bookmarks.id
To do that, in BookmarksTable.php file, line
->distinct(['Bookmarks.id'])
should look like
->distinct(['Bookmarks.id', 'BookmarksTags.tag_id'])
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.
i got this call in my controller:
#tournaments = Tournament.unignored.all(
:include => [:matches,:sport,:category],
:conditions=> ["matches.status in (0,4)
&& matches.date < ?",
Time.now.end_of_week + 1.day],
:order => "sports.sort ASC, categories.sort ASC, tournaments.sort ASC")
All works out in production mode and in the development console as well. But when I try to browse to that certain page in development mode i get:
The error occurred while evaluating nil.each
When I paste the created SQL Query into MySQL Browser there are results.
It refers to mysql2 (0.2.11) lib/active_record/connection_adapters/mysql2_adapter.rb:587:in `select'
The query arrives correctly in this one.
Did anyone had similar problems? This error came out of nowhere. No updates etc...
Thanks!
Rails 3.0.9 MySql 5.5 Ruby 1.8.7 and mysql2 0.2.11 gem
It looks like you need to use :joins instead of :include.
the :include option to all (and find and where etc) tells rails to separately do a query to load all the ncessary data for the given associated records.
The :join option gets rails to perform an SQL query that JOIN`s the associated models so you can query on their fields.
If you want to both query on the fields and preload them into the associations, you need to do both:
#tournaments = Tournament.unignored.all(
:include => [:matches,:sport,:category],
:joins => [:matches,:sport,:category],
:conditions=> ["matches.status in (0,4)
&& matches.date < ?",
Time.now.end_of_week + 1.day],
:order => "sports.sort ASC, categories.sort ASC, tournaments.sort ASC")