3

I have a table that I want to add new not null varchar(255) column

My query is:

alter table poll_management.DASHLETS add column name varchar(255) not null;
update poll_management.DASHLETS as dashlet set name = report.name 
from poll_management.REPORTS as report
WHERE dashlet.id = report.reportdashletid 

But I have an error:

ERROR:  column "name" contains null values
********** Error **********

ERROR: column "name" contains null values
SQL state: 23502
3
  • either add column with null, then update values and alter to not null, or add column not null default 'somevalue'; Commented Apr 30, 2017 at 9:43
  • You have added "name" column as not null and as per the error it says that in report table some values of "name" column is null. Commented Apr 30, 2017 at 9:47
  • Does this answer your question? How can I add a column that doesn't allow nulls in a Postgresql database? Commented Oct 29, 2022 at 2:43

2 Answers 2

4

To avoid your error, two solutions come at once:

BEGIN;
  alter table poll_management.DASHLETS add column name varchar(255);
  update poll_management.DASHLETS as dashlet set name = report.name 
from poll_management.REPORTS as report;
  --mind I removed where, cos you need to update ALL rows to have some avlue
  alter table poll_management.DASHLETS alter column "name" set not null;
END;

and the other:

  alter table poll_management.DASHLETS add column name varchar(255) NOT NULL default 'not set';
Sign up to request clarification or add additional context in comments.

1 Comment

I think the query alter table poll_management.DASHLETS alter column "name" not null; must be changed to SET not null
0

i found that my query must be change to :

alter table poll_management.DASHLETS add column name varchar(255) not null DEFAULT 'value';

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.