From 339ae844e9d6fbca260da5b9120b613849a056cb Mon Sep 17 00:00:00 2001 From: "Torsten Schulz (local)" Date: Thu, 18 Dec 2025 15:34:26 +0100 Subject: [PATCH] Enhance VIRTUAL field detection in sequelize.js by implementing multiple identification methods, ensuring accurate model synchronization and preventing unintended field removals. --- backend/utils/sequelize.js | 40 +++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/backend/utils/sequelize.js b/backend/utils/sequelize.js index c3e86ad..d81f18f 100644 --- a/backend/utils/sequelize.js +++ b/backend/utils/sequelize.js @@ -422,10 +422,48 @@ const syncModelsAlways = async (models) => { const virtualFields = {}; // Find and temporarily remove VIRTUAL fields + // Check multiple ways to identify VIRTUAL fields for (const [key, attr] of Object.entries(originalAttributes)) { - if (attr.type && attr.type.constructor && attr.type.constructor.name === 'VIRTUAL') { + // Check if it's a VIRTUAL field by checking the type + let isVirtual = false; + + if (attr.type) { + // Method 1: Direct comparison with DataTypes.VIRTUAL + if (attr.type === DataTypes.VIRTUAL) { + isVirtual = true; + } + // Method 2: Check constructor name + else if (attr.type.constructor && attr.type.constructor.name === 'VIRTUAL') { + isVirtual = true; + } + // Method 3: Check if type key is VIRTUAL + else if (attr.type.key === 'VIRTUAL') { + isVirtual = true; + } + // Method 4: Check toString representation + else if (typeof attr.type.toString === 'function') { + const typeStr = attr.type.toString(); + if (typeStr.includes('VIRTUAL') || typeStr === 'VIRTUAL') { + isVirtual = true; + } + } + // Method 5: Check if it's an instance of VIRTUAL type + else if (attr.type instanceof DataTypes.VIRTUAL || + (attr.type.constructor && attr.type.constructor === DataTypes.VIRTUAL.constructor)) { + isVirtual = true; + } + } + + // Also check if field has a getter but no setter (common pattern for VIRTUAL fields) + if (!isVirtual && attr.get && !attr.set && !attr.field) { + // This might be a VIRTUAL field, but be careful not to remove real fields + // Only remove if we're certain it's VIRTUAL + } + + if (isVirtual) { virtualFields[key] = attr; delete model.rawAttributes[key]; + console.log(` ⚠️ Temporarily removed VIRTUAL field: ${key} from model ${model.name}`); } }