Cannot insert character 'N' to PostgreSQL in Sequelize - postgresql

I have data object as below:
const sStatus = "N";
const oMemberCard = {
code: memberCardCode,
member: memberCode,
status: sStatus,
status_datetime: moment().format(),
};
I use Sequelize to insert but have error:
Executing (default): INSERT INTO "mb_member_card"
("member","code","status","status_datetime") VALUES
('A001132203','0315099359','NaN','2020-10-18 00:33:32.000 +07:00');
[ERROR] SequelizeDatabaseError: value too long for type character(1)
I tried use escape("N") but not success.
Anybody can help me! Thanks.
SOLDED
I mistake field type in model. Sorry!

The problem is - your trying to insert NaN not N on Your status column which is character type with length limit 1, check the oMemberCard variable especially status element first before passing it to sequalize.

Related

Unexpected end of string when removing key from hstore

When I am removing a key from a HSTORE, I get the error 'Unexpected end of string':
DB=# UPDATE mytable SET properties = properties - 'key' where label = '9912345678';
ERROR: Unexpected end of string
LINE 1: UPDATE mytable SET properties = properties - 'key' where ...
When I explicitly cast that string, it does work:
DB=# UPDATE mytable SET properties = properties - 'key'::text where label = '9912345678';
UPDATE 1
Why does it give this error message? Isn't 'key' a TEXT column? Or at least a string with a expected end?
The operator is overloaded so the right-hand operand should be explicitly cast. Note the example usage for text, text[] and hstore in the documentation:
'a=>1, b=>2, c=>3'::hstore - 'b'::text
'a=>1, b=>2, c=>3'::hstore - ARRAY['a','b']
'a=>1, b=>2, c=>3'::hstore - 'a=>4, b=>2'::hstore
In the statement
UPDATE mytable SET properties = properties - 'key' where label = '9912345678';
the string 'key' may be resolved as text or hstore. The first choice of the hstore algorithm is hstore, so the statement is resolved to
UPDATE mytable SET properties = properties - 'key'::hstore where label = '9912345678';
and raises the error
ERROR: Unexpected end of string
though the error message could be more informative, like Unexpected end of string while parsing hstore constant.

sqlalchemy to create temporary table

I created a temporary table with sqlalchemy (with an underlying postgres database) that is going to be joined with a database table. However, in some cases when a value is empty '' then postgres throws the error:
failed to find conversion function from unknown to text
SqlAlchemy assembles everything to the following context
[SQL: 'WITH temp_table AS \n(SELECT %(param_1)s AS id, %(param_2)s AS email, %(param_3)s AS phone)\n SELECT campaigns_contact.id, campaigns_contact.email, campaigns_contact.phone \nFROM campaigns_contact JOIN temp_table ON temp_table.id = campaigns_contact.id AND temp_table.email = campaigns_contact.email AND temp_table.phone = campaigns_contact.phone'] [parameters: {'param_1': 83, 'param_2': '', 'param_3': '+1234567890'}]
I assemble the temporary table as follows
stmts = []
for row in import_data:
row_values = [literal(row[value]).label(value) for value in values]
stmts.append(select(row_values))
subquery = union_all(*stmts)
subquery = subquery.cte(name="temp_table")
The problem seems to be the part here
...%(param_2)s AS email...
which after replacing the param_2 results in
...'' AS email...
which will cause the error mentioned above.
One way to solve the issue is to perform a cast
...''::text AS email...
However, I don't know how to perform ::text cast with sqlalchemy!?

PostgreSQL: return message after count = 0

I have maybe easy question, but I'm completely stucked.
I have script
SELECT COALESCE(COUNT(id), 0) as MyFiels from table
It works fine and when I have zero value it shows 0.
But I want that instead of 0, I can see one line = "NO RESULTS" for example.
I tried:
SELECT COALESCE(to_char(COUNT(id), 'NO RESULT')) as MyFiels from table
And PostgreSQL shows error message:
ERROR: "E" is not supported
SQL state: 0A000
Where I'm incorrect? Any ideas?
I see what is the error, you are trying to use coalesce to convert 0 to string, and coalesce convert null to something. You need use a CASE
SELECT CASE WHEN COUNT(*) = 0 THEN 'NO RESULT'
ELSE CAST(COUNT(*) as TEXT)
END as field
FROM Table

