3

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 : "test@test.com" , phone:"123456" });
$$) AS (result agtype)
Omar Saad
  • 349
  • 3
  • 8

17 Answers17

2

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.

Matheus Farias
  • 716
  • 1
  • 10
2

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.

Peter
  • 43
  • 4
1

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.

1

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;
Huzaifa
  • 484
  • 4
  • 8
1

As someone has already mentioned that auto_increment is not available and it will be implemented in the next releases. What we can do right now is to define a custom logic by which we can keep the value of the latest order_id in a node and we will keep updating that whenever we are entering a new order data. In this way our every order data has an order id which is unique.

You can also write custom functions in apache age for this:

You can get notes on user-defined functions from this documentation:

User Defined Functions

You can also raise that as an issue on GitHub so that work can be started in it asap.

Apache Age Issue

1

Apache AGE in its current form does not provide built-in mechanism for auto-incremented or serial attributes like in traditional relational databases.

However, you can auto-increment by implementing a custom solution and writing a function of your own. Hope that helps!

1

I think it is not supported but what you can try doing is to create a separate database table to create auto incremented IDs, you will need to implement a hybrid query that whenever you create a new node, you create a new row in the IDs table with a column that makes it easy for you to refer back to the node (which is not the best option) OR what could be a better option is to create a row in the table then get the ID value and store it in the new node, that's you assure every time you create a new node you have a unique ID.

I hope this helps you get a sense of how it could be implemented.

ahmed_131313
  • 142
  • 6
1

Although AGE does not support auto incremented attribute, you can create the first vertex with the order_id property set to 1, and then for the remaining vertices execute the following cypher query:

SELECT * FROM cypher('graph', $$
MATCH (u: User) WITH u.order_id AS n ORDER BY u.order_id DESC LIMIT 1 CREATE 
(new: User {name: 'user1', email: 'test@test.com', phone : '123', order_id: 
n+1}) RETURN new                    
$$) AS (result agtype);

This query MATCHes all the vertices with User label and picks the first row from vertices sorted in descending order on the order_id property. This way we get the last used order_id and for the new vertex, simply increment it by 1.

Zainab Saad
  • 728
  • 1
  • 2
  • 8
1

Unlike traditional relational databases, there is no built-in support for auto-increment of attributes in apache AGE.

But you can implement this functionality manually. You can built a function that will generate unique value for your attributes just like order_id. That function will add the incremented unique value/id to every attribute that is added

adil shahid
  • 125
  • 4
0

For achieving this you will have to define some custom logic, as currently the auto incremental method is not available.

Prachi
  • 39
  • 3
0

Currently there is no support for auto incrementation of properties in Apache AGE, but you could implement a user defined function to achieve that.

0

Currently, Apache Age does not contain a way to automatically make increments or generate some serial attributes. This feature is usually found in relational databases. But there is one way in which you can achieve this functionality, and that is by writing a custom function. Other than that, currently it is not possible to achieve this functionality.

0

As you are using Apache AGE's cypher extension, it does not currently support auto increment. You will need some manual logic to achieve that as suggested by others. You can try the suggestions above by defining a function to keep a check for uniqueness of the order_id.

0

For now, Apache AGE does not support this feature/property.

You need to define the intended code to handle this.

0

As AGE doesn't support this attribute yet, you have to write your code to tackle this problem.

OR

Create a new issue on this github for feature suggestion.

  • if anyone creates an issue ticket, please comment here with a link to it, or suggest an edit to this post to add a link to the post. – starball Aug 07 '23 at 22:26
  • How's this answer unclear? I had mentioned that this feature is not supported by AGE yet. Suggest this feature on their Github portal to develop it in AGE. – Vinay-Agens Aug 15 '23 at 09:43
0

As of the current date, Apache AGE doesn't natively support auto-incremented attributes like in traditional databases. However, you can manually implement this functionality.

One approach is to create a custom function that generates unique values for attributes and increments them. You could also use a hybrid query to get the last used order_id, increment it, and then insert a new node with the incremented value.

Another suggestion is to create a separate table to manage auto-incremented IDs and use triggers to update it. In the absence of built-in support, you can achieve this functionality through custom logic or functions.

Hassoo
  • 85
  • 11
0

Currently Apacge AGE does not support auto-increment feature like SQL. However you can achieve the same effect by using the node_id property of nodes.

The node_id property is automatically generated by Apache AGE and is unique for each node.

CREATE TABLE orders 
(
    order_ig node_id PRIMARY KEY,
    order_date timestamp,
    order_total float
)

The order_id column is the primary key for the table, which means that each value in the column must be unique. The node_id property will automatically generate unique values for this column

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459