Browse Source

enhancement: create index for foreign key in pg (#2165)

re #2142

Signed-off-by: Pranav C <pranavxc@gmail.com>
pull/2170/head
Pranav C 3 years ago committed by GitHub
parent
commit
4d423a3830
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 57
      packages/nocodb/src/lib/noco/meta/api/columnApis.ts

57
packages/nocodb/src/lib/noco/meta/api/columnApis.ts

@ -296,6 +296,19 @@ export async function columnAdd(req: Request, res: Response<TableType>) {
parentColumn: parent.primaryKey.column_name parentColumn: parent.primaryKey.column_name
}); });
} }
// todo: create index for virtual relations as well
// create index for foreign key in pg
if (base.type === 'pg') {
await createColumnIndex({
column: new Column({
...newColumn,
fk_model_id: child.id
}),
base,
sqlMgr
});
}
} }
await createHmAndBtColumn( await createHmAndBtColumn(
child, child,
@ -450,6 +463,27 @@ export async function columnAdd(req: Request, res: Response<TableType>) {
fk_mm_parent_column_id: childCol.id, fk_mm_parent_column_id: childCol.id,
fk_related_model_id: child.id fk_related_model_id: child.id
}); });
// todo: create index for virtual relations as well
// create index for foreign key in pg
if (base.type === 'pg') {
await createColumnIndex({
column: new Column({
...associateTableCols[0],
fk_model_id: assocModel.id
}),
base,
sqlMgr
});
await createColumnIndex({
column: new Column({
...associateTableCols[1],
fk_model_id: assocModel.id
}),
base,
sqlMgr
});
}
} }
} }
Tele.emit('evt', { evt_type: 'relation:created' }); Tele.emit('evt', { evt_type: 'relation:created' });
@ -995,6 +1029,29 @@ const deleteHmOrBtRelation = async (
await Column.delete(childColumn.id, ncMeta); await Column.delete(childColumn.id, ncMeta);
}; };
async function createColumnIndex({
column,
sqlMgr,
base,
indexName = null,
nonUnique = true
}: {
column: Column;
sqlMgr: SqlMgrv2;
base: Base;
indexName?: string;
nonUnique?: boolean;
}) {
const model = await column.getModel();
const indexArgs = {
columns: [column.column_name],
tn: model.table_name,
non_unique: nonUnique,
indexName
};
sqlMgr.sqlOpPlus(base, 'indexCreate', indexArgs);
}
const router = Router({ mergeParams: true }); const router = Router({ mergeParams: true });
router.post( router.post(
'/api/v1/db/meta/tables/:tableId/columns/', '/api/v1/db/meta/tables/:tableId/columns/',

Loading…
Cancel
Save