I am using AR's sum method for a query and seeing this error when using PostgreSQL:
PGError: ERROR: function sum(character varying) does not exist
LINE 1: SELECT sum("assets".asset_file_size) AS sum_asset_file_size ...
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
SELECT sum("assets".asset_file_size) AS sum_asset_file_size FROM "assets" [0m
I am using the following in my code, which works with MySQL:
Asset.sum(:asset_file_size)
I am trying to get a sum of the asset_file_size column.
What am I missing here?
OH.
In writing my question I worked out this issue.
I had accidentally made the column Varchar rather than an Integer. Hence the request to "explicitly type cast".
We apologise for the incovenience.
Related
I have the following as part of an AR query:
.having('COUNT(foo.id) > bar.maxUsers')
This generates an error:
ActiveRecord::StatementInvalid:
PG::UndefinedColumn: ERROR: column bar.maxusers does not exist
^
HINT: Perhaps you meant to reference the column "bar.maxUsers".
I am referencing the column bar.maxUsers.
So, apparently AR downcases the query. How to suppress this behavior?
Rails 4.2.10
PostgreSQL
EDIT: SQL:
SELECT ... HAVING COUNT(foo.id) > bar.maxUsers
So it is happening after the to_sql. Maybe from the execute?
This isn't an ActiveRecord or AREL issue, this is just how case sensitivity works in SQL and PostgreSQL.
Identifiers in SQL (such as table and column names) are case-insensitive unless they're quoted. Standard SQL says that unquoted identifiers are folded to upper case, PostgreSQL folds them to lower case, hence the bar.maxusers in the error message.
The solution is to quote the offending column name:
.having('COUNT(foo.id) > bar."maxUsers"')
Note that you must use double quotes for quoting the identifier as single quotes are only for string literals. Also note that identifier quoting is database-specific: standard SQL and PostgreSQL use double quotes, MySQL uses backticks, SQL Server uses brackets, ...
Can somebody please tell me what is the problem with the following informix statement :
SELECT * FROM
(some big query here)
INTO EXTERNAL empdata(selected_date date, land char, grund integer, some_user varchar, nr decimal(15))
USING (DATAFILES("DISK:/usr1/tbodan.out"))
I get a syntax error near INTO EXTERNAL empdata.
UPDATE
Informix version is 11.7 and omitting the column definition only brings the following error Error: Virtual column must have explicit name.
You need to follow the documented syntax at INTO EXTERNAL clause.
I think your problem is that you tried to provide column names and data types for the table.
This SQL worked for me:
SELECT * FROM elements
INTO EXTERNAL ext_elements
USING (DATAFILES("DISK:/Users/jleffler/tmp/ext-elements.table"))
This SQL did not, generating a -201 "A syntax error has occurred" error:
SELECT * FROM elements
INTO EXTERNAL ext_elements(atomic_number INTEGER, symbol CHAR(3),
name CHAR(20), atomic_weight DECIMAL(8,4),
pt_period SMALLINT, pt_group CHAR(2), stable CHAR(1))
USING (DATAFILES("DISK:/Users/jleffler/tmp/ext-elements.table"))
Testing on a Mac running macOS Sierra 10.12.5, using Informix 12.10.FC4.
Ok,so the problem was actually in the select query.
The query returns more columns that do not have an explicit name and are tagged as "expression" that is why the Error: Virtual column must have explicit name. popped up.
I solved this by using aliases for those columns. Then used the syntax suggested here.
I'm having a problem with a .first query in Rails 4 ActiveRecord. New behavior in Rails 4 is to add an order by the id field so that all db systems will output the same order.
So this...
Foo.where(bar: baz).first
Will give the query...
select foos.* from foos order by foos.id asc limit 1
The problem I am having is my select contains two sum fields. With the order by id thrown in the query automatically, I'm getting an error that the id field must appear in the group by clause. The error is right, no need for the id field if I want the output to be the sum of these two fields.
Here is an example that is not working...
baz = Foo.find(77).fooviews.select("sum(number_of_foos) as total_number_of_foos, sum(number_of_bars) as total_number_of_bars").reorder('').first
Here is the error...
ActiveRecord::StatementInvalid: PG::GroupingError: ERROR: column "foos.id" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: ...bars FROM "fooviews" ORDER BY "...
Since the select is an aggregate expression, there is no need for the order by id, but AR is throwing it in automatically.
I found that I can add a reorder('') on to the end before the .first and that removes the order by id, but is that the right way to fix this?
Thank you
[UPDATE] What I neglected to mention is that I'm converting a large Rails 3 project to Rails 4. So the output from the Rails 3 is an AR object. If possible, the I would like the solution to keep in that format so that there is less code to change in the conversion.
You will want to use take:
The take method retrieves a record without any implicit ordering.
For example:
baz = Foo.find(77).fooviews.select("sum(number_of_foos) as total_number_of_foos, sum(number_of_bars) as total_number_of_bars").take
The commit message here indicates that this was a replacement for the old first behavior.
I'm trying to integrate AuthLogic into my rails application, and I followed the example which defines persistence_token as a string :
https://github.com/binarylogic/authlogic_example
However when I run it with PostgreSQL 8.4 on my ubuntu desktop I get the following error :
ActiveRecord::StatementInvalid in UsersController#index
PGError: ERROR: operator does not exist: character varying = integer
LINE 1: ...* FROM "users" WHERE ("users"."persistence_token" = 21007622...
^
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
: SELECT * FROM "users" WHERE ("users"."persistence_token" = 2100762299) LIMIT 1
I tried to change persistence_token to an integer, but then it seemed other parts of AuthLogic did not update it.
I'm sure this must be a common problem but googling around was not very helpful, any ideas how to resolve this?
Ruby version: 1.8.7
Rails version: 2.3.5
AuthLogic version: 2.1.6
This is a problem because the rows in your table have a 'persistence_token' value of nil. Setting the column to a value will cause this error to go away. I suspect it has something to do with the way rails inspects the table columns and its interaction with authlogic.
Can you show the code that declares persistence_token?
I'd try type casts and conversions first. On the SQL side, all these will work.
WHERE ("users"."persistence_token" = cast(2100762299 as varchar))
WHERE ("users"."persistence_token" = 2100762299::text)
WHERE ("users"."persistence_token" = text(2100762299))
WHERE ("users"."persistence_token" = 2100762299 || '')
That last one is an implicit type coercion. It forces PostgreSQL to treat the number as a string by concatenating it to an empty string.
But you might have to change the data type at run time in Ruby.
yournumber.to_s
Hi
I changed my database from mySql to PostgreSQL and get an error every time I use query with :order statement
For example the following code works perfectly in MySQL
Hour.sum("working_hours",:conditions=>['project_id=? AND reported_date=?',project,h.reported_date],:order=>"reported_date
But gives me an error in PostgreSQL
PGError: ERROR: column "hours.reported_date" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: ...rted_date='2010-10-06 00:00:00.000000') ORDER BY reported_d..
: SELECT sum("hours".working_hours) AS sum_working_hours FROM "hours" WHERE (project_id=1 AND reported_date='2010-10-06 00:00:00.000000') ORDER BY reported_date
If I delete the order statement then the query works ok
I will most appreciate any help on this subject
PostreSQL is stricter to the SQL standard than MySQL is.
SQL states that if you ORDER by a column, that column must be SELECTed and appear in the GROUP BY clause.
Try this:
Hour.sum("working_hours",:conditions=>['project_id=? AND reported_date=?',project,h.reported_date], :order=>"reported_date", :group_by => "working_hours"