Postgresql 为什么值不是自动生成的?
我将列定义为:Postgresql 为什么值不是自动生成的?,postgresql,Postgresql,我将列定义为: not null default uuid_generate_v4() 当我尝试插入NULL值时,出现错误: DBD::Pg::st execute failed: ERROR: null value in column "uuid" violates not-null constraint 守则: INSERT INTO files( uuid, storage, type, user_id, extra ) VALUES( ?,?,?,?
not null default uuid_generate_v4()
当我尝试插入NULL
值时,出现错误:
DBD::Pg::st execute failed: ERROR: null value in column "uuid" violates not-null constraint
守则:
INSERT INTO files( uuid, storage, type, user_id, extra )
VALUES( ?,?,?,?,? )
ON CONFLICT( uuid ) DO UPDATE SET
storage = EXCLUDED.storage,
type = EXCLUDED.type,
user_id = EXCLUDED.user_id,
extra = EXCLUDED.extra
RETURNING *
SQL
,$self->{ uuid }
,$self->storage .""
,ref $self
,$self->{ owner_id }
,JSON::XS->new->allow_nonref->encode( $args{ extra } // {} )
我很困惑。
如果尚未定义
$self->{uuid}
,如何创建新行。如果提供了$self->{uuid}
,如何更新记录
如果说uuid
为空,则db不会尝试使用默认值
必须省略uuid
字段,以便DB使用默认值
INSERT INTO files( storage, type, user_id, extra )
如果列是自动生成的,则无需将其添加到插入列列表中,只需将手动通知的列放入即可。如果希望
默认值
生效,从目标列表中删除uuid
列,但这样就永远不会出现冲突anyway@a_horse_with_no_name:在我的情况下,是否有方法进行更新或插入
?如果在插入过程中生成新的唯一ID,那么您希望更新什么?将不会有另一行具有相同的ID。@a_horse_和_no_名称:似乎我应该将此查询一分为二,并在应用程序中选中$self->{uuid}
,您必须省略uuid
:并且我将失去更新或插入
创建工作示例的能力,以便我可以尝试修复它。