3

I am using LayerMapping to add a shapefile into the database. Here is the code I am using. The shapefile is ~100MB so there are a bunch of polygons to add.

mapping = {'name': 'OBJECTID', 'poly': 'POLYGON'}
lm = LayerMapping(TestGeo, 'toronto geo/PROPERTY_BOUNDARIES_WGS84.shp', mapping)
lm.save(verbose=True)

After running the above code, I'd see about 10 seconds of success messages, showing something like Saved: 'name': 12345. Then 10 seconds later, the messages turn into:

Failure to save: {'name': 12345, 'poly': (....)}: An error occurred in the current transaction. You can't execute queries until the end of the 'atomic' block.

It still keeps running after displaying the error message (I'm guessing it moved onto the next polygon object). After it goes through each polygon, despite the fact that the first couple hundred or so polygons showed a success message, none of the polygons from the shapefile were saved.

I came across a similar error but the content doesn't seem to be related.

Any idea why this is happening?

Valachio
  • 1,025
  • 2
  • 18
  • 40

2 Answers2

1

I also had a similar issue. Turns out that my model was misconfigured and the error cropped up in the first iteration of the save function. I'm guessing some sort of database lock remains on and subsequent calls give you the atomic block problem? Try running lm.save(verbose=True, strict=True) to catch any IntegrityError.

Scott
  • 111
  • 9
0

It seems that there are MultiPolygon in source layer.

change your mode geometry to models.MultiPolygonField solve your problem.