skip to Main Content

How to create an Auto Incremented (Serial) attribute in Apache AGE ?

For example in the following query I want to add a new attribute order_id that is unique and auto incremented every time I add a new order.

SELECT *
FROM cypher('online_orders', $$
    CREATE(:User{name:"user1" ,email : "[email protected]" , phone:"123456" });
$$) AS (result agtype)

3

Answers


  1. Since the attributes of nodes and edges are stored as a JSON object in the properties column, I believe this cannot be achieved only with the openCypher syntax. Also, AGE stores the ID of every node and edge, so creating an order_id property might be redundant. But you could do something like: create the user and set the order_id property to be the same as the node id.

    Login or Signup to reply.
  2. Apache AGE does not provide built-in support for auto-incremented or serial properties. we can define properties for nodes and edges as needed, but for auto-increment there may be a custom logic, one can define.

    Login or Signup to reply.
  3. There is no support for AUTOINCREMENT in AGE. What you can do is define a custom function for this.

    You can get the largest order_id before inserting the new record and then increment it with 1 and then insert the record.

    Something like this might help(not tested):

    CREATE FUNCTION next_order_id() RETURNS INTEGER AS $$
      DECLARE
        max_id INTEGER;
        next_id INTEGER;
      BEGIN
        -- Get the maximum order_id from the existing records
        SELECT max(order_id) INTO max_id FROM online_orders;
    
        -- If there are no existing records, start with an initial value of 1
        IF max_id IS NULL THEN
          next_id := 1;
        ELSE
          next_id := max_id + 1;
        END IF;
    
        RETURN next_id;
      END;
    $$ LANGUAGE plpgsql;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search