3

我有一个要添加新not null varchar(255)列的表

我的查询是:

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 

但我有一个错误:

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

ERROR: column "name" contains null values
SQL state: 23502
4

2 回答 2

3

为避免您的错误,同时提供两种解决方案:

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;

和另一个:

  alter table poll_management.DASHLETS add column name varchar(255) NOT NULL default 'not set';
于 2017-04-30T09:46:43.710 回答
0

我发现我的查询必须更改为:

alter table poll_management.DASHLETS add column name varchar(255) not null DEFAULT 'value';
于 2017-04-30T09:46:50.867 回答