# Creating records using related data and obtaining their primary keys?

**URL:** <https://community.dreamfactory.com/t/creating-records-using-related-data-and-obtaining-their-primary-keys/1999>\
**Category:** REST API\
**Created:** [January 7, 2016, 9:41pm UTC](https://community.dreamfactory.com/t/creating-records-using-related-data-and-obtaining-their-primary-keys/1999 "2016-01-07T21:41:40Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Martin\_Jansen](https://avatars.discourse-cdn.com/v4/letter/m/f05b48/32.png) [@Martin\_Jansen](https://community.dreamfactory.com/u/Martin_Jansen)\
**Post date:** [January 7, 2016, 9:41pm UTC](https://community.dreamfactory.com/t/creating-records-using-related-data-and-obtaining-their-primary-keys/1999/1 "2016-01-07T21:41:40Z")

</div>

I’ve got a Db structure where I have tblStock and tblStockLines related by fk\_stockID in the lines table.

When adding a new line to an existing stock or a whole new set (stock and stock line) - how can I get the primary key of the newly inserted records? Specifically the child object’s primary key.

Almost like using the fields command when using POST or PUT that results in a lookup of the inserted data.

---

<div class="post-metadata">

**Author:** ![Martin\_Jansen](https://avatars.discourse-cdn.com/v4/letter/m/f05b48/32.png) [@Martin\_Jansen](https://community.dreamfactory.com/u/Martin_Jansen)\
**Post date:** [January 8, 2016, 6:11am UTC](https://community.dreamfactory.com/t/creating-records-using-related-data-and-obtaining-their-primary-keys/1999/2 "2016-01-08T06:11:33Z")

</div>

//Planned? Perhaps something here to get the related table id’s back  
var stockUpdateParams = {  
resource: stock,  
ids: ‘id’,  
}

//Currently just sending the object to be updated like this (stock = object)  
SharedFactoryStock.StockUpdate().update(stock, StockUpdatedSuccess, StockUpdatedFailure);

---

<div class="post-metadata">

**Author:** ![Martin\_Jansen](https://avatars.discourse-cdn.com/v4/letter/m/f05b48/32.png) [@Martin\_Jansen](https://community.dreamfactory.com/u/Martin_Jansen)\
**Post date:** [January 15, 2016, 3:04pm UTC](https://community.dreamfactory.com/t/creating-records-using-related-data-and-obtaining-their-primary-keys/1999/3 "2016-01-15T15:04:13Z")

</div>

Hi @formerstaff - have you guys got a function where the newly created related table’s new primary key’s can be returned in the calling function - currently I have to POST to the DB using an alternate tempGUID and then search for the record by that tempGUID in order to obtain its ID in case I PUT the record again (if the ID isn’t in the in-memory object then DF creates yet another related/child record).

---

<div class="post-metadata">

**Author:** ![formerstaff](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.dreamfactory.com/formerstaff/32/1133_2.png) [@formerstaff](https://community.dreamfactory.com/u/formerstaff)\
**Post date:** [January 15, 2016, 7:57pm UTC](https://community.dreamfactory.com/t/creating-records-using-related-data-and-obtaining-their-primary-keys/1999/4 "2016-01-15T19:57:18Z")

</div>

Hey Martin, if you include the ‘related’ url parameter you will get back the new related data as well.  
Example, I have a table called movie, and a table called movie\_revenue where I’m inputting Movie box office data.  
Movie\_revenue is related to Movie using a foreign key field, movie\_revenue\_id.

My POST url: `/api/v2/localdata/_table/movie?related=movie_revenue_by_movie_revenue_id`

And my payload:  
{  
“resource”: [{  
“movie\_title”: “Avatar”,  
“movie\_year”: 2009,  
“movie\_revenue\_by\_movie\_revenue\_id”: [{  
“revenue\_in\_millions”: 2788  
}]  
}]  
}

And the response back from the server:  
{  
“resource”: [  
{  
“movie\_Id”: 3,  
“movie\_revenu\_by\_movie\_revenue\_id”: [  
{  
“revenue\_id”: 2,  
“revenue\_in\_millions”: 2788,  
“movie\_revenue\_id”: 3  
}  
]  
}  
]  
}

**edit** : sorry for the bad formatting. can’t seem to get the markdown to do what i want. paste it into [jsonlint.com](http://jsonlint.com) for pretty formatting. 😒

---

<div class="post-metadata">

**Author:** ![Martin\_Jansen](https://avatars.discourse-cdn.com/v4/letter/m/f05b48/32.png) [@Martin\_Jansen](https://community.dreamfactory.com/u/Martin_Jansen)\
**Post date:** [January 18, 2016, 10:47am UTC](https://community.dreamfactory.com/t/creating-records-using-related-data-and-obtaining-their-primary-keys/1999/5 "2016-01-18T10:47:57Z")

</div>

Hi @formerstaff thanks for the info. I’m trying this, but I’m not getting the child record. Have converted it to put the object in as a parameter object.

var stockNewParams = {

```
                resource: [],

                related: 'tblstocklinein_by_fk_stockID',

                fields: '*'
            };

            stockNewParams.resource.push(n);

            console.log(stockNewParams);

            SharedFactoryStock.StockNew().create(stockNewParams, stockNewSuccess, stockNewFailure);

```

And When I get a response back from the server it’s just got the parent record ID and not the related tables ID.  
I’ve also tried fields: as blank and fields as \* and as id.

---

<div class="post-metadata">

**Author:** ![Martin\_Jansen](https://avatars.discourse-cdn.com/v4/letter/m/f05b48/32.png) [@Martin\_Jansen](https://community.dreamfactory.com/u/Martin_Jansen)\
**Post date:** [January 18, 2016, 10:50am UTC](https://community.dreamfactory.com/t/creating-records-using-related-data-and-obtaining-their-primary-keys/1999/6 "2016-01-18T10:50:58Z")

</div>

this is the object I’m sending?

fields: "id"  
related: "tblstocklinein\_by\_fk\_stockID"  
resource: Array[1]  
0: Object  
$$hashKey: “uiGrid-017A”  
\_rowStatus: "NEW"  
agr: 1.97  
avgLenCurrent: 91.31  
avgLenIn: 91.31  
avgWtCurrent: 145  
avgWtIn: 145  
commentIn: ""  
dateIn: "2016-01-18"  
daysInStock: NaN  
fgr: 1.45  
fk\_companyID: "{COMPANY\_ID}"  
fk\_openedByTaskID: 181  
fk\_qualityIndicatorID: null  
fk\_siteDepartmentID: 28  
fk\_siteDepartmentPositionID: 11764  
fk\_siteID: "2"  
fk\_stockBatchID: "139"  
fk\_stockGroupID: "1257"  
id: null  
isActiveStock: false  
isIntermediaryStock: false  
mgr: undefined  
numCurrent: 113.79  
numIn: 113.79  
rgrIn: 0.93  
sku: "67"  
tblstocklinein\_by\_fk\_stockID: Array[1]  
tempGUID: "52a8ceec-f6e5-4421-b0dd-9cf52b8229d1"  
wtCurrent: 16500  
wtIn: 16500

---

<div class="post-metadata">

**Author:** ![Martin\_Jansen](https://avatars.discourse-cdn.com/v4/letter/m/f05b48/32.png) [@Martin\_Jansen](https://community.dreamfactory.com/u/Martin_Jansen)\
**Post date:** [January 18, 2016, 11:19am UTC](https://community.dreamfactory.com/t/creating-records-using-related-data-and-obtaining-their-primary-keys/1999/7 "2016-01-18T11:19:33Z")

</div>

Should I not be using something like: [https://github.com/dreamfactorysoftware/dsp-core/wiki/SQL-Related-Data#getting-the-related-data](https://github.com/dreamfactorysoftware/dsp-core/wiki/SQL-Related-Data#getting-the-related-data)

Where we have to related - Comma-delimited list of relations to return for each record in response, or “_" to retrieve all related records. By default, all fields of the related record(s) are returned. Optional fields, limit, and order can be sent for each relation. The following separate URL parameters apply to each relationship given…  
\<relation\_name\>.fields - A comma-delimited list of fields to return in the response for the related record(s). If this parameter is set to "_” or missing, all fields will be returned. Setting it to empty ("") will result in just the primary key field(s) and value(s) being returned.  
\<relation\_name\>.limit - An integer count value limiting the number of related records of the given relationship to return in the response. Default is unlimited until max response size met.  
\<relation\_name\>.order - Declares the ‘order by’ field and direction for sorting the related results per record per relationship.

---

<div class="post-metadata">

**Author:** ![Martin\_Jansen](https://avatars.discourse-cdn.com/v4/letter/m/f05b48/32.png) [@Martin\_Jansen](https://community.dreamfactory.com/u/Martin_Jansen)\
**Post date:** [January 18, 2016, 12:01pm UTC](https://community.dreamfactory.com/t/creating-records-using-related-data-and-obtaining-their-primary-keys/1999/8 "2016-01-18T12:01:46Z")

</div>

So now I’m trying send the following object to DreamFactory and still no related table?  
fields: "id,tempGUID"  
related: "tblstocklinein\_by\_fk\_stockID"  
resource: Array[1]  
0: Object  
length: 1  
**proto** : Array[0]  
tblstocklinein\_by\_fk\_stockID.fields: “id,tempGUID”

---

<div class="post-metadata">

**Author:** ![formerstaff](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.dreamfactory.com/formerstaff/32/1133_2.png) [@formerstaff](https://community.dreamfactory.com/u/formerstaff)\
**Post date:** [January 18, 2016, 1:43pm UTC](https://community.dreamfactory.com/t/creating-records-using-related-data-and-obtaining-their-primary-keys/1999/9 "2016-01-18T13:43:07Z")

</div>

martin, is this on version 1.9?

---

<div class="post-metadata">

**Author:** ![Martin\_Jansen](https://avatars.discourse-cdn.com/v4/letter/m/f05b48/32.png) [@Martin\_Jansen](https://community.dreamfactory.com/u/Martin_Jansen)\
**Post date:** [January 19, 2016, 6:18am UTC](https://community.dreamfactory.com/t/creating-records-using-related-data-and-obtaining-their-primary-keys/1999/10 "2016-01-19T06:18:30Z")

</div>

Hi Drew, sorry man, it’s the latest version, my bad, i didn’t have the “related” parameter pulling through to my angular factory, fixed it and the related data came through. Thanks man!

Is there a way to specify the individual fields on the related table?
