Обсуждение: Request for Implementation of Custom Error Messages for CHECK Constraints

Поиск
Список
Период
Сортировка

Request for Implementation of Custom Error Messages for CHECK Constraints

От
"Miguel Ferreira"
Дата:

PostgreSQL Project Leaders,

 

Currently, when a CHECK constraint is violated, PostgreSQL returns a generic error message that includes the constraint name. While informative for developers familiar with the database schema, this message can be less clear for other team members, end-users, or in application logs. This lack of specificity hinders the quick identification of the exact business rule that has been violated and can lead to more time-consuming debugging and less user-friendly error messages in applications.

 

Proposal:

 

I propose extending the syntax of the ALTER TABLE ADD CONSTRAINT statement (and potentially CREATE TABLE) to allow for the specification of a custom error message for each CHECK constraint. A possible syntax could be as follows:

 

ALTER TABLE table_name

ADD CONSTRAINT constraint_name

CHECK (condition)

MESSAGE 'Custom error message when the condition is not met.';

 

Benefits of Implementation:

 

    Improved User Experience: Applications could capture and display more contextual and helpful error messages to end-users, improving usability and reducing confusion.

    Enhanced Debugging: Developers could immediately identify the specific business rule that has been violated, speeding up the debugging and resolution of data integrity issues.

    Implicit Documentation: The custom message would serve as a way to document the intent of the constraint directly within the database schema, facilitating understanding and maintenance of the data model.

    Consistency: It would allow for a more consistent approach to providing informative feedback on business rule violations, complementing the existing capability in triggers.

 

 

 

Best regards,

 

 

"Miguel Ferreira" <miguelmbferreira@gmail.com> writes:
> I propose extending the syntax of the ALTER TABLE ADD CONSTRAINT
> statement (and potentially CREATE TABLE) to allow for the
> specification of a custom error message for each CHECK constraint.

Why don't you just choose better names for your constraints?

I'd argue that the proposed change might actually be a net loss
for usability, if it entirely obscures the fact that what happened
was a check-constraint violation.

It's also not very clear why we'd stop with check constraints,
if the desire is to get rid of database-produced error messages
in favor of something that somebody likes better.

            regards, tom lane



Re: Request for Implementation of Custom Error Messages for CHECK Constraints

От
"David G. Johnston"
Дата:
On Saturday, May 10, 2025, Miguel Ferreira <miguelmbferreira@gmail.com> wrote:

ALTER TABLE table_name

ADD CONSTRAINT constraint_name

CHECK (condition)

MESSAGE 'Custom error message when the condition is not met.';


I’m seeing some value here but, odds are, there is not enough obvious benefit or design to convince someone else to try and envision specifically how the behavior should work and effort to push it through.

 

    Improved User Experience: Applications could capture and display more contextual and helpful error messages to end-users, improving usability and reducing confusion.


Arguably a layering violation.  To make this point more clearly, do you need to account for i18n?

    Enhanced Debugging: Developers could immediately identify the specific business rule that has been violated, speeding up the debugging and resolution of data integrity issues.

Is there really a meaningful gap here?

    Implicit Documentation: The custom message would serve as a way to document the intent of the constraint directly within the database schema, facilitating understanding and maintenance of the data model.


Constraints can be targeted by “comment on”.
 

    Consistency: It would allow for a more consistent approach to providing informative feedback on business rule violations, complementing the existing capability in triggers.


Two different tools that can do the same job.  One with structure and one customizable.  Because triggers exist the proposed feature is less useful.

David J.

RE: Request for Implementation of Custom Error Messages for CHECK Constraints

От
"Miguel Ferreira"
Дата:
Tom Lane <tgl@sss.pgh.pa.us> writes:
> Why don't you just choose better names for your constraints?

A word versus a sentence. It's a big difference that greatly improves the
user experience.

> I'd argue that the proposed change might actually be a net loss for
usability, if it entirely obscures the fact that what happened was a
check-constraint violation.

I understand, I'm looking at it from the point of view of the end user who
is using an application. This application will not need to handle the
message if the database generates a more 'user-friendly' message.

> It's also not very clear why we'd stop with check constraints, if the
desire is to get rid of database-produced error messages in favor of
something that somebody likes better.

The idea is just to be able to define a different message. if I have to use
a trigger to set a different message, then I have to write the same rule
twice, in CHECK and TRIGGER, which is redundant.

Best regards,
Miguel Ferreira




RE: Request for Implementation of Custom Error Messages for CHECK Constraints

От
"Miguel Ferreira"
Дата:

De: David G. Johnston <david.g.johnston@gmail.com>
Enviada: 11 de maio de 2025 01:58

ALTER TABLE table_name

ADD CONSTRAINT constraint_name

