Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GeoDjango - LayerMapping: An error occurred in the current transaction. You can't execute queries until the end of the 'atomic' block

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?

like image 252
Valachio Avatar asked Dec 19 '25 22:12

Valachio


1 Answers

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.

like image 63
Scott Avatar answered Dec 21 '25 22:12

Scott



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!