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

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

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

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

Loading…
Cancel
Save