Reputation: 301
Apache AGE allows me to store values of different types in vertex properties with the same name. For example:
Creating a vertex with pages = 10:
SELECT * FROM cypher('books', $$
CREATE (v:Book {title: 'A book', pages: 10})
RETURN v $$) as (v agtype);
v
--------------------------------------------------------------------------------------------------
{"id": 844424930131969, "label": "Book", "properties": {"pages": 10, "title": "A book"}}::vertex
(1 row)
Creating a vertex with pages = '10':
SELECT * FROM cypher('books', $$
CREATE (v:Book {title: 'Another book', pages: '10'})
RETURN v $$) as (v agtype);
v
----------------------------------------------------------------------------------------------------------
{"id": 844424930131970, "label": "Book", "properties": {"pages": "10", "title": "Another book"}}::vertex
(1 row)
I understand that all types return as agtype, but could this potentially cause errors in building an application?
Upvotes: 3
Views: 149
Reputation: 42
It depends on how will you use this in your application you can allow page
property to take a string or integer and when you query on page
data cast it to for example to integer using toInteger()
Upvotes: 0
Reputation: 85
The agtype
returned by AGE returned in your code above refers to the AGE custom data type as stated in the official documentation. All different Vertex created by AGE comes with its own unique Graph ID, so there will be no collision.
Upvotes: 0
Reputation: 211
Depending on what you want to return, you are returning a vertex, or one of its properties. For the types, AGE does the heavy lifting for you by asserting the types you have defined using the values you have provided.
The agtype by itself is a custom type based of JSONB. Thus it is type-safe as it is documented in PostgreSQL
As defined in age--x.x.x.sql
:
CREATE TYPE agtype (
INPUT = ag_catalog.agtype_in,
OUTPUT = ag_catalog.agtype_out,
SEND = ag_catalog.agtype_send,
RECEIVE = ag_catalog.agtype_recv,
LIKE = jsonb
);
So when you are using a driver to call the PostgreSQL server running AGE. You'll be getting back JSONB like data. Upon which you can assert your preferred datatype on the values.
As you have stated, it can become an issue due to uneven types. But, the standard type-safe way to handle any data coming from a DB would be to assert your type within your Application code. So upon running the Read operation from the database, you should make sure to set it as int
in the case of your pages
property value.
OR,
You could use functions like toInteger()
& toString()
within the Cypher query to assert your type before getting back the query return value.
Upvotes: 2
Reputation: 29
I think you have misunderstood something. Age returns as agtype but there is underlined type defined, so when a query returns a value of type agtype, AGE internally stores the data along with its data type information so that it can be later retrieved and processed appropriately.
Upvotes: 0
Reputation: 233
I think you have misunderstood something. On your first query you create a vertex with the label Book
and assign it some properties (that is basically a json but that doesn't matter right now).
On your second query you create a different vertex with the same label and different properties, but if you notice these 2 vertices have different ID's, so Age will always now that there are 2 different vertices and there won't be any "collision" of values.
Therefore the fact that apacheAGE always returns agtype doesn't cause any problems.
Upvotes: -1