简体   繁体   English

在使用用PL / Python编写的布尔存储过程的PostgreSQL CHECK约束中,是否可以更新错误消息的详细信息?

[英]In a PostgreSQL CHECK constraint using a boolean stored procedure written in PL/Python, can the detail of the error message be updated?

The qustion's in the (albeit long) title. qustion在(尽管很长)的标题中。 I have code in this fashion: 我有这样的代码:

-- create the tables for models
CREATE TABLE invoice(
    id SERIAL PRIMARY KEY,
    value VARCHAR(8)
      CONSTRAINT valid_money CHECK(validate_invoice_money(value))
);

with this procedure: 用这个程序:

CREATE OR REPLACE FUNCTION validate_invoice_money(test VARCHAR) RETURNS BOOLEAN AS $$
    import plpy
    import re

    if (re.match("^[0-9]+\\.[0-9]{2}$", test) == None):
        return False

    return True
$$ LANGUAGE plpython3u IMMUTABLE;

(the specifics of this example are not important, I know there's better ways to validate a value than this, such as using the MONEY type.) (这个例子的细节并不重要,我知道有更好的方法来验证一个值,比如使用MONEY类型。)

When an insert is attempted that fails this check, I get the following error: 当尝试插入未通过此检查时,我收到以下错误:

ERROR  : new row for relation "invoice" violates check constraint "valid_model"
DETAIL : Failing row contains (1, "notvalid").

(the error/detail descriptors are my own code, but a PostgreSQL error has an error and a detail field provided.) (错误/详细描述符是我自己的代码,但PostgreSQL错误有错误,并提供详细信息字段。)

Is there any way for me to change the "detail" portion of this error from my Python procedure? 有没有办法让我从Python程序中更改此错误的“详细信息”部分?

In Postgres 9.6+ you can raise an error with detailed messages from a plpython function using utility functions, eg: 在Postgres 9.6+中,您可以使用实用程序函数从plpython函数中提取详细消息例如:

CREATE OR REPLACE FUNCTION validate_invoice_money(test VARCHAR) 
RETURNS BOOLEAN AS $$
    import re

    if (re.match("^[0-9]+\\.[0-9]{2}$", test) == None):
        plpy.error("custom exception message",
            detail="some info about exception",
            hint="hint for users")
        return False

    return True
$$ LANGUAGE plpython3u IMMUTABLE;

insert into invoice
values (default, 'notvalid');

ERROR:  plpy.Error: custom exception message
DETAIL:  some info about exception
HINT:  hint for users
CONTEXT:  Traceback (most recent call last):
  PL/Python function "validate_invoice_money", line 8, in <module>
    hint="hint for users")
PL/Python function "validate_invoice_money"

You don't need an additional language; 你不需要额外的语言; you can use a plain regexp for the value constraint: 你可以使用普通正则表达式来限制值:


-- create the tables for models
CREATE TABLE invoice(
    id SERIAL PRIMARY KEY,
    val VARCHAR(8)
      -- CONSTRAINT valid_money CHECK(validate_invoice_money(value))
      CONSTRAINT valid_money CHECK(val ~ '^[0-9]+\.[0-9]{2}$' )
);

INSERT INTO invoice (val) VALUES ('0123.98' ); -- success
INSERT INTO invoice (val) VALUES ('a123.98' ); -- failure

SELECT * FROM invoice;

Result: 结果:


CREATE TABLE
INSERT 0 1
ERROR:  new row for relation "invoice" violates check constraint "valid_money"
DETAIL:  Failing row contains (2, a123.98).
 id |   val   
----+---------
  1 | 0123.98
(1 row)

And to answer the question: I do not think you can get additional info on the constraint violation. 并回答这个问题:我认为您无法获得有关约束违规的其他信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM