无法插入到postgres列

时间:2018-08-20 20:19:26

标签: sql postgresql

我有一个具有以下架构的表:

           Column           |            Type             | Collation | Nullable |                Default                 | Storage  | Stats target | Description
----------------------------+-----------------------------+-----------+----------+----------------------------------------+----------+--------------+-------------
 id                         | integer                     |           | not null | nextval('test_table_id_seq'::regclass) | plain    |              |
 orig_filename              | text                        |           | not null |                                        | extended |              |
 file_extension             | text                        |           | not null |                                        | extended |              |
 created_date               | date                        |           | not null |                                        | plain    |              |
 last_modified_date         | date                        |           | not null |                                        | plain    |              |
 upload_timestamp_utc       | timestamp without time zone |           | not null |                                        | plain    |              |
 uploaded_by                | text                        |           | not null |                                        | extended |              |
 file_size_in_bytes         | integer                     |           | not null |                                        | plain    |              |
 original_containing_folder | text                        |           | not null |                                        | extended |              |
 file_data                  | bytea                       |           | not null |                                        | extended |              |
 source_shortname           | text                        |           |          |                                        | extended |              |
Indexes:
    "test_table_pkey" PRIMARY KEY, btree (id)

我在构建表后附加了source_shortname列。我现在想将值插入列中。

当我运行此命令时:

INSERT INTO test_table(source_shortname) VALUES('name');

我收到此错误:

ERROR:  null value in column "orig_filename" violates not-null constraint
DETAIL:  Failing row contains (31, null, null, null, null, null, null, null, null, null, name).

我没有将source_shortname列设置为“ not null”,所以我不确定为什么会引发该错误。特别是因为只有28行,这似乎在第31行上引发了错误。

1 个答案:

答案 0 :(得分:0)

您一次要插入整个记录,因此,当您为一列插入并仅指定一个值时,它将隐式假定所有其余值均为NULL。您需要插入完整的记录,并且插入语句中没有任何NULL值。

INSERT INTO test_table( 
 orig_filename             
 ,file_extension             
 ,created_date               
 ,last_modified_date         
 ,upload_timestamp_utc       
 ,uploaded_by                
 ,file_size_in_bytes         
 ,original_containing_folder 
 ,file_data                  
 ,source_shortname)
VALUES 
('','','','','','','','','','name')   

或者您需要运行更新,因为您刚刚添加了该列,并且想在该列中插入一些内容

UPDATE test_table 
SET source_shortname = 'name'
WHERE source_shortname IS NULL