2017-05-03 49 views
0

我已经列定义为:为什么价值不是自动生成的?

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 }是提供

+0

如果列自动生成的,你不需要添加它在插入列清单,只是把那些你手动通知。 –

+0

如果你想'默认'踢,从目标列表中删除'uuid'列 - 但然后你永远不会有冲突 –

+0

@a_horse_with_no_name:有没有办法使'UPDATE或INSERT'在我的情况下? –

回答

1

NULLDEFAULT

不同。如果你说uuid为NULL,则数据库不会尝试使用默认值。

必须省略uuid字段,以便DB使用DEFAULT

INSERT INTO files(storage, type, user_id, extra) 
+0

'您必须省略uuid':并且我失去了'更新或插入'的能力 –

+0

创建一个工作示例在[rextester](http://rextester.com/l/postgresql_online_compiler)中,所以我可以尝试修复它。 –