CHECK (condition)

MESSAGE 'Custom error message when the condition is not met.';

 

>> I’m seeing some value here but, odds are, there is not enough obvious benefit or design to convince someone else to try and envision specifically how the behavior     should work and effort to push it through.

 

if I have to use a trigger to set a different message, then I have to write the same rule twice, in CHECK and TRIGGER, which is redundant.

--------------------------------------------------------------------------------------------------------------------------

 

    Improved User Experience: Applications could capture and display more contextual and helpful error messages to end-users, improving usability and reducing confusion.

 

>> Arguably a layering violation.  To make this point more clearly, do you need to account for i18n?

No

--------------------------------------------------------------------------------------------------------------------------

 

    Enhanced Debugging: Developers could immediately identify the specific business rule that has been violated, speeding up the debugging and resolution of data integrity issues.

>> Is there really a meaningful gap here?

 

I think there is a significant gap here, especially in complex constraints. The constraint name does not always reveal which specific part of the business rule was violated, which can delay debugging. Personalized messages could provide this information directly

--------------------------------------------------------------------------------------------------------------------------

    Implicit Documentation: The custom message would serve as a way to document the intent of the constraint directly within the database schema, facilitating understanding and maintenance of the data model.

>> Constraints can be targeted by “comment on”.

 

 I agree, but the proposal aims at specific error messages when the constraint is violated, for better feedback in development and in applications.

--------------------------------------------------------------------------------------------------------------------------

    Consistency: It would allow for a more consistent approach to providing informative feedback on business rule violations, complementing the existing capability in triggers.

Two different tools that can do the same job.  One with structure and one customizable.  Because triggers exist the proposed feature is less useful.

 

 

Best regards,

Miguel Ferreira

 

Re: Request for Implementation of Custom Error Messages for CHECK Constraints

От
Marcos Pegoraro
Дата:
Em sáb., 10 de mai. de 2025 às 21:57, David G. Johnston <david.g.johnston@gmail.com> escreveu:
Constraints can be targeted by “comment on”.

So, why not using that COMMENT ON message to raise when that constraint gets violated ?
A GUC like Constraint_Exception_Returns_MessageOn = {Never | Always | If_Exists}
with its default value set to Never, so it runs like today, but if changed to If_Exists it will try to 
get that message or always, to show that COMMENT ON, even empty.

alter table b add constraint fk_b_a foreign key(ID) references a(ID);
comment on constraint fk_b_a on b is 'There is a problem on Foreign Key on Table B related to Table A';

regards
Marcos
 
On Sat, May 10, 2025 at 07:58:47PM -0400, Tom Lane wrote:
> "Miguel Ferreira" <miguelmbferreira@gmail.com> writes:
> > I propose extending the syntax of the ALTER TABLE ADD CONSTRAINT
> > statement (and potentially CREATE TABLE) to allow for the
> > specification of a custom error message for each CHECK constraint.
> 
> Why don't you just choose better names for your constraints?
> 
> I'd argue that the proposed change might actually be a net loss
> for usability, if it entirely obscures the fact that what happened
> was a check-constraint violation.
> 
> It's also not very clear why we'd stop with check constraints,
> if the desire is to get rid of database-produced error messages
> in favor of something that somebody likes better.

Yeah, you could name the constraint
"Custom_error_message_when_the_condition_is_not_met." and then just
convert underscore to spaces and display that to the user.

-- 
  Bruce Momjian  <bruce@momjian.us>        https://momjian.us
  EDB                                      https://enterprisedb.com

  Do not let urgent matters crowd out time for investment in the future.



Re: Request for Implementation of Custom Error Messages for CHECK Constraints

От
"David G. Johnston"
Дата:
On Monday, May 12, 2025, Bruce Momjian <bruce@momjian.us> wrote:

Yeah, you could name the constraint
"Custom_error_message_when_the_condition_is_not_met." and then just
convert underscore to spaces and display that to the user.


 The 63 byte limit seems much more likely to be a factor if the name has to serve the duty of a human-friendly error message.

David J.

On Mon, May 12, 2025 at 11:22:21AM -0700, David G. Johnston wrote:
> On Monday, May 12, 2025, Bruce Momjian <bruce@momjian.us> wrote:
> 
>     Yeah, you could name the constraint
>     "Custom_error_message_when_the_condition_is_not_met." and then just
>     convert underscore to spaces and display that to the user.
> 
>  The 63 byte limit seems much more likely to be a factor if the name has to
> serve the duty of a human-friendly error message.

Yes, that would be a limiting factor.

-- 
  Bruce Momjian  <bruce@momjian.us>        https://momjian.us
  EDB                                      https://enterprisedb.com

  Do not let urgent matters crowd out time for investment in the future.