Merge pull request #32205 from s-aga-r/fix/issue/31557

fix: unknown column error while updating value of maintain-stock in item master
diff --git a/erpnext/stock/doctype/item/item.py b/erpnext/stock/doctype/item/item.py
index 87fa72d..143fe40 100644
--- a/erpnext/stock/doctype/item/item.py
+++ b/erpnext/stock/doctype/item/item.py
@@ -945,7 +945,12 @@
 			if doctype == "Product Bundle":
 				filters = {"new_item_code": self.name}
 
-			if doctype in (
+				if linked_doc := frappe.db.get_value(
+					doctype, filters, ["new_item_code as docname"], as_dict=True
+				):
+					return linked_doc.update({"doctype": doctype})
+
+			elif doctype in (
 				"Purchase Invoice Item",
 				"Sales Invoice Item",
 			):
diff --git a/erpnext/stock/doctype/item/test_item.py b/erpnext/stock/doctype/item/test_item.py
index 89da721..1cee553 100644
--- a/erpnext/stock/doctype/item/test_item.py
+++ b/erpnext/stock/doctype/item/test_item.py
@@ -786,6 +786,36 @@
 		item.save()
 		self.assertTrue(len(item.customer_code) > 140)
 
+	def test_update_is_stock_item(self):
+		# Step - 1: Create an Item with Maintain Stock enabled
+		item = make_item(properties={"is_stock_item": 1})
+
+		# Step - 2: Disable Maintain Stock
+		item.is_stock_item = 0
+		item.save()
+		item.reload()
+		self.assertEqual(item.is_stock_item, 0)
+
+		# Step - 3: Create Product Bundle
+		pb = frappe.new_doc("Product Bundle")
+		pb.new_item_code = item.name
+		pb.flags.ignore_mandatory = True
+		pb.save()
+
+		# Step - 4: Try to enable Maintain Stock, should throw a validation error
+		item.is_stock_item = 1
+		self.assertRaises(frappe.ValidationError, item.save)
+		item.reload()
+
+		# Step - 5: Delete Product Bundle
+		pb.delete()
+
+		# Step - 6: Again try to enable Maintain Stock
+		item.is_stock_item = 1
+		item.save()
+		item.reload()
+		self.assertEqual(item.is_stock_item, 1)
+
 
 def set_item_variant_settings(fields):
 	doc = frappe.get_doc("Item Variant Settings")