Expresiones de restricción con otros campos con valores predeterminados

I have a field called uuid_transect, with an expression that calculates a default value. One of the conditions sets that value to NULL.

I have a second field, uuid_censo, with a restriction condition. If the uuid_transect field is set to null, the uuid_censo field can be null. If the uuid_transect field is NOT NULL, the uuid_censo field CANNOT be null.

If I manually set the NULL value in the uuid_transect field (using the button to clear the field content), it works correctly. But when the NULL value is set by the default value expression, the restriction does not recognize that change.

My constraint expression is this:

CASE

WHEN “uuid_cens” IS NOT NULL THEN TRUE

WHEN “uuid_transect” IS NULL THEN TRUE

ELSE FALSE

END

Can anyone help me?

Thank you.

have you tried with a nullif conditional?

you can look for it with the search bar in the middle of the Field Calculator window. The description reads:

Returns a NULL value if value1 equals value2; otherwise it returns value1. This can be used to conditionally substitute values with NULL.

It sounds like it might be useful for your use case.

I think your constraint expression is wrong.
Also, you seem to have made a typo in uuid_censo, is it with or without the o?

besides the possible typo,
The first WHEN states that when uuid_censo is not null (so it has a value) the constraint has been met: green checkmark, it is not a ‘failing constraint’
If so, it will not check the other WHEN statements, CASE now has an answer. So it doesn’t look at uuid_transect

Also the second WHEN is not checking your main condition: if uuid_transect is not null, uuid_censo must have a value, it doens’t look at uuid_censo

I think you need an expression like:
CASE
WHEN “uuid_transect” IS NOT NULL AND “uuid_censo” IS NULL THEN FALSE
ELSE TRUE
END

You write that uuid_censo CAN be null (but does not have to be null) when uuid_transect is null. That means you don’t have to check for this situation.
If uuid_censo MUST be null when uuid_transect is null then you need a different expression:

CASE
WHEN “uuid_transect” IS NOT NULL AND “uuid_censo” IS NULL THEN FALSE
WHEN “uuid_transect” IS NULL AND “uuid_censo” IS NOT NULL THEN FALSE
ELSE TRUE
END