PostgreSQL: how to insert null value to uuid - postgresql

Need to insert null value to field with uuid type without NOT NULL specification (not primary key).
When I try insert '', this return:
ERROR: invalid input syntax for uuid: ""
When I try insert null, this return:
ERROR: null value in column "uuid" violates not-null constraint
How to do it?
psql 9.3.5
SQL:
INSERT INTO inv_location (address_id)
VALUES (null)

In Postgres use uuid_nil() function to simulate empty uuid (same as 00000000-0000-0000-0000-000000000000)
INSERT INTO inv_location (address_id)
VALUES (uuid_nil())
You might need to have uuid extension (not sure), if you do, run this (only once):
create extension if not exists "uuid-ossp";

If the column is defined NOT NULL, you cannot enter a NULL value. Period.
In this error message:
ERROR: null value in column "uuid" violates not-null constraint
"uuid" is the name of the column, not the data type of address_id. And this column is defined NOT NULL:
uuid | character varying(36) | not null
Your INSERT statement does not include "uuid" in the target list, so NULL is defaults to NULL in absence of a different column default. Boom.
Goes to show how basic type names (ab)used as identifier lead to confusing error messages.

Related

How to convert an existing column to a foreign key that allows null values - PostgreSQL

I have a Postgres database with table t and column fk_c. I want to convert the column to a foreign key that references c_id in table lookup_c and allows null values. How can I do this?
ALTER TABLE public.t ADD CONSTRAINT "fk_t_c" FOREIGN KEY ("fk_c" ) REFERENCES "public"."lookup_c" ("c_id");
does not work because there rows with null values in column fk_c and I get
ERROR: insert or update on table "t" violates foreign key constraint "fk_t_c"
DETAIL: Key (fk_c)=() is not present in table "lookup_c".
The error message indicates that you have empty strings in that column, not null values.
You need to set those values to null before creating the foreign key:
update t
set fk_c = null
where trim(fk_c) = '';

null value in column violates not-null constraint PostgreSQL

I'm trying to import a csv into PostgreSQL database table.When i executing the following query:
My table name is trendmania_video
COPY public.trendmania_video FROM 'C:\Users\Shahnawaz Irfan\Desktop\0.csv' DELIMITER ',' CSV HEADER;
a following error occurred:
ERROR: null value in column "v_id" violates not-null constraint
DETAIL: Failing row contains (null, null, UgzYr_WZlR73yFBnRdx4AaABAg, yar
kindly ap urdu m b toturial bna lety wordpress k liye to hma..., null, null,
null, null, null, null, null, null, null, null).
CONTEXT: COPY trendmania_video, line 10: ",,UgzYr_WZlR73yFBnRdx4AaABAg,yar
kindly ap urdu m b toturial bna lety wordpress k liye to hmari b he..."
SQL state: 23502
I also tried manually by using import button, but same error occurs.
In your table trendmania_video, you have v_id to be not null which causes this issue. You one option is to get ride of the not null constrain:
ALTER TABLE public.trendmania_video ALTER COLUMN v_id DROP NOT NULL;
If this is a new table then it's better to recreate it with a new table with an auto-cremented id while v_id is another value.
CREATE TABLE trendmania_video(
id SERIAL PRIMARY KEY,
v_id VARCHAR
--the rest of the columns
);

SymmetricDS and Postgres with UUID primary key failed (invalid input syntax for type uuid)

I am using Postgres 11 and SymmetricDS 3.9.14.
I have a database table with primary key of UUID type. It seems like SymmetricDS is not able to cast 'UUID' correctly. One of SymmetricDS replication query is failing with the below error
JdbcSqlTemplate - SQL caused exception: [select "id" from "dbo"."groups" where "id"=?]
sql args: [ ]
org.postgresql.util.PSQLException: ERROR: invalid input syntax for type uuid: " "
my insert statement :-
INSERT INTO dbo.groups(
id, sortorder, name, hidden, sessionid, creationtime, modificationtime, regionid)
VALUES ('5A171D3F-F6A6-4D09-AE89-73B5793DA171', 1, 'abc', false, null,'2018-11-20 20:25:49.663', null, null);
my database table is :-
CREATE TABLE dbo.groups
(
id uuid NOT NULL,
sortorder integer NOT NULL DEFAULT 0,
name character varying(80) COLLATE pg_catalog."default",
hidden boolean NOT NULL DEFAULT false,
sessionid uuid,
creationtime timestamp without time zone DEFAULT CURRENT_TIMESTAMP,
modificationtime timestamp without time zone,
regionid uuid,
CONSTRAINT "PK_dbo.Groups" PRIMARY KEY (id)
)
EDIT:
My source database is MS SQL Server and target database is Postgres
The value of UUID in this case is a space or tab as it could be seen from the error message
invalid input syntax for type uuid: " "
Try finding why instead of the concrete UUID value this space/tab value is passed

No function matches the given name and argument types. You might need to add explicit type casts

I have created table in Postgres like as below.
CREATE TABLE rv_data_bkt_country_m
(
interval_start timestamp without time zone NOT NULL,
creative_id integer NOT NULL,
zone_id integer NOT NULL,
country character(3) NOT NULL DEFAULT ''::bpchar,
city character(50) NOT NULL DEFAULT ''::bpchar,
count integer NOT NULL DEFAULT 0,
CONSTRAINT rv_data_bkt_country_m_pkey PRIMARY KEY (interval_start, creative_id, zone_id, country, city)
)
When I insert using
SELECT bucket_update_rv_data_bkt_country_m('2018-03-14 08:00:00,1,1,'IN','',1)"
this query. It shows following error
ERROR: function bucket_update_rv_data_bkt_country_m(unknown, integer, integer, unknown, unknown, integer) does not exist
LINE 1: SELECT bucket_update_rv_data_bkt_country_m('2018-03-14 08:00...
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
How to solve this error?

On Conflict Do Nothing in Postgres with a not-null constraint

Is it not possible to utilize "ON CONFLICT DO NOTHING" to avoid inserting and violating a not-null constraint?
For example...
INSERT INTO public.users (user, user_yob, sex)
SELECT mom, mom_yob, 'F'
FROM staging.users
ON CONFLICT DO NOTHING;
Produces this error and stops...
INSERT INTO public.users (user, user_yob, sex) SELECT mom, mom_yob, 'F' FROM staging.users ON CONFLICT DO NOTHING
> ERROR: null value in column "user" violates not-null constraint
DETAIL: Failing row contains (0b159b81-6842-4ae7-961c-2e9cff8488b1, null, null, null, null, null, null, null, null, null, F).
Ideally, this particular insert would be ignored rather than raise the error.
As the documentation says:
The optional ON CONFLICT clause specifies an alternative action to raising a unique violation or exclusion constraint violation error.
NOT NULL is neither a unique nor an exclusion constraint. Technically speaking, it is not even a proper constraint in PostgreSQL.
What you need is a BEFORE INSERT trigger that returns NULL if the NEW row contains offending null values.