Cannot insert data with key in Grakn - vaticle-typedb

I am ingesting data into Grakn and getting this error
There is more than one thing of type [person] that owns the key [949] of type [person-id].
This is my query:
insert $a isa customer, has customer-id "94929", has person-id "949";
This is my schema:
person sub entity,
key person-id;
customer sub person,
key customer-id;

This indicates that there is an entity of type person, or a subtype of type person, that already has the id "949" in the database. If you run the following query, you should find 1 result.
match $x isa person, has person-id "949"; get;

Related

How to update only the first result of a match query in Typedb?

In the schema I have:
define
description sub attribute, value string;
task sub entity, has description;
Now I write the following two transactions into data:
insert
$a isa task;
$b isa task;
So the result of the query match $x isa task is as expected:
{ $x iid 0x826e80048000000000000000 isa task; }
{ $x iid 0x826e80048000000000000001 isa task; }
answers: 2, total (with concept details) duration: 4 ms
Now I want to insert description to only the first task. So I tried:
match $x isa task; limit 1; insert $x has description "Buy Milk";
But this results in an error:
[THW15] Invalid Thing Write: The thing variable '$x' cannot be inserted as a new instance without providing its type (isa).`
But if I tried:
match $x isa task; insert $x has description "Buy Milk";
then it would update both the tasks.
I can do the following:
match $x iid 0x826e80048000000000000000; insert $x has description "Buy Milk";
and it works!
But I wonder if there is a more elegant way to do this.
So question: How can I update the attribute of only the first result of a match in TypeDB?
I think the fundamental problem you have is that you're inserting tasks without identifying them in any way. TypeDB does give you an iid under the hood, but it would be easier for you to reason about if you gave your own ID to each task at the point at which you create them.
With the above insert query, you get a single task and add a description to it. This could be any task, which makes the query not very useful unless you know every single one of your tasks has no description.
If you wanted to insert a task without adding a description just yet, you could add an ID in advance using the schema:
define
description sub attribute,
value string;
id sub attribute,
value string;
task sub entity,
owns id,
owns description;
and the query:
insert
$a isa task, has id "123456789";
$b isa task, has id "987654321";
These are examples, but you now have a way to identify each task rather than finding a random one and adding a description to it.

Querying DynamoDB with partition key and secondary index in Javascript

Having the following table definition in DynamoDB
pk: userId (string)
sk: carId (string)
index: carModelIndex (string)
If I would like to query all the cars rented by a given user for a given model. How I would go about that?.
Please note I am using the javascript sdk for dynamodb.

EntityFramework Include and Join syntax

I am connecting to a 3rd party SQLite database. Add I am struggling to get the syntax correct for joining a lookup value to a series of nested Includes. Tables are Company, Dept, Employee and EmployeeType. EmployeeType is a list of IDs with associated Name e.g. 1=Casual, 2=Part Time, etc. Employee has an EmpTypeID that should be used to lookup the EmployeeType string. I have the nesting working OK as follows:
var Org= await _db.Company.Include(d => d.Depts).ThenInclude(e => e.Employees).ToListAsync();
What I can't work out is how to do the Join to get the EmployeeType.Name for the Employee in the resultset?

Insert data where Multiple Foreign keys are in SQL server table

I have three Tables:
Teachers
PK: A_pk
Students
PK: B_pk
Post
PK: C_pk
FK: A_pk
FK: B_pk
I have a Website page where users write different posts.
When teachers post in that group, I will insert data into the Post table like this:
A_pk = teacherName
C_pk = Post_text
When students post in that group, I will insert data into the Post table like this:
B_pk = studentName
C_pk = Post_text.
Reason is that I want to keep record which user posted data in my group.
Now the Question is how to insert record in Post table?
You cannot have one field in your table be a foreign key to two different tables. What you need to do is change your data structure to either hold your students and teachers in the same table and use that primary key in your Post table, or to have two columns in your Post table - one for the Teacher primary key and one for the Student primary key and populate the appropriate one based on who made the post.
There is absolutely no need nor reason to have the ID of two different tables within the same field. Doing so would be bad design.
Finally--- I found the Solution.
Actually its very simple... Like you can have nullable FK.so when you insert for student then A_PK can be null and vice versa. –
If teacher posted data in group than
if(A_pk != null) { Insert A_pk and Insert C_pk }
If student posted data in group than
else if(B_pk != null) { Insert B_pk and Insert C_pk }
Thanks alot Sir
KumarHarsh 1 hour ago

Entity Framework: Working with FK's (why do they get hidden?)

First of all, let's define a few tables:
Users table will store information about a user:
Users
- UserID (int, PK)
- ...
UserTasks is a table that stores a task associated with a user:
UserTasks
- userID (int, FK to Users table)
- taskName (varchar)
When I generate the UserTasks table using the ADO Entity Framework, I'll get a class that looks like this:
UserTasks
- taskName (string)
- Users (collection of Users objects)
Note: There's no userID that is generated in the UserTasks table. So let's assume now that I need to insert a new user task... how do I do it? I don't have access to the userID FK field, so my only option is to do a lookup and populate a Users object, then pass that to my task object, like this:
//get the user we are interested in
var user = (from u in context.Users
where u.userID == 2
select u).FirstOrDefault();
//create the new task
UserTasks newTask = new UserTasks();
newTask.taskName = "New Task";
newTask.User = user;
The problem with the above is that I'm doing an extra and needless database call to populate my user object. Is there anyway to somehow create a new mapping to my userID field and still keep the User object?
Thanks
--Ryan
Not now, but maybe in the next version.

Resources