unknown LINE 1: select "id" from"notifications" where"data"->>'id' = $1 You might need to add explicit type casts(SQL: select"id"from"notifications" - heroku-postgres

i make project with laravel and i upload it on the heroku i used notifications so data column in notification table stored like {"id":36,"total":"300","tableNum":"6"} these data for order
i used this code to make notification unread
public function show($id) //$id refers to order id in this example id = 36
{
$getId = DB::table('notifications')->where("data->id", $id)->pluck('id');
DB::table('notifications')->where('id', $getId)->update(['read_at' => now()]);
return redirect()->route('current');
}
this code runs without any errors, but in heroku make an error "cast" what can i do ?
SQLSTATE[42883]: Undefined function: 7 ERROR: operator does not exist: text ->> unknown
LINE 1: select "id" from "notifications" where "data"->>'id' = $1
^
HINT: No operator matches the given name and argument types. You might need to add explicit type casts. (SQL: select "id" from "notifications" where "data"->>'id' = 1)

You have a syntax error in your current example.
->where("data->id", $id)
It should either be "id" or $data->id, but it's hard to assume based from the code you just posted. However, your answer could be simplified by just using this. Don't need to pluck id and use it again for another query
public function show($id) //$id refers to order id in this example id = 36
{
DB::table('notifications')->where('id', $id)->update(['read_at' => now()]);
return redirect()->route('current');
}

Related

EXECUTE FORMAT() USING in postgres is showing error for $

i will directly get to the code rather than explaining too much
execute format('
"$1" = select "Source1" from temp_tables._%s;
'::text, (translate("Song_Id_"::text, '-', '_')))
using "Source1__";
the table is dynamically created and the table name is all fine as i have used that table to insert some data into it. if i run this code, the error i am getting is
ERROR: syntax error at or near "$1"
LINE 1: $1 = select "Source1" from temp_tables._24af1593_3539_49fd_9...
^
QUERY: $1 = select "Source1" from temp_tables._24af1593_3539_49fd_9ef4_29307f301d38;
i have tried other method too like
execute
'$1 = select "Source1" from temp_tables._' || (translate("Song_Id_"::text, '-', '_')) ||';'
using "Source1__";
even this gives the same error.
note : "Source1__" is a variable of type text declared in the stored procedure where everything else is being executed too.
This is wrong. The EXECUTE command accepts only SQL statements. There is nothing statement like var = SELECT .... More - the you can pass just value by using clause USING. You cannot to pass any reference to variable. Solution is easy. Just use clause INTO
EXECUTE 'SELECT ... ' INTO target_plpgsql_variable
Please, read related documentation. Unfortunately, some parts the stored procedures are not extra intuitive because there is mix of two very different languages. It is good by reading documentation, because is hard to find correct solution without knowledge of possibilities and syntax.

Where not null for reference column showing error - rails

I want to query a table where multiple referenced column in not null.
I tried like this:
JobTypePresetting.where('comment_text == ?', nil)
JobTypePresetting.where('comment_text_id == ? OR part_listing_id == ?', nil, nil)
Even first is not working showing error like
ActiveRecord::StatementInvalid: PG::UndefinedFunction: ERROR: operator does not exist: integer == unknown
LINE 1: ...resettings"."template_id" = $2 AND (comment_text_id == NULL)
^
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
How do i query this, currently i'm using like
JobTypePresetting.where('comment_text_id is not null or part_listing_id is not null')
You need to use single(=) oprator.
JobTypePresetting.where('comment_text = ?', nil)
Hope this may work for you.
Try to use Arel, much easier to construct complex sql queries. They have been part of rails from 3 release. https://github.com/rails/arel
jtp = JobTypePresetting.arel_table
JobTypePresetting.where(jtp[:comment_text_id].not_eq(nil).or(jtp[: part_listing_id].not_eq(nil)))
Hope it helps

Rails/Postgres nested JSON query

I have a JSON column in my events table called payload. I can create an event like this:
Event.create!(application_id: 1, stage: 'INITIATION', payload: { key: 'OUTCOME', value: {school_id: 2, prefered_language: 'GBP'}})
Simple queries like
Event.where("payload->>'key' = ?", "OUTCOME")
Will work fine, but how can I then add extra filters with the JSON that's nested in the value portion
Event.where("payload->>'key' = ? AND payload ->>'value'->>'school' = ?", "OUTCOME", 2)
^^^^^^^^^^^^^^^^^^^^
I tried that but i got:
PG::UndefinedFunction: ERROR: operator does not exist: text ->> unknown
LINE 1: ...ad ->>'key' = 'INPUTPARAMS' AND payload ->>'value'->>'school... ^
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
How do I get into the school attribute within value?
I have taken the idea from this documentation on ActiveRecord and JSON
After much searching, I came across this article.
The #>> operator allows you to dig inside the JSON. My query could be fixed like this.
Event.where("payload->>'key'= ? AND payload#>>'{value, school_id}' = ?", 'OUTCOME', shcool_id)
You could theoretically go down further by adding each layer to the predicate block {value, school, next_level, and_so_on}

How to read json in Rails Controller?

So, I'm doing like that at my controller action:
json_document = JSON(params) (json gem)
If I print json_document it gives me a Json like that:
{"createdAt":"Mar 6, 2012 6:12:54 PM","id":177139718620856322,"text":"#rgjunio where?","source":"web","isTruncated":false,"inReplyToStatusId":177139644012572673, ...
But when I try to access it:
tweet = Complaint.find_by_twitter_status json_document["inReplyToStatusId"]
i get the following error:
can't convert ActiveSupport::HashWithIndifferentAccess into String
So i tried another way using symbols:
tweet = Complaint.find_by_twitter_status json_document[:inReplyToStatusId]
but it gives me the error:
TypeError (can't convert Symbol into Integer)
Any idea?
Update:
If I use params[:inReplyToStatusId] isntead I got the following:
ActiveRecord::StatementInvalid (PG::Error: ERROR: operator does not exist: character varying = bigint
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
SELECT "complaints".* FROM "complaints" WHERE "complaints"."twitter_status" = 177144980450721794 LIMIT 1):
Try:
params[:inReplyToStatusId].to_s

table_method how to check left outer join field is null

I have a generated admin module with the following table method
class Exam{
...
function retrieveExamList(Doctrine_Query $q){
$rootAlias = $q->getRootAlias();
return $q->
innerJoin("$rootAlias.Person p")->
innerJoin("$rootAlias.Code ec")->
leftJoin ("$rootAlias.Order order")
->leftJoin("order.LatestVmOrderDetail od")->
addSelect("$rootAlias.*, p.*, ec.*, ec.name as exam_code_name, "
."order.order_id, od.payment_status, od.payment_date, od.payment_method_id"
);
}
In the admin list view, $exam->getPaymentStatus() returns null if the outer join returns null, what is the recommended way to check for this?
$exam->_data['payment_status'] === null
or is there something better?
I have tried
if ($exam->getPaymentStatus()){
which returns a Fatal error.
My problem is specifically that I have an order number in exam, but that order number doesn't exist in the corresponding relation
I usually use a simple if like this:
if ($exam->getPaymentStatus()) { /* ... */ }

Resources