Implement logic to create tables without Foreign Key constraints in sequelize.js when referenced tables do not exist. Enhance error handling and logging to provide clear feedback during synchronization attempts, improving robustness in model management.
This commit is contained in:
@@ -577,6 +577,60 @@ const syncModelsAlways = async (models) => {
|
||||
// Überspringe dieses Model und fahre mit dem nächsten fort
|
||||
continue;
|
||||
}
|
||||
// Wenn eine referenzierte Tabelle noch nicht existiert, erstelle die Tabelle ohne Foreign Key
|
||||
else if (syncError.message && (syncError.message.includes('existiert nicht') || syncError.message.includes('does not exist') || syncError.message.includes('Relation'))) {
|
||||
const tableName = model.tableName;
|
||||
const schema = model.options?.schema || 'public';
|
||||
console.warn(` ⚠️ Cannot create ${model.name} (${schema}.${tableName}) with Foreign Key - referenced table does not exist yet`);
|
||||
console.warn(` ⚠️ Attempting to create table without Foreign Key constraint...`);
|
||||
|
||||
try {
|
||||
// Prüfe, ob die Tabelle bereits existiert
|
||||
const [tableExists] = await sequelize.query(`
|
||||
SELECT EXISTS (
|
||||
SELECT 1 FROM information_schema.tables
|
||||
WHERE table_schema = :schema
|
||||
AND table_name = :tableName
|
||||
) as exists
|
||||
`, {
|
||||
replacements: { schema, tableName },
|
||||
type: sequelize.QueryTypes.SELECT
|
||||
});
|
||||
|
||||
if (tableExists && tableExists.exists) {
|
||||
console.log(` ℹ️ Table ${schema}.${tableName} already exists, skipping creation`);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Erstelle die Tabelle manuell ohne Foreign Key
|
||||
// Verwende queryInterface.createTable mit den Attributen, aber ohne Foreign Keys
|
||||
const queryInterface = sequelize.getQueryInterface();
|
||||
const attributes = {};
|
||||
|
||||
// Kopiere alle Attribute aus dem Model, aber entferne references
|
||||
for (const [key, attr] of Object.entries(model.rawAttributes)) {
|
||||
attributes[key] = { ...attr };
|
||||
// Entferne references, damit kein Foreign Key erstellt wird
|
||||
if (attributes[key].references) {
|
||||
delete attributes[key].references;
|
||||
}
|
||||
}
|
||||
|
||||
// Erstelle die Tabelle mit queryInterface.createTable ohne Foreign Keys
|
||||
await queryInterface.createTable(tableName, attributes, {
|
||||
schema,
|
||||
// Stelle sicher, dass keine Foreign Keys erstellt werden
|
||||
charset: model.options?.charset,
|
||||
collate: model.options?.collate
|
||||
});
|
||||
console.log(` ✅ Table ${schema}.${tableName} created successfully without Foreign Key`);
|
||||
} catch (createError) {
|
||||
console.error(` ❌ Failed to create table ${schema}.${tableName} without Foreign Key:`, createError.message);
|
||||
console.error(` ⚠️ Skipping ${model.name} - will retry after dependencies are created`);
|
||||
// Ü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;
|
||||
|
||||
Reference in New Issue
Block a user