Error when ignoring accents in full text search using Ecto and PostgreSQL - postgresql

I have this search function in my controller:
def search(query, search_term) do
(from u in query,
where: fragment("(to_tsvector('portuguese', unaccent(?)) ## plainto_tsquery(?))", u.name, ^search_term),
order_by: fragment("ts_rank((to_tsvector('portuguese', unaccent(?)), plainto_tsquery(?))) DESC", u.name, ^search_term))
end
I'm getting this error:
ERROR (undefined_function): function unaccent(character varying) does not exist
I've also added the unaccented extension to PostgreSQL with:
CREATE EXTENSION unaccent;
How to make this work?

Related

Is there any way to add multiple labels to a node?

Let's say I'm creating a database about food and in it, I want to add Dal which would be both Curry and Lentil.
SELECT * FROM cypher('menu', $$
CREATE (:LENTIL:CURRY {name:"Dal"})
$$) AS (dal agtype);
2023-02-20 06:49:01.568 IST [145143] ERROR: syntax error at or near ":" at character 49
2023-02-20 06:49:01.568 IST [145143] STATEMENT: SELECT * FROM cypher('menu', $$
CREATE (:LENTIL:CURRY {name:"Dal"})
$$) AS (dal agtype);
ERROR: syntax error at or near ":"
LINE 2: CREATE (:LENTIL:CURRY {name:"Dal"})
That does not seem to be working.
It does not work with SET clause either: -
SELECT * FROM cypher('menu', $$
MATCH (dal:CURRY {name: "Dal Makhani"})
SET dal:LENTIL
RETURN dal
$$) AS (dal agtype);
2023-02-20 06:29:38.402 IST [145143] ERROR: syntax error at or near ":" at character 80
2023-02-20 06:29:38.402 IST [145143] STATEMENT: SELECT * FROM cypher('menu', $$
MATCH (dal:CURRY {name: "Dal Makhani"})
SET dal:LENTIL
RETURN dal
$$) AS (dal agtype);
ERROR: syntax error at or near ":"
LINE 3: SET dal:LENTIL
Is there any work-around for this?
This feature is currently in development. Through label inheritance it will be possible to set multiple labels for a single node in AGE.
Keep an eye on this branch for more updates:
https://github.com/apache/age/tree/AGE_label_inheritance

Database-migration ms sql to postgresql

