I just deployed my app and had to switch from mysql to postgresql.
Now when executing my query I get following error:
ActiveRecord::StatementInvalid (PG::GroupingError: ERROR: column "search_terms.id" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: SELECT "search_terms".* FROM "search_terms" WHERE (search_te...
^
: SELECT "search_terms".* FROM "search_terms" WHERE (search_terms.term like '%%') GROUP BY search_terms.game_id):
I have a table called search_terms with term:string and game_id:integer. A game can have multiple terms. So I want to group the terms that match by the game_id.
Does anyone know how to solve this?
In my model search_term.rb, I have defined this search function:
# Search Terms
def self.search(query)
where("search_terms.term like ?", "%#{query}%").group("search_terms.game_id")
end
Thank you for any help.
I think this is what you need to try.
def self.search(query)
where("search_terms.term like ?", "%#{query}%").group("search_terms.game_id", "search_terms.term")
end
explanation here: Rails 3.1 with PostgreSQL: GROUP BY must be used in an aggregate function
Related
I have used django to develop a web app.
I want to get the distinct "title" form the queryset get by filter.
But I use mysql so could not pass "title" to distict.
How could I filter the queryset with the distinct "title"?
query_set = CourseInfo.objects.filter(discipline_id=id).distinct('title')
return render(request, 'main.html',
context={'query_set':query_set})
I get error for this in mysql as it may only used in postgresql
`
It will give you distinct titles:
titles = CourseInfo.objects.filter(
discipline_id=id
).order_by('title').values('title').distinct()
Note:
there is no such thing called SELECT DISTINCT ON in MySQL.
You can only use it in Postgresql but maybe GROUP BY helps you for converting SELECT DISTINCT ON query to MySQL query.
Check out this link then you kinda can convert this query to MySQL query.
I'm trying to build the following query in propel
SELECT *
FROM user
ORDER BY username = 'foo.bar' DESC, username
These options all give me errors:
UserQuery::create()
->addAscendingOrderByColumn("username = 'foo.bar'")
This is caused due to the ".". Without it's working
Errormessage:
"Criteria:(Error: Cannot fetch TableMap for undefined table: username = 'foo)"
Sadly, this is not possible in Propel 1 (nor do I know if it's possible with Propel 2). I had a similar issue:
https://groups.google.com/forum/#!topic/propel-users/sz3wA_bdF8k
And upon further investigation, I discovered that you can only pass column names and fully qualified names (table.column) into the ORDER BY methods. Switch statements do work in ->withColumn clauses though, for anyone who's trying to do something similar.
I have tried the suggested solutions on similar topics but they don't work.
MenuItem.where(cat: s.cat).group(:subcat).find_each do |w|
The code above works in the development environment which uses sqlite3 but throws out the following error in the deployment environment which uses postgresql:
PG::GroupingError: ERROR: column "menu_items.id" must appear in the GROUP BY clause or be used in an aggregate function
Any help on solving the issue will be greatly appreciated!
PG::GroupingError: ERROR: column "menu_items.id" must appear in the
GROUP BY clause or be used in an aggregate function
Try the below query
MenuItem.where(cat: s.cat).group(:id, :subcat).find_each do |w|
Switching a rails app from MySQL to Postgres gives the following error:
ERROR: column "contacts.id" must appear in the GROUP BY clause or be used in an aggregate function
Here is the scope in question:
class User < MyModel
def self.top_contacts(timeframe = 1.week.ago, limit = 5)
Contact.unscoped
.where('created_at between ? and ?', timeframe, Time.now)
.group(:user_id)
.order('sum(score) DESC')
.limit(limit)
.includes(:user)
.collect{|x| x.user}
end
end
How to fix this?
Isn't using Rails as the database abstraction layer ensure switching the database should work seamlessly?
The problem is on the level of the SQL, which is invisible from your ORM layer. The problem is exactly with the RoR ORM, because it seems to generate a MySQL-friendly query which uses an extraordinary feature of the MySQL, which postgresql don't have.
The quick solution: give contacts.id to the columns by which you are GROUP-ing as well:
.group("user_id, contacts.id");
I am using this method for search engine friendly URLs in Rails - http://jroller.com/obie/entry/seo_optimization_of_urls_in
My model looks like this.
class Listing < ActiveRecord::Base
def to_param
"#{id}-#{title.parameterize}"
end
end
This works with MySQL but not Postgresql.
This is the error I get:
ActiveRecord::StatementInvalid (PGError: ERROR: invalid input syntax for integer: "16- college-station-apartments"
: SELECT * FROM "floorplans" WHERE ("floorplans"."listing_id" = E'16-college-station-apartments') ORDER BY beds ASC, baths ASC, footage ASC, price ASC):
Is ActiveRecord not doing a .to_i on the find for Postgresql?
Rails will automatically call to_i on your parameter for some methods, mainly those where an integer is expected as a parameter, like Listing.find(params[:id]).
However, for other types of search methods that can accept strings as parameters, you'll need to manually call to_i
Listing.find_by_id(params[:id].to_i)
Listing.find(:conditions => ["id = ?", params[:id].to_i])
The reason you're not having a problem with MySQL is that MySQL does what would in effect be a to_i on its end (i.e. it's not a database-adapter issue, but rather a capability of the actual database server).