Adding related table to ArcGIS Online feature layer

59387
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

What do I need as a "serviceName" if it is NOT a layer?  A web map name?  I tried that and it says invalid url and I do not know what else it could be.

Thanks

0 Kudos
KhaledHassen
Esri Contributor

Can you send me the url you are using the also the json used.

I will check and let you know.

0 Kudos
EricGardecki1
New Contributor III

I think I have the right URL now.

Am I incorporating your sample JSON into what is already existing or completely deleting what is there and only adding something like your sample?  (presuming since there has to be a difference between addToDefinition and updateDefinition)

Also, how am I defining what the 2nd layer is?  As in, a pointer to the name of the 2nd layer (table) so it knows which table is related?  I do not see any such reference in your sample.

Any luck on any sort of documentation on this?  Seems to be a lot more to it.

Thanks

0 Kudos
EricGardecki1
New Contributor III

I took the "layer" section from the JSON from the feature server for LayerB and added it to the JSON from the feature server for LayerA on the addToDefinition page.  Then incorporated the sample from above into the relationships sections.  Is this correct?

I feel I have the JSON formatting correct but I received an error when trying to apply it.  Invalid definition for System.Collections.Generic.List`1[ESRI.ArcGIS.SDS.Metadata.LayerCoreInfo]
Exception has been thrown by the target of an invocation.

Here is the JSON I added (removed some things that may be private):

{
"adminServiceInfo" : {
"name" : "SampleData_Catch_Basins",
"type" : "FeatureServer",
"cacheMaxAge" : 0,
"status" : "Started",
"database" : {
"datasource" : {
"name" : "db"
}
}
},
"currentVersion" : 10.61,
"serviceItemId" : "...",
"serviceDescription" : "",
"hasVersionedData" : false,
"supportsDisconnectedEditing" : false,
"hasStaticData" : false,
"maxRecordCount" : 2000,
"supportedQueryFormats" : "JSON",
"supportsVCSProjection" : false,
"capabilities" : "Create,Delete,Query,Update,Editing,Sync,ChangeTracking",
"description" : "",
"copyrightText" : "",
"spatialReference" : {
"wkid" : 102100,
"latestWkid" : 3857
},
"initialExtent" : {
"xmin" : -8652196.102416249,
"ymin" : 5104375.070647859,
"xmax" : -8648522.5592200719,
"ymax" : 5105715.3676761677,
"spatialReference" : {
"wkid" : 102100,
"latestWkid" : 3857
}
},
"fullExtent" : {
"xmin" : -8652196.102416249,
"ymin" : 5104375.070647859,
"xmax" : -8648522.5592200719,
"ymax" : 5105715.3676761677,
"spatialReference" : {
"wkid" : 102100,
"latestWkid" : 3857
}
},
"allowGeometryUpdates" : true,
"units" : "esriMeters",
"supportsAppend" : true,
"size" : 16384,
"syncEnabled" : true,
"syncCapabilities" : {
"supportsAsync" : true,
"supportsRegisteringExistingData" : true,
"supportsSyncDirectionControl" : true,
"supportsPerLayerSync" : true,
"supportsPerReplicaSync" : true,
"supportsSyncModelNone" : true,
"supportsRollbackOnFailure" : true,
"supportsAttachmentsSyncDirection" : true
},
"extractChangesCapabilities" : {
"supportsReturnIdsOnly" : false,
"supportsReturnExtentOnly" : true,
"supportsReturnAttachments" : true,
"supportsLayerQueries" : true,
"supportsGeometry" : true,
"supportsFeatureReturn" : true
},
"supportsApplyEditsWithGlobalIds" : true,
"editorTrackingInfo" : {
"enableEditorTracking" : true,
"enableOwnershipAccessControl" : false,
"allowOthersToQuery" : true,
"allowOthersToUpdate" : true,
"allowOthersToDelete" : true,
"allowAnonymousToQuery" : true,
"allowAnonymousToUpdate" : true,
"allowAnonymousToDelete" : true
},
"changeTrackingInfo" :
{
"lastSyncDate" : 1536634415665,
"layerServerGens" : [
{
"id" : 0,
"minServerGen" : 8005,
"serverGen" : 8005
}
]
},
"xssPreventionInfo" : {
"xssPreventionEnabled" : true,
"xssPreventionRule" : "InputOnly",
"xssInputRule" : "rejectInvalid"
},
"layers" : [
{
"adminLayerInfo" : {
"tableName" : "...",
"geometryField" : {"name" : "Shape"},
"xssTrustedFields" : ""
},
"id" : 0,
"name" : "Point layer",
"type" : "Feature Layer",
"displayField" : "",
"description" : "",
"copyrightText" : "",
"defaultVisibility" : true,
"editFieldsInfo" : {
"creationDateField" : "CreationDate",
"creatorField" : "Creator",
"editDateField" : "EditDate",
"editorField" : "Editor"
},
"editingInfo" : {
"lastEditDate" : 1537206552230
},
"relationships" : [
{
"id" : 1,
"name" : "myRel",
"relatedTableId" : 1,
"cardinality" : "esriRelCardinalityOneToMany",
"role" : "esriRelRoleOrigin",
"keyField" : "GlobalID",
"composite" : false
}
],
"isDataVersioned" : false,
"supportsAppend" : true,
"supportsCalculate" : true,
"supportsASyncCalculate" : true,
"supportsTruncate" : false,
"supportsAttachmentsByUploadId" : true,
"supportsAttachmentsResizing" : true,
"supportsRollbackOnFailureParameter" : true,
"supportsStatistics" : true,
"supportsExceedsLimitStatistics" : true,
"supportsAdvancedQueries" : true,
"supportsValidateSql" : true,
"supportsCoordinatesQuantization" : true,
"supportsQuantizationEditMode" : true,
"supportsApplyEditsWithGlobalIds" : true,
"advancedQueryCapabilities" : {
"supportsPagination" : true,
"supportsPaginationOnAggregatedQueries" : true,
"supportsQueryRelatedPagination" : true,
"supportsQueryWithDistance" : true,
"supportsReturningQueryExtent" : true,
"supportsStatistics" : true,
"supportsOrderBy" : true,
"supportsDistinct" : true,
"supportsQueryWithResultType" : true,
"supportsSqlExpression" : true,
"supportsAdvancedQueryRelated" : true,
"supportsCountDistinct" : true,
"supportsQueryAttachments" : true,
"supportsReturningGeometryCentroid" : false,
"supportsQueryWithDatumTransformation" : true,
"supportsHavingClause" : true,
"supportsOutFieldSQLExpression" : true,
"supportsMaxRecordCountFactor" : true,
"supportsTopFeaturesQuery" : true
},
"useStandardizedQueries" : true,
"geometryType" : "esriGeometryPoint",
"minScale" : 0,
"maxScale" : 0,
"extent" : {
"xmin" : -8652196.102416249,
"ymin" : 5104375.070647859,
"xmax" : -8648522.5592200719,
"ymax" : 5105715.3676761677,
"spatialReference" : {
"wkid" : 102100,
"latestWkid" : 3857
}
},
"drawingInfo":{"renderer":{"type":"simple","symbol":{"angle":0,"xoffset":0,"yoffset":0,"type":"esriPMS","url":"287c0bfa-518b-4ace-a8bf-c58ea264fdea","imageData":"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","contentType":"image/png","width":15,"height":15}},"transparency":0},
"allowGeometryUpdates" : true,
"hasAttachments" : true,

"attachmentProperties" : [
{
"name" : "name",
"isEnabled" : true
},
{
"name" : "size",
"isEnabled" : true
},
{
"name" : "contentType",
"isEnabled" : true
},
{
"name" : "keywords",
"isEnabled" : true
}
],
"htmlPopupType" : "esriServerHTMLPopupTypeNone",
"hasM" : false,
"hasZ" : false,
"objectIdField" : "FID",
"globalIdField" : "GlobalID",
"typeIdField" : "",
"fields" : [
{
"name" : "FID",
"type" : "esriFieldTypeOID",
"actualType" : "int",
"alias" : "FID",
"sqlType" : "sqlTypeInteger",
"nullable" : false,
"editable" : false,
"visible" : true,
"domain" : null,
"defaultValue" : null
},
{
"name" : "GlobalID",
"type" : "esriFieldTypeGlobalID",
"alias" : "",
"sqlType" : "sqlTypeOther",
"length" : 38,
"nullable" : false,
"editable" : false,
"visible" : true,
"domain" : null,
"defaultValue" : null
},
{
"name" : "CreationDate",
"type" : "esriFieldTypeDate",
"alias" : "CreationDate",
"sqlType" : "sqlTypeOther",
"length" : 8,
"nullable" : true,
"editable" : false,
"visible" : true,
"domain" : null,
"defaultValue" : null
},
{
"name" : "Creator",
"type" : "esriFieldTypeString",
"alias" : "Creator",
"sqlType" : "sqlTypeOther",
"length" : 128,
"nullable" : true,
"editable" : false,
"visible" : true,
"domain" : null,
"defaultValue" : null
},
{
"name" : "EditDate",
"type" : "esriFieldTypeDate",
"alias" : "EditDate",
"sqlType" : "sqlTypeOther",
"length" : 8,
"nullable" : true,
"editable" : false,
"visible" : true,
"domain" : null,
"defaultValue" : null
},
{
"name" : "Editor",
"type" : "esriFieldTypeString",
"alias" : "Editor",
"sqlType" : "sqlTypeOther",
"length" : 128,
"nullable" : true,
"editable" : false,
"visible" : true,
"domain" : null,
"defaultValue" : null
},
{
"name" : "InspectionDate",
"type" : "esriFieldTypeDate",
"alias" : "Inspection Date",
"sqlType" : "sqlTypeOther",
"length" : 0,
"nullable" : true,
"editable" : true,
"visible" : true,
"domain" : null,
"defaultValue" : null
},
{
"name" : "AmtRemoved",
"type" : "esriFieldTypeDouble",
"alias" : "Pounds of Debris Removed",
"sqlType" : "sqlTypeOther",
"nullable" : true,
"editable" : true,
"visible" : true,
"domain" : null,
"defaultValue" : null
},
{
"name" : "NeedsRepair",
"type" : "esriFieldTypeString",
"alias" : "Needs Repair?",
"sqlType" : "sqlTypeOther",
"length" : 3,
"nullable" : true,
"editable" : true,
"visible" : true,
"domain" : null,
"defaultValue" : "No"
},
{
"name" : "RepairType",
"type" : "esriFieldTypeString",
"alias" : "Type of Repairs",
"sqlType" : "sqlTypeOther",
"length" : 256,
"nullable" : true,
"editable" : true,
"visible" : true,
"domain" : null,
"defaultValue" : null
},
{
"name" : "Notes",
"type" : "esriFieldTypeString",
"alias" : "",
"sqlType" : "sqlTypeOther",
"length" : 256,
"nullable" : true,
"editable" : true,
"visible" : true,
"domain" : null,
"defaultValue" : null
},
{
"name" : "YearInspected",
"type" : "esriFieldTypeInteger",
"alias" : "Year Inspected",
"sqlType" : "sqlTypeOther",
"nullable" : true,
"editable" : true,
"visible" : true,
"domain" : null,
"defaultValue" : 2018
}
],
"indexes" : [
{
"name" : "GlobalID_Index",
"fields" : "GlobalID",
"isAscending" : false,
"isUnique" : true,
"description" : ""
},
{
"name" : "...",
"fields" : "Shape",
"isAscending" : false,
"isUnique" : false,
"description" : "Shape Index"
},
{
"name" : "PK__SampleDa__C1BEA5A2744565A2",
"fields" : "FID",
"isAscending" : true,
"isUnique" : true,
"description" : "clustered, unique, primary key"
},
{
"name" : "CreationDateIndex",
"fields" : "CreationDate",
"isAscending" : true,
"isUnique" : false,
"description" : "CreationDate Field index"
},
{
"name" : "CreatorIndex",
"fields" : "Creator",
"isAscending" : false,
"isUnique" : false,
"description" : "Creator Field index"
},
{
"name" : "EditDateIndex",
"fields" : "EditDate",
"isAscending" : true,
"isUnique" : false,
"description" : "EditDate Field index"
},
{
"name" : "EditorIndex",
"fields" : "Editor",
"isAscending" : false,
"isUnique" : false,
"description" : "Editor Field index"
}
],
"types" : [],
"templates" : [
{
"name" : "New Feature",
"description" : "",
"drawingTool" : "esriFeatureEditToolNone",
"prototype" : {
"attributes" : {
"InspectionDate" : null,
"AmtRemoved" : null,
"NeedsRepair" : "No",
"RepairType" : null,
"Notes" : null,
"YearInspected" : 2018
}
}
}
],
"supportedQueryFormats" : "JSON, geoJSON",
"hasStaticData" : false,
"maxRecordCount" : 2000,
"standardMaxRecordCount" : 32000,
"tileMaxRecordCount" : 8000,
"maxRecordCountFactor" : 1,
"capabilities" : "Create,Delete,Query,Update,Editing,Sync,ChangeTracking",
"exceedsLimitFactor" : 1
},
{
"adminLayerInfo" : {
"tableName" : "...", "geometryField" : null,
"xssTrustedFields" : ""
},
"id" : 1,
"name" : "CatchBasinInspections",
"type" : "Table",
"displayField" : "",
"description" : "",
"copyrightText" : "",
"defaultVisibility" : true,
"editingInfo" : {
"lastEditDate" : null
},
"relationships" : [
{
"id" : 1,
"name" : "myRel",
"relatedTableId" : 0,
"cardinality" : "esriRelCardinalityOneToMany",
"role" : "esriRelRoleDestination",
"keyField" : "reference",
"composite" : false
}
],
"isDataVersioned" : false,
"supportsAppend" : true,
"supportsCalculate" : true,
"supportsASyncCalculate" : true,
"supportsTruncate" : false,
"supportsAttachmentsByUploadId" : true,
"supportsAttachmentsResizing" : true,
"supportsRollbackOnFailureParameter" : true,
"supportsStatistics" : true,
"supportsExceedsLimitStatistics" : true,
"supportsAdvancedQueries" : true,
"supportsValidateSql" : true,
"supportsCoordinatesQuantization" : true,
"supportsQuantizationEditMode" : true,
"supportsApplyEditsWithGlobalIds" : false,
"advancedQueryCapabilities" : {
"supportsPagination" : true,
"supportsPaginationOnAggregatedQueries" : true,
"supportsQueryRelatedPagination" : true,
"supportsQueryWithDistance" : true,
"supportsReturningQueryExtent" : true,
"supportsStatistics" : true,
"supportsOrderBy" : true,
"supportsDistinct" : true,
"supportsQueryWithResultType" : true,
"supportsSqlExpression" : true,
"supportsAdvancedQueryRelated" : true,
"supportsCountDistinct" : true,
"supportsReturningGeometryCentroid" : false,
"supportsQueryWithDatumTransformation" : true,
"supportsHavingClause" : true,
"supportsOutFieldSQLExpression" : true,
"supportsMaxRecordCountFactor" : true,
"supportsTopFeaturesQuery" : true
},
"useStandardizedQueries" : true,
"allowGeometryUpdates" : true,
"hasAttachments" : false,
"htmlPopupType" : "esriServerHTMLPopupTypeNone",
"hasM" : false,
"hasZ" : false,
"objectIdField" : "ObjectId",
"globalIdField" : "",
"typeIdField" : "",
"fields" : [
{
"name" : "reference",
"type" : "esriFieldTypeInteger",
"actualType" : "int",
"alias" : "reference",
"sqlType" : "sqlTypeInteger",
"nullable" : true,
"editable" : true,
"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
}
],
"indexes" : [
{
"name" : "PK__CATCHBAS__9A61929115D6101B",
"fields" : "ObjectId",
"isAscending" : true,
"isUnique" : true,
"description" : "clustered, unique, primary key"
}
],
"types" : [],
"templates" : [],
"supportedQueryFormats" : "JSON, geoJSON",
"hasStaticData" : true,
"maxRecordCount" : 2000,
"standardMaxRecordCount" : 32000,
"tileMaxRecordCount" : 8000,
"maxRecordCountFactor" : 1,
"capabilities" : "Query",
"exceedsLimitFactor" : 1
}
]
}

0 Kudos
KhaledHassen
Esri Contributor

Eric,

Sorry this is not documented well so I could not find the doc for you to use. Will work with the team to get this documented.

But you will need to follows the example I have sent you. Only what I have included in my example is needed. Before you do anything, pl. send me the full url (strip the token) and the json used. The json you have used in your last message is not correct. 

>Also, how am I defining what the 2nd layer is?  As in, a pointer to the name of the 2nd layer (table) so it knows which >table is related?  I do not see any such reference in your sample.

 All what you really need to figure out is the followings:

- Origin layer Id

- Destination layer id

- Primary key in the origin layer (field name that acts as a primary key)

- Foreign key in the destination layer. 

You just need to replace these in the example json I sent you.

I have added some comments below.

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

{

  "layers" : [

  {

    "id" : 0,

    "relationships" : [

    {

      "id" : 1,

      "name" : "myRel",  // <name> for your relationshp. Any name is accepted

      "relatedTableId" : 1,  //  (table or related layer Id)  

      "cardinality" : "esriRelCardinalityOneToMany",

      "role" : "esriRelRoleOrigin",

      "keyField" : "<PrimaryKey>",  // Origin primary key like GlobalId if the relationship based on globalId

      "composite" : <true | false> // Not sure if you need composite but set it to false if you do not need it

    }

   ]

  },

  {

    "id" : 1,

    "relationships" : [

    {

      "id" : 1,

      "name" : "myRel",  // use the same name you defined above for the relationship name.

      "relatedTableId" : 0,  // This is the layerId of the origin

      "cardinality" : "esriRelCardinalityOneToMany",

      "role" : "esriRelRoleDestination",

      "keyField" : "<ForignKey>",

      "composite" : <true | false> // // Not sure if you need composite but set it to false if you do not need it

    }

   ]

 }

 ]

}

EricGardecki1
New Contributor III

I am still missing something.  Still do not know how this references the 2nd layer.  So the JSON for LayerA has one layer, id "0".  LayerB has one layer, id "0".   You keep saying just add the id of the layer but that doesn't make sense as then there would be two with "0" and that doesn't reference the other layer in any way.

For reference, LayerA is just a shapefile I uploaded. LayerB is a csv, also uploaded.

In my JSON pasted above, I added the entire section where there is a 2nd layer (made it id 1).  So removing that you would see what the existing JSON is.

So when finally adding the correct JSON, I would remove what is already existing in the addToDefinition, and replace with the new (much simpler) one?


Thanks

0 Kudos
KhaledHassen
Esri Contributor

Are you trying to add relationship between two services layer? This is not supported. Relationships can be added only between two layers that belong to the same service. As you know, within each service, the layerId is unique. If this is what you are trying to do then you might need to use query layer or view layer. Let me know if this is the case.

Khaled 

0 Kudos
EricGardecki1
New Contributor III

That is what I was doing as I didn't know of any other way.  How do I add the related table layer to the same service?  (AGOL without Desktop)

See my response to Xander below which describes what I am trying to accomplish.

Thanks

0 Kudos
BIGlovo
New Contributor II

Any luck with the documentation? Many thanks for this answer really helpful!

MGEGIS
by
New Contributor II

I know this post is old, but how do you create a many-to-many relationship via addtodefinition?

I can create one in ArcPro and then publish those layers and tables but I can't recreate that same many-to-many relationship by adding to the service definition.

When I place the below in addtodefinition of the feature service i get an error:

Error:

There are no primary or candidate keys in the referenced table 'XXX_XXX_INSPECTION_TAGS' that match the referencing column list in the foreign key 'REL_FK_XXX_XXX_INSPECTION_TAGS_HAVE_XXX_CUSTOMERS_1'. Could not create constraint or index. See previous errors.

 

JSON:

 

{
  "layers" : [
  {
    "id" : 1,
    "relationships" : [
    {
      "id" : 99,
          "name" : "Inspection Tag Customers",
          "relatedTableId" : 3,
          "cardinality" : "esriRelCardinalityManyToMany",
          "role" : "esriRelRoleOrigin",
          "keyField" : "inspection_tag_id",
          "composite" : false,
          "relationshipTableId" : 25,
          "keyFieldInRelationshipTable" : "inspection_tag_id"
    }
   ]
  },
  {
    "id" : 3,
    "relationships" : [
    {
      "id" : 99,
          "name" : "Customers Inspection Tags",
          "relatedTableId" : 1,
          "cardinality" : "esriRelCardinalityManyToMany",
          "role" : "esriRelRoleDestination",
          "keyField" : "customer_id",
          "composite" : false,
          "relationshipTableId" : 25,
          "keyFieldInRelationshipTable" : "customer_id"
    }
   ]
 }
 ]
}
0 Kudos