Sql Can';t插入postgres列
我有一个具有以下架构的表:Sql Can';t插入postgres列,sql,postgresql,Sql,Postgresql,我有一个具有以下架构的表: Column | Type | Collation | Nullable | Default | Storage | Stats target | Description ----------------------------+-----------------------------+-----------+---
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列设置为“notnull”,所以我不确定它为什么会抛出这个错误。特别是因为只有28行,这似乎会在第31行上引发错误。您一次插入整个记录,因此当您为一列插入并指定一个值时,它会隐式地假定所有其余的都为空。您需要插入完整的记录,并且insert语句中没有任何空值
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
正如您的表定义清楚地显示的那样,您确实创建了一个表,该表基于您的模式为
orig_filename
设置了not null
约束,直到并包括file_data
之前的所有列都不可为null,这意味着您必须在插入时提供一个值。我确实觉得有点讽刺的是,您为其提供值的一列是可空的(将允许空值)。也许您的DDL脚本错误,并将值或值的含义反转为可空的?是的,只是。。并非所有列数据类型都是textual@OtoShavadze然后花几秒钟,选择一些默认值,并将它们用作占位符?