diff --git a/CHANGELOG b/CHANGELOG
index e2a6517f127c70939763aff90c5c35c5d17b18b2..3ceaad7be87b2942b1bd7ed77ed42b908deaf507 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,6 +1,8 @@
 minerva (13.1.1) stable; urgency=medium
   * Bug fix: adding gene mapping before the genome caused an error (#835)
   * Bug fix: "Terms of Use" change to "Terms of Service" in all places (#843)
+  * Bug fix: upgrade to 13.1.0 crashed on machines where every element was 
+    inside compartment (#856)
 
  -- Piotr Gawron <piotr.gawron@uni.lu>  Fri, 28 Jun 2019 17:00:00 +0200
 
diff --git a/persist/src/main/resources/db/migration/13.1.0~alpha.0/V13.1.0.20190503__z_index_to_elements_added.sql b/persist/src/main/resources/db/migration/13.1.0~alpha.0/V13.1.0.20190503__z_index_to_elements_added.sql
index 74c76beee46fe5dabc84c611ed777be3cd886deb..918295f3cce14ae6a9ac5004a4b6246579d34dbd 100644
--- a/persist/src/main/resources/db/migration/13.1.0~alpha.0/V13.1.0.20190503__z_index_to_elements_added.sql
+++ b/persist/src/main/resources/db/migration/13.1.0~alpha.0/V13.1.0.20190503__z_index_to_elements_added.sql
@@ -2,6 +2,8 @@
 alter table element_table add column z integer;
 alter table reaction_table add column z integer;
 
+--add artificial element outside compartment, so the update queries will always return non-null values
+insert into element_table (id, element_type_db, visibility_level, width, height) values(-1, 'GENERIC_PROTEIN', 0, 1,1);
 
 with v_element_table as
 (
@@ -69,3 +71,6 @@ update element_table set z = (select max(z) from element_table) where z is null;
 
 ALTER TABLE element_table ALTER COLUMN z SET NOT NULL;
 ALTER TABLE reaction_table ALTER COLUMN z SET NOT NULL;
+
+--remove artificial element added at the beginning of the script
+delete from element_table where id = -1;