I encountered an issue with casting in my application while using PostgreSQL. Let me provide a specific example to illustrate the problem.
When I execute the following query:
UPDATE intent SET status = ? WHERE id = ?;
It works without any problems on my AWS server. However, when I attempt to run it on my local machine, I encounter a casting error:
nested exception is org.postgresql.util.PSQLException: ERROR: column "status" is of type intent_status but expression is of type character varying Hint: You will need to rewrite or cast the expression
My main question is why does the query require casting only on my local machine, considering that both environments have the same PostgreSQL version?
Current PostgreSQL version:
PostgreSQL 12.14 (Ubuntu 12.14-0ubuntu0.20.04.1) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0, 64-bit
Table "platform.intent"
----------------------------+-------------------------------+-----------+----------+---------------------------------------------
id | integer | | not null | nextval('platform.intent_id_seq'::regclass)
org_id | integer | | not null |
bot_id | integer | | not null |
added_by | integer | | not null |
added_date | timestamp without time zone | | not null | now()
status | platform.intent_status | | not null | 'ACTIVE'::platform.intent_status
flow | jsonb | | not null | '{}'::jsonb
nlu_label_id | integer | | |
webhook_settings | jsonb | | not null | '{}'::jsonb
matrix_settings | jsonb | | not null | '[]'::jsonb
type | platform.intent_type | | not null | 'FLOW'::platform.intent_type
expiry_settings | jsonb | | not null | '{}'::jsonb
closed_cycle_settings | jsonb | | not null | '{}'::jsonb
event_logging_level | platform.intent_logging_level | | not null | 'ENTER_EXIT'::platform.intent_logging_level
failure_action | jsonb | | | '{}'::jsonb
has_smart_search | boolean | | not null | false
switching_settings | jsonb | | not null | '{}'::jsonb
queuing_settings | jsonb | | not null | '{}'::jsonb
trigger_condition_settings | jsonb | | not null | '{}'::jsonb
Indexes:
"intent_pkey" PRIMARY KEY, btree (id)
"id_bot_id" btree (bot_id)
"idx_bot_org_id" btree (org_id, bot_id)
"idx_intent_type" btree (type)
"idx_org_id" btree (org_id)
"intent_expr_idx" btree ((((flow -> 'arabot_flow'::text) -> 'data'::text) ->> 'name'::text))
Foreign-key constraints:
"fk_botid" FOREIGN KEY (bot_id) REFERENCES platform.bot(id) ON DELETE CASCADE NOT VALID
"fk_orgid" FOREIGN KEY (org_id) REFERENCES platform.organization(id) ON DELETE CASCADE
Java Code (JdbcTemplate)
public void updateEntity(Intent intent) {
try {
jdbcTemplate.update("update intent set status = ?, flow = to_json(?::json), webhook_settings = to_json(?::json) , expiry_settings = to_json(?::json)," +
" closed_cycle_settings = to_json(?::json) , event_logging_level = ?::intent_logging_level , failure_action = to_json(?::json), switching_settings = to_json(?::json), queuing_settings = to_json(?::json), trigger_condition_settings = ?::jsonb " +
" where id = ?",
intent.getStatus().name(),
intent.getFlow().toString(),
new FlowReader().writeNode(intent.getWebhookSettings()),
new FlowReader().writeNode(intent.getExpirySettings()),
new FlowReader().writeNode(intent.getClosedCycleSettings()),
intent.getEventLoggingLevel().name(),
new FlowReader().writeNode(intent.getFailureAction()),
new FlowReader().writeNode(intent.getSwitchingSettings()),
new FlowReader().writeNode(intent.getQueuingSettings()),
new FlowReader().writeNode(intent.getTriggerConditionSettings()),
intent.getId());
} catch (JsonProcessingException e) {
throw new ServerErrorException(String.format("Failed to update intent due to error while parsing webhook settings: %s ", e.getMessage()), e);
}
}