ERROR : The column index is out of range: 1, number of columns: 0

I'm using wso2dss 3.0.0.I'm trying to execute a postgresql query i.e.
SELECT addressid, geocode
FROM maddress
WHERE geocode::point <# circle '((18.9750,72.8258), 5)';
It is working fine in PostgreSQL.When i'm using same query in wso2dss i.e.
SELECT addressid, geocode
FROM maddress
WHERE geocode::point <# circle '((?,?), ?)';
It gives me error like :
DS Fault Message: Error in 'SQLQuery.processNormalQuery'
DS Code: DATABASE_ERROR
Source Data Service:-
Name: Geofence_DataService
Location: /Geofence_DataService.dbs
Description: N/A
Default Namespace: http://ws.wso2.org/dataservice
Current Request Name: adding_geofence_op
Current Params: {longitude=72.8258, radius=4, latitude=18.9750}
Nested Exception:-
DS Fault Message: Error in 'createProcessedPreparedStatement'
DS Code: UNKNOWN_ERROR
Nested Exception:-
org.postgresql.util.PSQLException: The column index is out of range: 1, number of columns: 0.
if i remove " ' "(quotation mark) of circle then also it will not execute. query '' look like this :
SELECT addressid, geocode
FROM maddress
WHERE geocode::point <# circle ((?,?), ?);
it'll give following error :
Caused by: javax.xml.stream.XMLStreamException: DS Fault Message: Error in 'SQLQuery.processNormalQuery'
DS Code: DATABASE_ERROR
Source Data Service:-
Name: Geofence_DataService
Location: /Geofence_DataService.dbs
Description: N/A
Default Namespace: http://ws.wso2.org/dataservice
Current Request Name: geofence_op
Current Params: {longitude=72.8258, radius=4, latitude=18.9750}
Nested Exception:-
org.postgresql.util.PSQLException: ERROR: function circle(record, double precision) does not exist
Hint: No function matches the given name and argument types. You might need to add explicit type cast
But circle is inbuilt geographical function of PostgreSQL then is it necessary to write explicit function? else where is the exact error? Even if i put the query with input parameter as i execute in PostgreSQL then also it's working.If so then why it is not accepting dynamic parameters? Please let me know..
Geometric types can be input in multiple ways.
In the first form, your ? parameters are not replaced with values because they are literal parts of a string. So 0 parameters are expected ...
In the second form without single quotes, your ? parameters are replaced, but ((18.9750,72.8258), 5) is interpreted to be a row type, which doesn't work with circle().
You are trying to invoke the geometric function circle() that takes a point and a double precision ("center and radius to circle"). These are valid syntax variants:
SELECT circle '((18.9750,72.8258), 5)' AS cast_literal
' <(18.9750,72.82580),5>'::circle AS cast_literal2
, circle(point '(18.9750,72.8258)', '5') AS literal_point_n_radius
, circle(point(18.9750,72.8258), '5') AS point_n_literal_radius
, circle(point(18.9750,72.8258), 5) AS point_n_radius
SQL fiddle.
The cast to ::text is just to sanitize the deranged display in SQL fiddle
In your case, to provide numeric values (not a string literal), use the last form and it should work:
SELECT addressid, geocode
FROM maddress
WHERE geocode::point <# circle(point(?,?), ?);
If wso2dss (which I have no experience with) does not accept functions, you have to use one of the first two forms and provide a single parameter as string literal:
SELECT addressid, geocode
FROM maddress
WHERE geocode::point <# circle ?;
... where the parameter is the concatenated literal as displayed above.
You could let Postgres do the concatenation and still pass three numeric values:
SELECT addressid, geocode
FROM maddress
WHERE geocode::point <# ('(('::text || ? || ',' || ? || '),' || ? || ')')::circle;

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. :)