fix: drop all foreign keys (#576)

This commit is contained in:
chenos 2022-07-04 23:48:19 +08:00 committed by GitHub
parent 93fba8fcb5
commit ae66dd5ded
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 46 additions and 5 deletions

View File

@ -44,7 +44,6 @@ export default {
targetKey: 'name',
foreignKey: 'collectionName',
sortBy: 'sort',
constraints: true,
},
],
} as CollectionOptions;

View File

@ -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();
}
}
}

View File

@ -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',

View File

@ -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',

View File

@ -30,7 +30,6 @@ export default defineCollection({
name: 'uiSchema',
target: 'uiSchemas',
foreignKey: 'uid',
onDelete: 'CASCADE',
},
{
type: 'belongsTo',