Index: openacs-4/packages/acs-content-repository/sql/postgresql/content-create.sql
===================================================================
RCS file: /usr/local/cvsroot/openacs-4/packages/acs-content-repository/sql/postgresql/content-create.sql,v
diff -u -r1.65.2.1 -r1.65.2.2
--- openacs-4/packages/acs-content-repository/sql/postgresql/content-create.sql	14 Aug 2019 07:34:35 -0000	1.65.2.1
+++ openacs-4/packages/acs-content-repository/sql/postgresql/content-create.sql	24 Mar 2021 10:09:58 -0000	1.65.2.2
@@ -197,8 +197,16 @@
   storage_area_key    varchar(100) default 'CR_FILES' not null,
   tree_sortkey        varbit not null,
   max_child_sortkey   varbit
-);  
+);
 
+--
+-- Avoid potential loops on parent_ids. An item must not be equal to
+-- its own parent.  Note that this constraint is not guaranteed to
+-- avoid all loops; it is still possible to create indirect recursive
+-- loops but excludes some real-world problems.
+--
+ALTER TABLE cr_items ADD CONSTRAINT cr_items_parent_id_ck CHECK (item_id != parent_id);
+
 create index cr_items_by_locale on cr_items(locale);
 create index cr_items_by_content_type on cr_items(content_type);
 create unique index cr_items_by_live_revision on cr_items(live_revision);
