I have the following simple table relations:
CREATE TABLE PRODUCT (
ID BIGSERIAL,
TYPE VARCHAR(24),
TITLE VARCHAR(128),
PRIMARY KEY (ID)
);
CREATE TABLE EVENT (
ID BIGSERIAL,
STATE VARCHAR(64) NOT NULL,
DATETIME TIMESTAMP NOT NULL,
PRODUCT_ID BIGINT,
FOREIGN KEY (PRODUCT_ID) REFERENCES PRODUCT(ID),
PRIMARY KEY (ID)
);
The "STATE" in Event, is converted from a Java Enum, can be: INITIALIZED, AVAILABLE, PROCESSED, etc.
I want to do the following: if the last event entry of the product has STATE 'PROCESSED', then create a new insert in the EVENT table with STATE 'AVAILABLE'. This must be for all products.
As you can see, the event table has a reference to product. I have looked at: Insert, on duplicate update in PostgreSQL?
But could not figure it out.