14

I'm trying to import a csv into PostgreSQL database table.When i executing the following query:

enter image description here

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.

7
  • Have values for each row in v_id, or make the column nullable Commented Dec 20, 2019 at 20:08
  • @JGH v_id coloumn has primary key constraint and when i add v_id coloumn nullable, an error occurs "set primary_key feild as null=False" Commented Dec 20, 2019 at 20:13
  • a PK cannot be null. How should this value be created? Commented Dec 20, 2019 at 20:16
  • yes same issue stuck me a lot? Tell me the solution. Commented Dec 20, 2019 at 20:17
  • It depends on your use case. You can use a sequence, or you can populate the column in the csv.... or you can use another column for the PK, or... Commented Dec 20, 2019 at 20:20

2 Answers 2

16

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
);
Sign up to request clarification or add additional context in comments.

10 Comments

ERROR: column "v_id" is in a primary key SQL state: 42P16
i can't fill records in v_id because its our project criteria.
Or creating a temporary table as a working space to get things loaded and cleaned before copying to the final table. I find this two-step process to be easier than trying to fix the CSV or breaking existing table constraints.
@ShahnawazIrfan I've updated my answer with an alternative. You can re-create the table be adding a separated auto-cemented id and v_id as a normal column.
Dear, i can't set multiple primary keys field but another error occurs after executing "multiple primary keys for table trendmania_video" are not allowed.
|
0

if you have this autogenerated id in your entity , just delete it : @GeneratedValue(strategy = GenerationType.IDENTITY)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.