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.
Related
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
);
I am creating a table like this:
CREATE TABLE artist (
Id serial PRIMARY KEY,
NameNormalized varchar(256) NOT NULL UNIQUE,
Name text NOT NULL,
MusicBrainzId char(36) NULL,
Rating DECIMAL(2,1),
CONSTRAINT non_empty CHECK (length(NameNormalized) > 0 and length(Name) > 0)
);
When I try to execute the following INSERT operation, I get an error:
INSERT INTO artist (NameNormalized, Name, MusicBrainzId, Rating)
VALUES ('test', 'Test', '123456789012345678901234567890123456', 0.5)
ON CONFLICT (NameNormalized) DO NOTHING;
The error message is as follows:
ERROR: there is no unique or exclusion constraint matching the ON CONFLICT specification
I have done some research but to my knowledge, setting NameNormalized to UNIQUE should be enough for ON CONFLICT to work. Also I am pretty sure this has worked in the past for me.
This is my creation script:
CREATE TABLE PERSOANE (
idPers numeric (5)
CONSTRAINT pk_persoane PRIMARY KEY,
NumePren varchar (30)
CONSTRAINT ck_nume CHECK (NumePren=LTRIM(INITCAP(NumePren))),
Loc varchar (30)
CONSTRAINT nn_loc NOT NULL
CONSTRAINT ck_loc CHECK (Loc=LTRIM(INITCAP(Loc))),
Jud varchar (25)
CONSTRAINT nn_jud NOT NULL
CONSTRAINT ck_jud CHECK (Jud=LTRIM(INITCAP(Jud))),
Tel numeric (10)
CONSTRAINT nn_tel NOT NULL,
E_mail varchar(254)
CONSTRAINT nn_e_mail NOT NULL
CONSTRAINT ck_e_mail CHECK (E_mail = LTRIM(E_mail))
);
When I try to insert values in the table I got error: new row for relation "persoane" violates check constraint "ck_jud" Here is my insert script:
INSERT INTO PERSOANE VALUES (11111, 'slimi marius', 'oras', 'judet', 0752361507, 'simic#yahoo.com');
Anyone has any suggestion how to fix this problem?
initcap() will change the first character to uppercase. So your check constraints on the columns numepren, loc and jud require you to enter values where the first character of every word is in uppercase. In the value 'jude' the first character is lowercase, so the check constraint is violated. You need to use 'Jude' instead. This is also true for the other columns you have defined:
INSERT INTO PERSOANE
(idpers, numepren, loc, jud, tel, e_mail)
VALUES
(11111, 'Slimi Marius', 'Oras', 'Judet', 0752361507, 'simic#yahoo.com');
I would like to know how I could make sure that the value of SecondInstrument doesn't equal the value in PrimInstrument since a musician cannot be good with the same instrument twice. So far, this is what I have tried yet, and I got this error message:
02438. 00000 - "Column check constraint cannot reference other columns"
*Cause: attempted to define a column check constraint that references
another column.
*Action: define it as a table check constraint.
Could somebody help me out on this one? ;) Thanks in advance.
Create table SessionMusician (
Musician_ID number constraint pkSessionMus_Mus_ID Primary Key,
StageName varchar2(30)constraint nnSessionMus_StageName Not NULL,
RealName varchar2(30)constraint nnSessionMus_RealName Not NULL,
PrimInstrument varchar2(30) constraint nnSessionMus_PrimInstrumant Not Null,
SecondInstrument varchar2(30) Default 'N/A' Constraint
chk_SessionMus_Secinstrument check (SecondInstrument <> PrimInstrument),
CONSTRAINT SessionMusician_unique UNIQUE (StageName,RealName));
You need to make it an out-of-line constraint:
CREATE TABLE SessionMusician (
Musician_ID NUMBER CONSTRAINT pkSessionMus_Mus_ID PRIMARY KEY,
StageName VARCHAR2(30) CONSTRAINT nnSessionMus_StageName NOT NULL,
RealName VARCHAR2(30) CONSTRAINT nnSessionMus_RealName NOT NULL,
PrimInstrument VARCHAR2(30) CONSTRAINT nnSessionMus_PrimInstrumant NOT NULL,
SecondInstrument VARCHAR2(30) DEFAULT 'N/A',
CONSTRAINT SessionMusician_unique UNIQUE ( StageName, RealName ),
CONSTRAINT chk_SessionMus_Secinstrument CHECK (SecondInstrument <> PrimInstrument)
);
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.