How do you get id of a record newly inserted into the database with only one query .

In my project I was inserting a project into a database and I need to assign the “id” created when project is inserted into database.

r.db(“DatabaseName”).table(“TableName”).insert(“Values”)

{
“deleted”: 0 ,
“errors”: 0 ,
“generated_keys”: [
“6538d988-f8e8-4ab0-bf71-f363fce91b9f”
] ,
“inserted”: 1 ,
“replaced”: 0 ,
“skipped”: 0 ,
“unchanged”: 0

}



The query to insert will provide a response as shown above and we can make use of the generated_keys to use for our purpose.

The database used is rethinkdb and query is written in reql language.



get in rethinkdb

In rethink db when we want to have a row selected we can make use of this get.

table.get(key) will give us a single row. if there are no documents null is returned.

how to pass a javascript object to be written to a database

I have an Address object from my user interface like below

Address:{address1:”Franklin”,address2:”Warrensburg”,adress3:”Missouri}

so as i am sending an object i should be able to capture all the fields.

so i define a class as

public class Address
{
public string Address1 { get; set; }
public string Address2 { get; set; }
public string Address3 { get; set; }
}

and now when we get an object ,we get values as follows

[“Address”] = new JObject
{
[“Address1”] = _customer.Address.Address1, ===>Franklin
[“Address2”]= _customer.Address.Address2, ===>Warrensburg
[“Address3”] = _customer.Address.Address3 ===> Missouri
}

converting json structure to circular error

I faced this error while having a react js web application requesteing data from backend services which talk to our rethinkdb database.

The root cause was there were no tables created in the database and backend services are requiringn these tables to process the request.

So after creating the data tables and inserting data into the tables I was able to solve the error.