As of now there is no way to achieve that with some constraints or function… one way to achieve that is by creating a trigger that queries for the current highest order_id and increments by 1
Create your function for the trigger
CREATE OR REPLACE FUNCTION increment_order_id()
RETURNS TRIGGER
LANGUAGE PLPGSQL
AS
$$
DECLARE max_order_id INTEGER;
DECLARE current_order_id INTEGER;
BEGIN
SELECT max(order_id) INTO max_order_id FROM online_orders;
current_order_id := max_order_id + 1;
UPDATE online_orders
SET order_id = max_order_id
WHERE order_id = 0;
RETURN NEW;
END;
$$
create the trigger
CREATE TRIGGER autoincrement_order_id
AFTER INSERT ON online_orders
FOR EACH ROW
EXECUTE FUNCTION increment_order_id();
Create an order
SELECT *
FROM cypher('online_orders', $$
CREATE(:User {name:"user1" ,email : "test@test.com" , phone:"123456", order_id: 0 });
$$) AS (result agtype)
everytime you create an order make sure you add order_id
to be 0.