Postgres: SQL Error [42883]: ERROR: operator does not exist: uuid = text

Postgres: SQL Error [42883]: ERROR: operator does not exist: uuid = text

下面我有一个 Postgres 查询,它根据 INSERT 或 UPDATE audit_operation 将主要 table 中的数据与审计 table 中的数据一起恢复到特定时间点。

create or replace function fun(test_val1_input text, test_val3_input text)
returns void
as $functions$
declare test_id text ;
    test_val1 text ;  
    test_val2 text ; 
    test_val3 timestamp;
    test_val4 text ;
declare cur cursor 
for select id, val1 , val2 , val3, val4
    from test_table at 
    where val1 = UUID() and val3 > to_timestamp(, 'YYYY-MM-DD HH24:MI:SS')
    order by val3 desc;
begin   
open cur;

fetch next from cur into test_id, test_val1 , test_val2 , test_val3 , test_val4;

while found
loop

    if (test_val4 = 'INSERT')
    then
        delete from main_table mt where id = test_val1;
    elsif (test_val4 = 'UPDATE')
    then
        delete from main_table mt where id = test_val1;
        with cte
        as 
        (
            select * 
            from test_table at
            where val1 = test_val1 and val3 < test_val3 
            order by val3 desc
            limit 1
        )
        update mt 
        set mt.id = cte.id,
            mt.val1 = cte.val1,
            mt.val2= cte.val2
        from main_table mt
             join cte on cte.val1 = brb.val1; 
    end if;

    fetch next from cur into test_id, test_val1, test_val2, test_val3, test_val4;
end loop;

close cur;
end;
$functions$ language plpgsql;

供参考-

主要table

     id       |         val1           |  val2
--------------+------------------------+---------
31cc5a4f-7a23 | 4d87-ad12-2f78c1c52b7a | data_1
12da6b6a-8b12 | 4d87-ad12-2f78c1c52b7a | data_2
82na1q1a-1b45 | 4d87-ad12-2f78c1c52b7a | data_3

Type of columns in the main_table

id: uuid

val1: uuid

val2: text

审计table

     id       |           val1         |  val2   |    val3             | val4
--------------+------------------------+---------+---------------------+------------------
31cc5a4f-7a23 | 4d87-ad12-2f78c1c52b7a | data_1  | 2001-09-10 12:02:20 |      INSERT
12da6b6a-8b12 | 4d87-ad12-2f78c1c52b7a | data_2  | 2001-09-10 12:02:20 |      INSERT      
82na1q1a-1b45 | 4d87-ad12-2f78c1c52b7a | data_3  | 2001-09-12 15:12:54 |      INSERT      

Type of columns in the audit_table

id: uuid

val1: uuid

val2: text

val3: timestamp

val4: text

在为以下输入执行上述 SQL 函数时-

select fun('4d87-ad12-2f78c1c52b7a', '2001-09-10 12:02:20')

我收到以下错误:

SQL Error [42883]: ERROR: operator does not exist: uuid = text

Hint: No operator matches the given name and argument types. You might need to add explicit type casts.

Where: PL/pgSQL function revert_business_rule_days(text,text) line 23 at SQL statement

相反,我希望在指定时间恢复主要 table 中的数据:

主要table

     id       |         val1           |  val2  
--------------+------------------------+---------
31cc5a4f-7a23 | 4d87-ad12-2f78c1c52b7a | data_1
12da6b6a-8b12 | 4d87-ad12-2f78c1c52b7a | data_2

请帮助我,让我知道我哪里出错了,我将不胜感激!

此外,如果您需要对此有更多了解,请告诉我。

来自 Postgresql documentation:

A UUID is written as a sequence of lower-case hexadecimal digits, in several groups separated by hyphens, specifically a group of 8 digits followed by three groups of 4 digits followed by a group of 12 digits, for a total of 32 digits representing the 128 bits.

PostgreSQL also accepts the following alternative forms for input: use of upper-case digits, the standard format surrounded by braces, omitting some or all hyphens, adding a hyphen after any group of four digits.

你应该更正:

  • 当您创建函数时,它的名称是 fun 而不是 revert_business_rule_days.

  • 变量v_idv_pk_id的类型应该是UUID而不是TEXT错误发生在DELETE语句的WHERE子句中,因为id列类型为UUID,变量v_id类型为文本。

  • 您的 UPDATE 查询应该类似于(您的语法不正确):

UPDATE main_table mt 
SET id = cte.id,
    pk_id = cte.pk_id,
    col_1= cte.col_1
FROM cte 
WHERE cte.pk_id = mt.pk_id;