PostgreSQL:如何将空值插入到uuid

use*_*223 5 postgresql null sql-insert

需要向uuid类型没有NOT NULL规范(非主键)的字段插入空值。

当我尝试插入 '' 时,返回:

ERROR:  invalid input syntax for uuid: ""
Run Code Online (Sandbox Code Playgroud)

当我尝试插入 null 时,返回:

ERROR:  null value in column "uuid" violates not-null constraint
Run Code Online (Sandbox Code Playgroud)

怎么做?

psql 9.3.5

SQL:

INSERT INTO inv_location (address_id)
VALUES (null)
Run Code Online (Sandbox Code Playgroud)

Mic*_*man 9

在Postgres中使用uuid_nil()函数模拟空uuid(与00000000-0000-0000-0000-000000000000

INSERT INTO inv_location (address_id)
VALUES (uuid_nil())
Run Code Online (Sandbox Code Playgroud)

您可能需要有 uuid 扩展名(不确定),如果需要,请运行此命令(仅一次):

create extension if not exists "uuid-ossp";
Run Code Online (Sandbox Code Playgroud)


Erw*_*ter 7

如果已定义该列NOT NULL,则无法输入NULL值。时期。

在此错误消息中:

错误:“uuid”列中的空值违反了非空约束

“uuid”是列的名称,而不是数据类型address_id。该列定义为NOT NULL

uuid         | character varying(36) | not null
Run Code Online (Sandbox Code Playgroud)

您的INSERT语句在目标列表中不包含“uuid”,因此在没有不同的列默认值的情况下,NULL 默认为 NULL。繁荣。

去展示用作标识符的基本类型名称(ab)如何导致令人困惑的错误消息。