I am getting a syntax error when converting between SQL Server to PostgreSQL. Any thoughts?
IF (var_port_with_bmrk_total_mv != 0 AND var_bmrk_info IS NOT NULL) THEN
BEGIN
insert into t$tmp_diff
select #asof_dt asof_dt,#choiceID choiceID ,p.input_array_type ,p.group_order, CONVERT(DECIMAL(32,10),p.port_value/#var_port_total_mv) port_value,convert(decimal(32,10), isnull(bmrk_value/#port_with_bmrk_total_mv,0)) bmrk_value
from t$tmp_port_sum p, t$tmp_bmrk_sum b
where p.input_array_type=b.input_array_type and p.group_order = b.group_order
END;
ELSE
Original before conversion
insert into #tmp_other_diff
select #asof_dt asof_dt,#choiceID choiceID , b.input_array_type,b.grouping,convert(decimal(32,10),0) port_value, (bmrk_value/#port_with_bmrk_total_mv) bmrk_value
from #tmp_bmrk_other_sum b
where b.key_value not in ( select p.key_value from #tmp_port_other_sum p)
Error message:
Error occurred during SQL query execution
Reason:
SQL Error [42601]: ERROR: syntax error at or near ","
Position: 9030
the relevant comma being:
CONVERT(DECIMAL(32,10),p.port_value
There is no convert() function in Postgres. Use the SQL standard cast or the Postgres extension ::data type. In this case:
...., cast(0 as decimal(30,10)) port_value, ....
OR
...., 0::decimal(30,10) port_value, ...
Note: No comma after the expression. In the original port_value is the column alias. You need to keep it that way.

ERROR: function policies_are(unknown, unknown, unknown) is not unique

I am getting below error while running test on pgtap in postgresql. Here I am using "function policies_are" to test my policy which I have created to enable ROW LEVEL SECURITY.
In this case I am running below test case:-
BEGIN;
CREATE EXTENSION pgtap;
select plan(1);
SELECT policies_are( 'public', 'employee', 'emp_rls_policy' );
select * from finish();
ROLLBACK;
I found below reference to test this function:-
https://pgtap.org/documentation.html#policies_are
The same error I am getting for running below 2 functions as well:-
policy_cmd_is()
policy_roles_are()
Error:-
ERROR: function policies_are(unknown, unknown, unknown) is not unique
LINE 1: SELECT policies_are( 'public', 'employee', 'emp_rls_policy' ...
^
HINT: Could not choose a best candidate function. You might need to add explicit type casts.

Pgsql error: You might need to add explicit type casts

My website is just working fine til i deployed it to heroku and the problem is heroku uses pgsql and I'm using mysql and laravel framework.
my query is
$patient = Patient::where('patient_address', 'ILIKE' ,'%' . $request->input)->where('patient_sex', 'ILIKE' ,'%' . $request->gender)->whereHas('users', function($q) use($vaccine_id){
$q->where('vaccine_id','ILIKE','%' . $vaccine_id);
})->get();
here's what I'm getting when I deploy it to heroku
SQLSTATE[42883]: Undefined function: 7 ERROR: operator does not exist: integer ~~* unknown
LINE 1: ...ient_id" = "patients"."PatientID" and "vaccine_id" ILIKE $3)
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts. (SQL: select * from "patients" where "patient_address" ILIKE %San Francisco and "patient_sex" ILIKE % and exists (select * from "vaccines" inner join "immunizations" on "vaccines"."VaccineID" = "immunizations"."vaccine_id" where "immunizations"."patient_id" = "patients"."PatientID" and "vaccine_id" ILIKE %))
I have tried using cast like CAST(vaccine_id AS VARCHAR) and I' not getting the error but it doesnt return any result.
The problem is here:
$q->where('vaccine_id','ILIKE','%' . $vaccine_id)
looks like vaccine_id is integer, and you can not use operator ILIKE to integer. Try just '='
If you want to use LIKE, ILIKE or other text operator you must cast your data to text. In SQL it must looks like:
WHERE "vaccine_id"::text ILIKE val
instead
WHERE "vaccine_id" ILIKE val
You could do this:
$q->where('cast(vaccine_id AS VARCHAR)','LIKE','%' . $vaccine_id)
OR
$q->where('cast(vaccine_id AS TEXT)','LIKE','%' . $vaccine_id)

SQL Command failed with: ERROR: operator does not exist: character varying?

i am trying to get values from database by using following query
SELECT Distinct org.name AS org, sto.ad_org_id AS wh_nearstoredetails_id, sum(sto.qtyonhand) AS qty, pro.name AS product
FROM ad_org org, m_storage_detail sto, m_product pro
WHERE sto.ad_org_id::text = org.ad_org_id::text
AND (sto.m_locator_id::text IN ( select cast(m_locator.m_locator_id as text)
from m_locator,m_warehouse
where m_warehouse.isactive = cast('Y' as varchar)
and m_warehouse.em_ai_warehouseparent::text not like cast('' as text)
and m_warehouse.m_warehouse_id::text = m_locator.m_warehouse_id::text
and m_locator.isdefault = cast('Y' as varchar)))
AND sto.m_product_id::text = pro.m_product_id::text
AND sto.qtyonhand >= cast(1 as numeric)
AND sto.ad_org_id::text IN ( SELECT cast(m_warehouse.ad_org_id as text)
FROM m_warehouse
WHERE m_warehouse.em_ai_warehouseparent::text not like cast('' as text))
GROUP BY org.name,sto.ad_org_id,pro.name
ORDER BY org.name, pro.name;
after creating this i also created a class to call this query and get data but when i deploy my project i am getting following error
WARN - SQL Command failed with: ERROR: operator does not exist: character varying !
Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts.
please help to solve this issue.
i finally found the mistake what i made.
the problem in the above query is i'm trying to make type cast of null value
now i changed the condition to check 'is not null' than "not like ''"
and even i don't need to add cast operation it's useless.
anyways thanks for you response. :)