Adding related table to ArcGIS Online feature layer

62352
73
Jump to solution
01-08-2019 10:26 AM
EricGardecki1
New Contributor III

I'm trying to figure out how to add a relationship class (related table one-to-many) to a feature layer within AGOL. I do not have ArcGIS Desktop or Server (do have QGIS but I don't think that will help here).

The only info I was able to find led me to believe that it may be possible by editing the service definition JSON but I can't find any reference as to how to edit it to make it work.

What are my options here?

Also, is viewing/editing/adding to related tables supported in AGOL web apps yet (not just Collector)?

Thanks!!

73 Replies
EricGardecki1
New Contributor III

That seems to have worked.  I was able to add to the feature service and then add the relationship.

I'm having some issues with working with it though.  Adding a new related record (inspection) is very clunky and the related records do not show up at all afterwards, even though they are actually there in the data table.  I'm not sure how to symbolize and use the data yet either but I will keep working on it.  If you have any pointers I'd appreciate it!


Thanks

BrandonGuo
Occasional Contributor

I am new to the REST api, can you explain more about how to use it?

I am trying to use FME httpCaller to create a new table within the existing feature service, but keep getting an error code: The requested layer (layerId: addToDefinition) was not found.

 

The URL am using: **/arcgis/rest/services/Testing_time_layer_line/FeatureServer/addToDefinition

HTTP method is: POST

Upload the json through content type: URL Encoded (application/x-www-form-urlencoded)

body content:


{

"layers" : [

{

"name" : "newTable",

"type" : "Table", // you can also make a feature layer by using Feature "Feature Layer" instead of "Table

"fields" : [

{

name" : "myFieldName",

// copy the rest from the original layer

} ,

]

}

]

}

 

 

0 Kudos
KhaledHassen
Esri Contributor

One place to look is the foreign key value in related table. It should match a primary key in the source or origin layer. You need to be sure also to create a unique index in the origin layer. To create a unique index, you need to use the layer addToDefinition admin API.

Origin layer:

https://url/ArcGIS/rest/admin/services/<service2>/FeatureServer/<layerId>/addToDefinition

Pass the following json:

{

   "indexes" : [

   {

       "name" : "myindex",  // some unique name

       "fields" : "<primary field name in the origin",

       "isUnique" : true

  }

]

}

You might need to create the index and repeat the workflow again if you do not have a unique index already.

By the way, you can create the unique index from the UX as well.

So I would recommend:

1. Create the unique index on the origin layer primary key that you will be using for the relationship.

2. Add the relationship again. You might need to start from scratch (sorry).

Hope that helps.

Khaled

0 Kudos
EricGardecki1
New Contributor III

I am currently using the FID as the PK, not a custom field. Is that already indexed?

The FK is a custom integer field. It is getting filled appropriately with the FID of the origin feature

0 Kudos
KhaledHassen
Esri Contributor

Yes there is a unique index on the objectid.  We normally there are issues with ObjectId based relationships and we always recommend GlobalId based relationship.

Once thing you can try is export this service to a filegdb and then add it as another service with a new name. The objectId based relationship will be converted to one based on GlobalId and it would preserve the original ObjectId.

When you add a related record you will be using the GlobalId instead of the globalId.

Khaled

0 Kudos
EricGardecki1
New Contributor III

Every time I try to export to FGDB it seems to create one that is corrupt.  I can't use it in AGOL and once downloaded, the zip can't be opened.  I have tried with multiple feature services.  Can you verify that there is no bug?  Suggestions?

0 Kudos
KhaledHassen
Esri Contributor

We do not have any currently known bug in the export to filegdb. Do you have a link to the service to look at?

0 Kudos
EricGardecki1
New Contributor III

https://karta.maps.arcgis.com/home/item.html?id=df19163199f84d7b89a9f4069ac1e288#overview   This is just fake data I'm testing with.

I did some further testing and it appears that the export only doesn't work on some of the layers.  Perhaps I'll just start over with some fresh data.

0 Kudos
EricGardecki1
New Contributor III

Khaled,

I started out with a new feature layer created through AGOL.  Added the 2nd layer and relationship as below.  When I attempt to add a record to the related table, it seems to go through (no errors) but it is not shown anywhere.  The table remains empty.


Also, when I tried to export this feature layer as a FGDB, it also seems to fail in some way.  The export screen stays up as if it is frozen.  A file is created but it seems corrupt as I can't open it.


I'm guessing I must be doing something wrong with the JSON.  Suggestions?

For adding layer:

{
"layers" : [
{
"name" : "Inspections",
"type" : "Table",
"fields" : [
{
"name" : "reference", 
"type" : "esriFieldTypeGUID", 
"alias" : "reference", 
"sqlType" : "sqlTypeOther", 
"nullable" : false, 
"editable" : false,
"length" : 38, 
"visible" : true, 
"domain" : null, 
"defaultValue" : null
}, 
{
"name" : "Insp_date", 
"type" : "esriFieldTypeDate", 
"actualType" : "datetime2", 
"alias" : "Insp_date", 
"sqlType" : "sqlTypeTimestamp2", 
"length" : 8, 
"nullable" : true, 
"editable" : true, 
"visible" : true, 
"domain" : null, 
"defaultValue" : null
}, 
{
"name" : "notes", 
"type" : "esriFieldTypeString", 
"actualType" : "nvarchar", 
"alias" : "notes", 
"sqlType" : "sqlTypeNVarchar", 
"length" : 256, 
"nullable" : true, 
"editable" : true, 
"visible" : true, 
"domain" : null, 
"defaultValue" : null
}, 
{
"name" : "ObjectId", 
"type" : "esriFieldTypeOID", 
"actualType" : "int", 
"alias" : "ObjectId", 
"sqlType" : "sqlTypeInteger", 
"nullable" : false, 
"editable" : false, 
"visible" : true, 
"domain" : null, 
"defaultValue" : null
}
]
}
]
}

 for adding relationship:

{
"layers" : [
{
"id" : 0,
"relationships" : [
{
"id" : 1,
"name" : "Inspections",
"relatedTableId" : 1,
"cardinality" : "esriRelCardinalityOneToMany",
"role" : "esriRelRoleOrigin",
"keyField" : "GlobalID",
"composite" : false
}
]
},
{
"id" : 1,
"relationships" : [
{
"id" : 1,
"name" : "Inspections",
"relatedTableId" : 0,
"cardinality" : "esriRelCardinalityOneToMany",
"role" : "esriRelRoleDestination",
"keyField" : "reference",
"composite" : false
}
]
}
]
}

 

Suggestions?

0 Kudos
KhaledHassen
Esri Contributor

Can you pl. provide me with accessible link to the feature service. Will be good to have it as public if it is ok with you. The last link you sent me was inaccessible to me. Will take a look.

0 Kudos