Home > Mobile >  The value in the generated column does not change
The value in the generated column does not change

Time:08-31

I created the generated column "available_quantity" -

alter table products_regions 
  add column available_quantity int8 
  GENERATED ALWAYS AS (quantity_total - quantity_ordered) STORED;  

When performing some logic, only the "quantity_ordered" column changes.
But I get an

ERROR: the column "available_quantity" can only be assigned the DEFAULT value. The column "available_quantity" is generated.

I don't write anything directly into this field.
If you change the "quantity_ordered" field in the database manually, then "available_quantity" is calculated

How can I solve the problem?

CodePudding user response:

The solution has been found.
If there is a calculated field in the Postgresql database and ORM Hibernate, then at the entity level it is required to specify an
@Column for this field (name = "your name", inserted = false, updated = false).

CodePudding user response:

You cannot modify a generated column. The documentation is pretty clear about that:

A generated column cannot be written to directly. In INSERT or UPDATE commands, a value cannot be specified for a generated column, but the keyword DEFAULT may be specified.

Use a regular column and fill it with a trigger during INSERT.

  • Related