fix: drop all foreign keys (#576)
This commit is contained in:
parent
93fba8fcb5
commit
ae66dd5ded
@ -44,7 +44,6 @@ export default {
|
||||
targetKey: 'name',
|
||||
foreignKey: 'collectionName',
|
||||
sortBy: 'sort',
|
||||
constraints: true,
|
||||
},
|
||||
],
|
||||
} as CollectionOptions;
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
@ -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',
|
||||
|
@ -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',
|
||||
|
@ -30,7 +30,6 @@ export default defineCollection({
|
||||
name: 'uiSchema',
|
||||
target: 'uiSchemas',
|
||||
foreignKey: 'uid',
|
||||
onDelete: 'CASCADE',
|
||||
},
|
||||
{
|
||||
type: 'belongsTo',
|
||||
|
Loading…
Reference in New Issue
Block a user