Select to view content in your preferred language

FeatureLayerCollection.replicas.create always returns the correct tables but 0 records in the tables

375
5
Monday
Trippetoe
Frequent Contributor

Hi there. I have a python script that attempts to export a hosted feature service as a file geodatabase using the  python api's featurelayercollection.replicas.create() method. I am running python 3.11.10 and arcgis api 2.3.0

The service details:

- One feature layer (id: 0, geometry type: point) with attachments
- One related table (id: 1) with attachments
- Sync is enabled
- supportsSyncModelNone = true

The problem:

the replicas.create() operation consistently returns a file geodatabase with the correct schema but 0 rows in all tables, regardless of parameters used.

What works:

  • Using the AGOL Item Details page to Export as file geodatabase.
  • A direct query on layer 0 returns the expected 27 records
  • Running createReplica manually via the REST Services Directory HTML form returns the expected number of records

What I've have tried (all returning 0 rows):

  • syncModel=none with no geometry filter
  • syncModel=none with geometry set to the service's full extent
  • Adding layerQueries with where=1=1, useGeometry=false, includeRelated=true, queryOption=useFilter for both layers
  • syncDirection=bidirectional
  • geometryType=esriGeometryPoint
  • async=true and async=false
  • layers as comma-separated string (0,1) and as JSON array ([0,1])
  • using an empty querylayers
  • using a populated queryLayers with filter set to 1=1

Any thoughts on how to get the expected number of records returned?

5 Replies
KenGalliher1
Esri Contributor

@Trippetoe This is a great question. I'm looking into it now. Can you provide a code snippet that shows how you are setting up the replica? What are the sync options, geometry and layer queries look like?

0 Kudos
Trippetoe
Frequent Contributor

Thanks for your response. I'm a bit behind you so i'll answer this response first.  Here's my python code that uses `replicas.create()`. As noted in my question, the code returns an empty file geodatabase...

edit_date_filter = "EditDate >= '"+str(last_successful_run_date)+"'"
feat_srvc = arcgis.features.FeatureLayerCollection(settings.AGOL_FEATURE_SERVICE_URL, agol_security_token)

export_job = feat_srvc.replicas.create(
	replica_name='NameDoesNothing',
	layers=[settings.FS_IDX, settings.CONDITION_IDX],                          
	layer_queries={
		str(settings.FS_IDX):{
			"queryOption":"useFilter", "where":edit_date_filter},
		str(settings.CONDITION_IDX):{
			"queryOption":"useFilter", "where":edit_date_filter}
	},
	geometry_filter=None,
	replica_sr=None,
	transport_type='esriTransportTypeUrl',
	return_attachments=True,
	return_attachments_databy_url=False, 
	asynchronous=True, 
	attachments_sync_direction='download',
	sync_model='none', 
	data_format="filegdb",
	replica_options=None, 
	wait=False,
	out_path=settings.DOWNLOAD_DIR,
	sync_direction=None, 
	target_type='client')

 

Just for good measure, i'm including this 'direct request' against the createReplicas endpoint. the results are the same. a perfectly fine, but empty, file geodatabase.....

url = f"{feat_srvc.url}/createReplica"

data = {
	'f': 'json',
	'replicaName': 'test_direct4',
	'layers': '[0,1]',
	'layerQueries': '{"0": {"where": "1=1", "useGeometry": false, "includeRelated": true, "queryOption": "useFilter"}, "1": {"where": "1=1", "useGeometry": false, "includeRelated": true, "queryOption": "useFilter"}}',
	'geometryType': 'esriGeometryPoint',
	'returnAttachments': 'false',
	'syncModel': 'none',
	'syncDirection': 'bidirectional',
	'dataFormat': 'filegdb',
	'async': 'false',
	'token': agol_security_token._con.token
}

response = requests.post(url, data=data)
print(response.json())

dl_url = response.json().get('responseUrl')
resp = requests.get(url=dl_url, params={'token': agol_security_token._con.token})
zipped = zipfile.ZipFile(io.BytesIO(resp.content))

0 Kudos
KenGalliher1
Esri Contributor

@Trippetoe I was not able to reproduce the missing rows with a file geodatabase or SQLite geodatabase. Here is the code I used. Let me know if you have any questions.

 

import time
import tempfile
from arcgis.features.layer import FeatureLayerCollection
from arcgis.gis import GIS
from arcgis import geometry

gis = GIS(profile="your_online_profile")
item = gis.content.get("401876e7f782419dbd29974ce8a4e11b")
flc = FeatureLayerCollection(item.url, gis)

output_gdb_path = tempfile.TemporaryDirectory()

service_url = flc.url

extent = {
    "xmin": -13238397.099502765,
    "ymin": 3923105.9219284547,
    "xmax": -13037402.432768419,
    "ymax": 4065046.3578960407,
}
geom_filter = geometry.filters.envelope_intersects(extent)
sync_options = {"syncDataOptions": 260}
layer_queries = {
    "0": {"queryOption": "useFilter", "useGeometry": True},
    "2": {"queryOption": "useFilter", "useGeometry": False},
}

# For file gdb, the sync model must be none
# For mobile gdb, use "perLayer" or "perReplica"
sync_model = "none"
data_format = "filegdb"
replica_options = {
    "replica_name": f"ReplicaTest_{int(time.time())}",
    "layers": [0, 2],
    "layer_queries": layer_queries,
    "geometry_filter": geom_filter,
    "replica_sr": {"wkid": 102642, "latestWkid": 2226},
    "transport_type": "esriTransportTypeUrl",
    "return_attachments": True,
    "attachments_sync_direction": "bidirectional",
    "sync_model": sync_model,
    "data_format": data_format,
    "replica_options": sync_options,
    "out_path": output_gdb_path.name,
    "sync_direction": "bidirectional",
}

try:
    res = flc.replicas.create(**replica_options)
    print(res)
except Exception as ex:
    print("Create Replica failed:", str(ex))
finally:
    output_gdb_path.cleanup()
0 Kudos
Trippetoe
Frequent Contributor

@KenGalliher1 Thanks for the code example. I changed the code as necessary for my hosted feature service and ran it. unfortunately, i got the same results: an other wise perfect file geodatabase with my related tables, attachment tables, etc - but there we no rows in the feature class, the related table, nor either of the attachment tables. The only table with any rows was the GDB_ServiceItems table.

I don't know why really, but a hunch is that the attachments in the related table are causing the problem. Does your hosted feature service have attachments enabled on the related table (index 2 i believe in your example)?

0 Kudos
KenGalliher1
Esri Contributor

@Trippetoe Yes, I enabled and added attachments on the feature layer and the table. Thanks for providing your parameters. I'll test some of those in my code. I'll let you know what happens.

0 Kudos