【发布时间】:2015-07-03 16:47:04
【问题描述】:
这是一个成功的例子:
with x as (
delete from common.companies where id = '0f8ed160-370a-47bb-b4bf-2dcf79100a52'
returning row_to_json(companies) as old_data, null as new_data, 'common.companies' as model, id, 'delete' as action)
insert into edit_history (old_data, new_data, model, model_pk, action, submitter)
select old_data, null, model, id, action, '0b392013-f680-45a6-b19a-34f3d42d0120' from x;
INSERT 0 1
请注意,插入选择中的第二列显式为空。
这是一个失败的例子:
with x as (
delete from common.companies where id = '160d7ef2-807c-4fe0-bfed-7d282c031610'
returning row_to_json(companies) as old_data, null as new_data, 'common.companies' as model, id, 'delete' as action)
insert into edit_history (old_data, new_data, model, model_pk, action, submitter)
select old_data, new_data, model, id, action, '0b392013-f680-45a6-b19a-34f3d42d0120' from x;
ERROR: failed to find conversion function from unknown to json
请注意,在此示例中,我得到的不是第二列中的显式 null,而是 new_data,它从 delete 语句中返回为 null。
如果两个值都为空,为什么第二个示例会因为这个错误让我大吃一惊?我已经仔细研究过了,这是唯一的功能差异。
【问题讨论】:
标签: sql postgresql casting common-table-expression