Enhance error handling in sequelize.js for Foreign Key Constraint Errors by adding logging for orphaned records and skipping problematic models during synchronization. Update syncDatabase.js to include cleanup logic for orphaned political_office entries, improving database integrity and user feedback during sync operations.
This commit is contained in:
@@ -577,6 +577,17 @@ const syncModelsAlways = async (models) => {
|
||||
// Überspringe dieses Model und fahre mit dem nächsten fort
|
||||
continue;
|
||||
}
|
||||
// Wenn Sequelize einen Foreign Key Constraint Fehler hat, entferne verwaiste Einträge oder überspringe das Model
|
||||
else if (syncError.name === 'SequelizeForeignKeyConstraintError' || (syncError.message && (syncError.message.includes('FOREIGN KEY') || syncError.message.includes('Fremdschlüssel')))) {
|
||||
const tableName = model.tableName;
|
||||
const schema = model.options?.schema || 'public';
|
||||
console.error(` ❌ Cannot sync ${model.name} (${schema}.${tableName}) due to Foreign Key Constraint Error`);
|
||||
console.error(` ❌ Detail: ${syncError.parent?.detail || syncError.message}`);
|
||||
console.error(` ⚠️ This usually means there are orphaned records. Cleanup should have removed them.`);
|
||||
console.error(` ⚠️ Skipping sync for ${model.name} - please check and fix orphaned records manually`);
|
||||
// Überspringe dieses Model und fahre mit dem nächsten fort
|
||||
continue;
|
||||
}
|
||||
// Wenn Sequelize versucht, Foreign Keys zu erstellen, entferne sie nach dem Fehler
|
||||
else if (syncError.message && syncError.message.includes('REFERENCES')) {
|
||||
console.log(` ⚠️ Sequelize tried to create FK despite constraints: false, removing any created FKs...`);
|
||||
|
||||
Reference in New Issue
Block a user