diff --git a/packages/plugins/collection-manager/src/collections/collections.ts b/packages/plugins/collection-manager/src/collections/collections.ts index 6d6faa87f..1c4ae1007 100644 --- a/packages/plugins/collection-manager/src/collections/collections.ts +++ b/packages/plugins/collection-manager/src/collections/collections.ts @@ -44,7 +44,6 @@ export default { targetKey: 'name', foreignKey: 'collectionName', sortBy: 'sort', - constraints: true, }, ], } as CollectionOptions; diff --git a/packages/plugins/collection-manager/src/migrations/20220704225714-drop-foreign-keys.ts b/packages/plugins/collection-manager/src/migrations/20220704225714-drop-foreign-keys.ts new file mode 100644 index 000000000..aa1e92a85 --- /dev/null +++ b/packages/plugins/collection-manager/src/migrations/20220704225714-drop-foreign-keys.ts @@ -0,0 +1,45 @@ +import { Migration } from '@nocobase/server'; + +export default class DropForeignKeysMigration extends Migration { + async up() { + const result = await this.app.version.satisfies('<=0.7.1-alpha.7'); + if (!result) { + return; + } + const transaction = await this.db.sequelize.transaction(); + try { + if (this.db.inDialect('mysql')) { + const [results]: any = await this.db.sequelize.query( + ` + SELECT CONCAT('ALTER TABLE ',TABLE_SCHEMA,'.',TABLE_NAME,' DROP FOREIGN KEY ',CONSTRAINT_NAME,' ;') as q + FROM information_schema.TABLE_CONSTRAINTS c + WHERE c.TABLE_SCHEMA='${this.db.options.database}' AND c.CONSTRAINT_TYPE='FOREIGN KEY'; + `, + { transaction }, + ); + for (const result of results) { + await this.db.sequelize.query(result.q as any, { transaction }); + } + } else if (this.db.inDialect('postgres')) { + const [results]: any = await this.db.sequelize.query( + ` + select 'alter table '||quote_ident(tb.relname)|| + ' drop constraint '||quote_ident(conname)||';' as q + from pg_constraint c + join pg_class tb on tb.oid = c.conrelid + join pg_namespace ns on ns.oid = tb.relnamespace + where ns.nspname in ('public') + and c.contype = 'f'; + `, + { transaction }, + ); + for (const result of results) { + await this.db.sequelize.query(result.q as any, { transaction }); + } + } + await transaction.commit(); + } catch (error) { + await transaction.rollback(); + } + } +} diff --git a/packages/plugins/ui-routes-storage/src/collections/uiRoutes.ts b/packages/plugins/ui-routes-storage/src/collections/uiRoutes.ts index b17bbdc34..de2e2cb6d 100644 --- a/packages/plugins/ui-routes-storage/src/collections/uiRoutes.ts +++ b/packages/plugins/ui-routes-storage/src/collections/uiRoutes.ts @@ -26,14 +26,12 @@ export default defineCollection({ target: 'uiRoutes', sourceKey: 'key', foreignKey: 'parentKey', - constraints: true, }, { type: 'belongsTo', name: 'uiSchema', target: 'uiSchemas', foreignKey: 'uiSchemaUid', - constraints: true, }, { type: 'json', diff --git a/packages/plugins/ui-schema-storage/src/collections/uiSchemaServerHooks.ts b/packages/plugins/ui-schema-storage/src/collections/uiSchemaServerHooks.ts index 651c9026c..2d124c396 100644 --- a/packages/plugins/ui-schema-storage/src/collections/uiSchemaServerHooks.ts +++ b/packages/plugins/ui-schema-storage/src/collections/uiSchemaServerHooks.ts @@ -6,7 +6,7 @@ export default { // autoGenId: false, timestamps: false, fields: [ - { type: 'belongsTo', name: 'uiSchema', target: 'uiSchemas', foreignKey: 'uid', onDelete: 'CASCADE' }, + { type: 'belongsTo', name: 'uiSchema', target: 'uiSchemas', foreignKey: 'uid' }, { type: 'string', name: 'type' }, { type: 'string', diff --git a/packages/plugins/ui-schema-storage/src/collections/uiSchemaTemplates.ts b/packages/plugins/ui-schema-storage/src/collections/uiSchemaTemplates.ts index a7b37c190..06adf7630 100644 --- a/packages/plugins/ui-schema-storage/src/collections/uiSchemaTemplates.ts +++ b/packages/plugins/ui-schema-storage/src/collections/uiSchemaTemplates.ts @@ -30,7 +30,6 @@ export default defineCollection({ name: 'uiSchema', target: 'uiSchemas', foreignKey: 'uid', - onDelete: 'CASCADE', }, { type: 'belongsTo',