I am attempting to use the Apache-AGE query SELECT create_vlabel('testdb', 'cities');
, but it returns the error message: "Label 'cities' already exists." How can I modify the query to ensure that it only runs if the label does not already exist?

- 351
- 1
- 7
4 Answers
Hi you can achieve that through checking if the label is already existing on ag_label table or not, the following query achieves that:
SELECT create_vlabel('testdb', 'cities')
WHERE NOT EXISTS (
SELECT name FROM ag_label WHERE name = 'cities'
);

- 678
- 5
- 12
To verify the existence of a label, you can use the command _label_id
. If the command returns 0 (zero), it means that the label doesn't exist. Here's an example of how to use the command:
SELECT create_vlabel('graph_name', 'label_name')
WHERE _label_id('graph_name', 'label_name') = 0;
While it is technically possible to check the label's existence directly in the label table, I would not recommend it. This is because the internal storage management of label tables in Apache AGE may undergo changes, and it is more reliable to check the graph to which the label belongs using this function.

- 763
- 1
- 12
To ensure that the query only runs if the label does not already exist, you can use the IF NOT EXISTS
clause. The following query will create the cities label if it does not already exist:
IF NOT EXISTS (
SELECT 1
FROM vlabels
WHERE label = 'cities'
)
BEGIN
CREATE vlabel('cities');
END;
If the cities
label already exists, the query will not execute and no error will be returned.

- 17
- 4
SELECT create_vlabel('testdb', 'cities') IF NOT EXISTS; use this query hope this helps

- 36
- 1