PostgreSQL:如何向 uuid 插入空值

标签 postgresql null sql-insert

需要在没有NOT NULL规范(不是主键)的uuid类型的字段中插入空值。

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

ERROR:  invalid input syntax for uuid: ""

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

ERROR:  null value in column "uuid" violates not-null constraint

怎么做?

psql 9.3.5

SQL:

INSERT INTO inv_location (address_id)
VALUES (null)

最佳答案

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

INSERT INTO inv_location (address_id)
VALUES (uuid_nil())

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

create extension if not exists "uuid-ossp";

关于PostgreSQL:如何向 uuid 插入空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26992821/

相关文章:

ruby-on-rails - 如何随机化 Rails 查找器方法的结果排序?

java - SonarQube 显示可能的空指针取消引用

mysql - 在使用 COUNT() 和 GROUP BY 时如何包含 NULL 计数,因为 COUNT 会忽略 MySQL 中的 NULL?

sql - 将三个表连接在一起得到一个值

sql - 与另一个表的最大值合并

postgresql - Ubuntu 20.04 上的 PgAdmin fatal error : password authentication failed for user

用C连接Postgresql

postgresql - 无法打开 SELECT 查询作为游标

ruby - 仅在非 nil 时才分配变量

Mysql:错误代码:1452。无法添加或更新子行:外键约束失败