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
);
Related
I've the following table:
CREATE TABLE api.capabilities (
id uuid NOT NULL,
historycounter int8 NOT NULL DEFAULT 0,
url jsonb NOT NULL,
checkservice bool NOT NULL DEFAULT false,
added timestamptz NOT NULL,
modified timestamptz NULL,
deleted timestamptz NULL,
adduser varchar(40) NOT NULL DEFAULT 'pgsql'::character varying,
lastuser varchar(40) NULL,
CONSTRAINT id_histcount PRIMARY KEY (id, historycounter)
);
With the following content:
|80af3ff3-2dc1-434b-ad3c-490d8b4a7949|1|{"host": "dev.33wessling824.net", "protocol": "http"}|false|2022-07-12 18:35:17.465 +0200|||pgsql||
|80af3ff3-2dc1-434b-ad3c-490d8b4a7949|0|{"host": "dev.33wessling824.net", "protocol": "http"}|true|2022-07-12 18:35:17.465 +0200|2022-07-14 11:46:50.073 +0200||pgsql||
I'd like to run this update statement:
Update api.capabilities set historycounter = historycounter + 1 where id = '80af3ff3-2dc1-434b-ad3c-490d8b4a7949';
Trying this results in an error duplicate key value violates unique constraint "id_histcount"
Because first the dataset with historycounter = 0 is updated, I do get this error. How to change my code to first update the dataset with the biggest historycounter?
Thanks in advance! Achim
If you are totally sure that your data will not violate primary key after your transaction. First, alter table constraint. This is safe:
ALTER TABLE api.capabilities
ALTER CONSTRAINT id_histcount DEFERRABLE INITIALLY IMMEDIATE;
Then in your transaction:
begin transaction;
set constraints all deferred;
Update api.capabilities set historycounter = historycounter + 1 where id = '80af3ff3-2dc1-434b-ad3c-490d8b4a7949';
--your other operations
commit transaction;
Source: https://www.postgresql.org/docs/current/sql-set-constraints.html
A table in production environment was created like this:
-- DDL generated by Postico 1.5.10
-- Not all database features are supported. Do not use for backup.
-- Table Definition ----------------------------------------------
CREATE TABLE "TempUser" (
id bigint PRIMARY KEY,
"Uuid" text NOT NULL,
"dateCreated" timestamp without time zone NOT NULL,
"lastRequestDate" timestamp without time zone NOT NULL,
"lastRequestLocation" text,
"numOfRequest" bigint NOT NULL,
comment text NOT NULL,
"lastRequestDescription" text NOT NULL,
"bookedSeats" text NOT NULL,
"bookUntil" timestamp without time zone,
"lastEventVisited" text
);
-- Indices -------------------------------------------------------
CREATE UNIQUE INDEX "pk:TempUser.id" ON "TempUser"(id int8_ops);
Why I remove table in dev environment and create like this in Postgres console, I got following error after try writing programmatically:
[ ERROR ] PostgreSQLError.server.error.ExecConstraints: POST /registerTempUser null value in column "id" violates not-null constraint (ErrorMiddleware.swift:26)
[ DEBUG ] Possible causes for PostgreSQLError.server.error.ExecConstraints: Failing row contains (null, 015574220836500, 2020-07-09 19:42:36.803846, 2020-07-09 19:42:36.803846, null, 1, , POST /registerTempUser HTTP/1.1
Host: localhost:8080
Content-Typ..., [], null, null). (ErrorMiddleware.swift:26)
Why, what is wrong?
Tried set sequence thing:
select setval(pg_get_serial_sequence('"TempUser"', 'id'), 1);
select currval(pg_get_serial_sequence('"TempUser"', 'id'));
nothing changed.
I am using the COPY FROM command to load data from a file.
The table is defined with identity column, which is not part of the file.
CREATE TABLE APP2DBMAP (
FIELD_ID integer NOT NULL GENERATED BY DEFAULT AS IDENTITY,
FIELD_NAME varchar(128) ,
TABLE_NAME varchar(128) ,
COLUMN_NAME varchar(128) ,
CONSTRAINT PK_APP2DBMAP PRIMARY KEY ( FIELD_ID )
);
I executed the following COPY FROM command, the file contains 3 values in 1 row.
copy app2dbmap (field_name, table_name, column_name) from '/opt/NetMgr/data/templ_db.txt' DELIMITER ',' ;
And I got the following error:
ERROR: null value in column "field_id" violates not-null constraint
DETAIL: Failing row contains (null, 'aaa', 'bbb', 'ccc').
CONTEXT: COPY app2dbmap, line 1: "'aaa','bbb','ccc'"
I tried to change the column description of field_id to serial, and it did work fine.
I don't understand why it doesn't work with the original table definition.
The problem is you have specified the field_id to be a not null value and hence when the file is passing null as a value, your error is there.
If you want an auto increment id, Use,
CREATE TABLE APP2DBMAP (
FIELD_ID smallserial NOT NULL,
FIELD_NAME varchar(128) ,
TABLE_NAME varchar(128) ,
COLUMN_NAME varchar(128) ,
CONSTRAINT PK_APP2DBMAP PRIMARY KEY ( FIELD_ID )
);
You can also use bigserial(int4) instead of smallint(int8)
or you can give a default value,
CREATE TABLE APP2DBMAP (
FIELD_ID integer NOT NULL default 0,
FIELD_NAME varchar(128) ,
TABLE_NAME varchar(128) ,
COLUMN_NAME varchar(128) ,
CONSTRAINT PK_APP2DBMAP PRIMARY KEY ( FIELD_ID )
);
You will have to pass something, you cannot pass null in a not null column
Below example table Stream_types gives an ERROR: there is no unique constraint matching given keys for referenced table, and having stared at it for while now I can't figure out why this error arises in this situation.
create table Stream_types (
id integer NOT NULL,
career careertype NOT NULL,
code character(1) NOT NULL,
description shortstring NOT NULL
);
create table Streams (
id integer, -- PG: serial
code char(6) not null, -- e.g. COMPA1, SENGA1
name LongName not null,
offeredBy integer references OrgUnits(id),
stype ShortString references Stream_types(id),
description TextString,
firstOffer integer references Semesters(id), -- should be not null
lastOffer integer references Semesters(id), -- null means current
primary key (id)
);
The error is:
ERROR: there is no unique constraint matching given keys for referenced table "stream_types"
********** Error **********
ERROR: there is no unique constraint matching given keys for referenced table "stream_types"
SQL state: 42830
I'm using postgresql 9.3.16, can anyone help me fix this?
change create table Stream_types statement like here:
create table Stream_types (
id integer NOT NULL PRIMARY KEY,
career careertype NOT NULL,
code character(1) NOT NULL,
description shortstring NOT NULL
);
I am trying to import data to a table using COPY from a csv file. This is the table in which I want to import:
CREATE TABLE public.forms_member_registration
(
baseformmodel_ptr_id integer NOT NULL,
"Agrihub" character varying(200) NOT NULL,
"Ward_Number" character varying(300) NOT NULL,
"Area" character varying(300) NOT NULL,
"First_Name" character varying(300) NOT NULL,
"Last_Name" character varying(300) NOT NULL,
"Other_Name" character varying(300) NOT NULL,
-----------snip--------------------------------
"L3_Modules_Completed" character varying(200),
"L3_Specify_Other" character varying(300) NOT NULL,
gps_location geometry(Point,4326),
CONSTRAINT forms_member_registration_pkey
PRIMARY KEY (baseformmodel_ptr_id),
CONSTRAINT baseformmodel_ptr_id_refs_id_c03f6c72
FOREIGN KEY (baseformmodel_ptr_id)
REFERENCES public.forms_baseformmodel (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION DEFERRABLE INITIALLY DEFERRED
)
The primary key is referencing this table:
CREATE TABLE public.forms_baseformmodel
(
id integer NOT NULL DEFAULT nextval('forms_baseformmodel_id_seq'::regclass),
user_id integer NOT NULL,
created_at timestamp with time zone NOT NULL,
CONSTRAINT forms_baseformmodel_pkey
PRIMARY KEY (id),
CONSTRAINT user_id_refs_id_3a410ec9
FOREIGN KEY (user_id)
REFERENCES public.auth_user (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION DEFERRABLE INITIALLY DEFERRED
)
I am using this copy command:
COPY forms_member_registration("Agrihub", "Ward_Number", "Area","First_Name", "Last_Name", "Other_Name", "SA_ID_Number", "Gender", "Phone_Number") FROM '/opt/project/migration/file-3.csv' DELIMITER ',' CSV HEADER;
Giving this error:
ERROR: null value in column "baseformmodel_ptr_id" violates not-null constraint
So the problem as I see it is that "baseform_ptr_id" needs to be retrieved from the id column of the forms_baseformmodel table for each entry but id only gets created when an entry is made to forms_baseformmodel.
How can I create the entry in forms_baseformmodel, retrieve it and add it to the tuple being copied?
Hope that makes sense... This is all kinda new for me.
Thanks in advance
This is a rather common problem. What you must do is:
COPY the data to a TEMPORARY or UNLOGGED table;
INSERT INTO real_table SELECT ... FROM temp_table INNER JOIN other_table ...
In other words, copy to a staging table, then generate the real data set with a join and insert the join product into the real table.
It's somewhat related to the bulk upsert problem.
So in your case you'd create a temp_forms_member_registration, copy the csv into it including the user_id column you wish to replace, then:
INSERT INTO forms_member_registration(
baseformmodel_ptr_id,
"Agrihub",
...
)
SELECT
fbfm.id,
tfmr."Agrihub",
...
FROM temp_forms_member_registration tfmr
INNER JOIN forms_baseformmodel ON (tfmr.user_id = fbfm.user_id);