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