Browse Source

fix: map first column as display value if no pk

Signed-off-by: mertmit <mertmit99@gmail.com>
pull/5141/head
mertmit 2 years ago
parent
commit
e1bee8a068
  1. 2
      packages/nocodb/src/lib/meta/api/helpers/populateMeta.ts
  2. 24
      packages/nocodb/src/lib/meta/helpers/mapDefaultDisplayValue.ts

2
packages/nocodb/src/lib/meta/api/helpers/populateMeta.ts

@ -229,6 +229,8 @@ export async function populateMeta(base: Base, project: Project): Promise<any> {
const columns = (await sqlClient.columnList({ tn: table.table_name }))
?.data?.list;
mapDefaultDisplayValue(columns);
/* create nc_models and its rows if it doesn't exists */
models2[table.table_name] = await Model.insert(project.id, base.id, {
table_name: table.table_name,

24
packages/nocodb/src/lib/meta/helpers/mapDefaultDisplayValue.ts

@ -4,31 +4,27 @@ export default function mapDefaultDisplayValue<T extends ColumnType>(
columnsArr: Array<T>
): void | T {
if (!columnsArr.some((column) => column.pv)) {
let len = columnsArr.length;
let pkIndex = -1;
while (len--) {
if (columnsArr[len].pk) {
pkIndex = len;
break;
}
}
const pkIndex = columnsArr.findIndex((column) => column.pk)
// if PK is at the end of table
if (pkIndex === columnsArr.length - 1) {
if (pkIndex > 0) {
columnsArr[pkIndex - 1].pv = true;
return columnsArr[pkIndex - 1];
}
} else if (columnsArr.length > 0) {
columnsArr[0].pv = true;
return columnsArr[0];
}
// pk is not at the end of table
else if (pkIndex > -1) {
} else if (pkIndex > -1) {
columnsArr[pkIndex + 1].pv = true;
return columnsArr[pkIndex + 1];
}
// no pk at all
else {
// todo:
} else {
if (columnsArr.length > 0) {
columnsArr[0].pv = true;
return columnsArr[0];
}
}
}
}

Loading…
Cancel
Save