How to set ID for primary key?


#1

Haii guys…

i try to set ID for primary key but its not solved.

my code :

InsertQuery addFerari = qb.insert(var().id(“id_01”).isa(“car”)
.has(“merk”, “ferrari”));
addFerari.execute();
or

InsertQuery addFerari = qb.insert(var().id(ConceptId.of(“id_001”)).isa(“car”)
.has(“merk”, “ferrari”));
addFerari.execute();

give me the way for set ID for my graph!
thanks before.


#2

Hi @rider_galau,

So Concept Ids are actually immutable and are set by the system. You can only use them for lookups. If you want to set your own id then you do this by defining a key relation.

Doing this involves a few steps:

  1. Creating a Resource Type to represent your key/id and linking it to the EntityType which needs the key. For example:
qb.insert(
    label("my-key").sub("resource").datatype(ResourceType.DataType.STRING),
    label("my-entity-type").sub("entity").key("my-key")
).execute();

2 . Creating the actual key and linking it to an Entity:

qb.insert(
    var().isa("my-entity-type").has("my-key", "key-1")
).execute();

This is how you create a “Primary Key” in the Grakn world. So the key “key-1” can only ever be linked to one Entity


#3

Could I create the code when i create Ontology ?

Thanks @filipe !


#4

Could I create the code when i create Ontology ?

Yes. Defining the keys and how they link to specific Entity Types is part of defining the ontology.


#5

i have one Entity (car).
car entity have some ResourceType : [merk,details].
my question is, For add new ‘key’ like you’re code, what should I do in my code?

i’m sorry if the santences its not good.


#6

Maybe the following is what you looking for. First create the ontology:

qb.insert(
    //Create the resource types
    //Normal Resource Types
    label("merk").sub("resource").datatype(ResourceType.DataType.STRING),
    label("details").sub("resource").datatype(ResourceType.DataType.STRING),
    //The one I want to use as a key
    label("key").sub("resource").datatype(ResourceType.DataType.STRING),
   
    //Create the entity type Car and link it to the resources
    label("car").sub("entity").key("my-key").has("merk").has("details")
).execute();

Now here is how you would load some sample data in:

qb.insert(
    var().isa("my-entity-type").      //Create a car
    has("details", "My awesome car"). //Give it details
    has("merk", "Merk thing").        //Give it a merk
    has("key", "key-1")               //Give it a key - This is the primary key
).execute();

#7

:grin:
Its so great…
entity and resources has been created.

but when I’ll insert the sample data, I got Error.

The type [car] is not allowed to have a resource of type [key]

================================================================
Should I add some code for solution?
thanks…


#8

That means you did not specify in the ontology that your resource type key is allowed to be a key. Try this to fix that:

qb.insert(
    label("car").sub("entity").key("key");
);

#9

Hai… @filipe ,
could you check my code, where the error code…?
i’ll the set key from label 'id’
but the same ID still saved in my grakn.

code create Ontology

	qb.insert(
		    label("merk").sub("resource").datatype(ResourceType.DataType.STRING),
		    label("details").sub("resource").datatype(ResourceType.DataType.STRING),
		    label("id").sub("resource").datatype(ResourceType.DataType.STRING),
		   
		    label("car").sub("entity").key("id").has("merk").has("details")
		).execute();

code insert Sample data

      	qb.insert(
		    var().isa("car").  
		    has("details", "My Test car"). 
		    has("merk", "Merk OKEM").  
		    has("id","id_001")      
		).execute();

thanks


#10

Hi @rider_galau,

Ahhhh I see we may have a misunderstanding. When you say:

but the same ID still saved in my grakn

I take it you mean the concept ID? As I said, that is immutable and internally set by the system. You cannot change that. The procedure we went through above is how you add your own ID or primary key. Not change an existing one. This ID you created should be listed along with all the other resources on the concept.

P.S. your code looks correct as far as creating your own primary key.


#11

I mean, I set my own primary key with the resources name ‘id’. :slight_smile:

like this…

but the resources name ‘id’ can be duplicated.

:slight_smile: :slight_smile:


#12

but the resources name ‘id’ can be duplicated.

Do you mean to say that for a given entity (in this case a car) you can set your own id name more than once?

Can you show me all the resources attached to your car? Or is that already the screenshot above?


#13

no, but i wanna to set the my own primary key with the resources name ‘id’. :slight_smile:

there is has been ready resource name ‘key’ .
but when I try to insert the sample data. I got Error message like

> The type [car] is not allowed to have a resource of type [key]

thanks @filipe


#14

Ahhh it might be because you specifying it as a resource not a key. So instead of doing this:

qb.insert(
		    var().isa("car").  
		    has("details", "My Test car"). 
		    has("merk", "Merk OKEM").  
		    has("id","id_001")      
		).execute();

do this:

qb.insert(
		    var().isa("car").  
		    has("details", "My Test car"). 
		    has("merk", "Merk OKEM").  
		    key("id","id_001")      
		).execute();

My apologies @rider_galau I should have noticed the incorrect usage of has


#15

My apologies too because I couldn’t speak english very well.
I got error again when I follow the code.

:smile:

what should I do ?

thanks


#16

Hmmm it appears that using has() is correct. I think you may have found a bug @rider_galau. Let me see if I can replicate the issue.


#17

I can’t seem to replicate your issue with the latest version. What version are you using @rider_galau ?


#18

I used grakn-dist-0.12.1 .
i’m so confuse for the issue . :sweat:
@filipe can I delete the grakn keyspace ?


#19

You can delete the keyspace by saying:

graph.admin().delete();

#20

ups…, :joy:
only graph.admin()._____
method ‘delete’ its not ready in my code ?