diff --git a/packages/nc-gui/components/project/spreadsheet/components/headerCell.vue b/packages/nc-gui/components/project/spreadsheet/components/headerCell.vue
index d88fd79bcd..857b227324 100644
--- a/packages/nc-gui/components/project/spreadsheet/components/headerCell.vue
+++ b/packages/nc-gui/components/project/spreadsheet/components/headerCell.vue
@@ -2,8 +2,6 @@
-
-
mdi-key-variant
{{ uiDatatypeIcon }}
@@ -31,12 +29,21 @@
mdi-menu-down
-
- mdi-pencil
+
+ mdi-pencil
Edit
+
+ mdi-key-star
+
+
+ Set as Primary value
+
+ Primary value will be shown in place of primary key
+
+
- mdi-delete-outline
+ mdi-delete-outline
Delete
@@ -89,7 +96,7 @@ import EditColumn from "@/components/project/spreadsheet/editColumn/editColumn";
export default {
components: {EditColumn},
- props: ['value', 'column', 'isForeignKey', 'meta', 'nodes', 'columnIndex', 'isForm', 'isPublicView','isVirtual'],
+ props: ['value', 'column', 'isForeignKey', 'meta', 'nodes', 'columnIndex', 'isForm', 'isPublicView', 'isVirtual'],
name: "headerCell",
mixins: [cell],
data: () => ({
@@ -116,6 +123,34 @@ export default {
} catch (e) {
console.log(e)
}
+ }, async setAsPrimaryValue() {
+ try {
+ const meta = JSON.parse(JSON.stringify(this.meta));
+ for (const col of meta.columns) {
+ if (col.pv) {
+ delete col.pv;
+ }
+ if (col.cn === this.column.cn) {
+ col.pv = true;
+ }
+ }
+
+
+ await this.$store.dispatch('sqlMgr/ActSqlOp', [{
+ env: this.nodes.env,
+ dbAlias: this.nodes.dbAlias
+ }, 'xcModelSet', {
+ tn: this.nodes.tn,
+ meta
+ }]);
+ this.$toast.success('Successfully updated as primary column').goAway(3000);
+ } catch (e) {
+ console.log(e)
+ this.$toast.error('Failed to update primary column').goAway(3000);
+ }
+ this.$emit('saved');
+ this.columnDeleteDialog = false;
+
}
}
}
diff --git a/packages/nc-gui/components/project/spreadsheet/mixins/spreadsheet.js b/packages/nc-gui/components/project/spreadsheet/mixins/spreadsheet.js
index 4509a28c17..cf6f3c653f 100644
--- a/packages/nc-gui/components/project/spreadsheet/mixins/spreadsheet.js
+++ b/packages/nc-gui/components/project/spreadsheet/mixins/spreadsheet.js
@@ -146,8 +146,8 @@ export default {
return this.nodes.tn || this.nodes.view_name
},
primaryValueColumn() {
- if (!this.meta || !this.availableColumns) return '';
- return this.availableColumns.length ? this.availableColumns[0]._cn : '';
+ if (!this.meta || !this.availableColumns || !this.availableColumns.length) return '';
+ return (this.availableColumns.find(col => col.pv) || {_cn: ''})._cn;
},
},
watch: {
diff --git a/packages/nocodb/package.json b/packages/nocodb/package.json
index 1d16755fe5..ed81c119ae 100644
--- a/packages/nocodb/package.json
+++ b/packages/nocodb/package.json
@@ -125,7 +125,6 @@
"handlebars": "^4.7.6",
"import-fresh": "^3.2.1",
"inflection": "^1.12.0",
- "is-docker": "^2.2.1",
"js-beautify": "^1.11.0",
"json2csv": "^5.0.6",
"jsonfile": "^6.1.0",
diff --git a/packages/nocodb/src/lib/sqlMgr/code/models/xc/ModelXcMetaMysql.ts b/packages/nocodb/src/lib/sqlMgr/code/models/xc/ModelXcMetaMysql.ts
index c0db7cc5a7..e4caf3d7dd 100644
--- a/packages/nocodb/src/lib/sqlMgr/code/models/xc/ModelXcMetaMysql.ts
+++ b/packages/nocodb/src/lib/sqlMgr/code/models/xc/ModelXcMetaMysql.ts
@@ -20,7 +20,7 @@ class ModelXcMetaMysql extends BaseRender {
*/
prepare() {
- const data:any = {};
+ const data: any = {};
/* example of simple variable */
data.tn = this.ctx.tn;
@@ -181,7 +181,53 @@ class ModelXcMetaMysql extends BaseRender {
columnsArr.push(columnObj)
}
+ // pk can be at the end
+ //
+
+
+ /*
+
+ if PK is at the end of table
+ if (there is a column for PV)
+ make that PV
+ else
+ lets think
+ else if (pk is not at the end of table)
+ if (there is a column for PV)
+ make that PV
+ else
+ lets think
+ else if ( no pk at all)
+ let's think
+ */
+
+ if (!columnsArr.some(column => column.pv)) {
+ let len = columnsArr.length;
+ let pkIndex = -1;
+
+ while (len--) {
+ if (columnsArr[len].pk) {
+ pkIndex = len;
+ break;
+ }
+ }
+
+ // if PK is at the end of table
+ if (pkIndex === columnsArr.length - 1) {
+ if (pkIndex > 0) {
+ columnsArr[pkIndex - 1].pv = true;
+ }
+ }
+ // pk is not at the end of table
+ else if (pkIndex > -1) {
+ columnsArr[pkIndex + 1].pv = true;
+ }
+ // no pk at all
+ else {
+ // todo:
+ }
+ }
return columnsArr;
}
@@ -319,7 +365,7 @@ class ModelXcMetaMysql extends BaseRender {
}
- _getUIDataType(col):any {
+ _getUIDataType(col): any {
switch (this.getAbstractType(col)) {
case 'integer':
return 'Number';
@@ -356,7 +402,7 @@ class ModelXcMetaMysql extends BaseRender {
}
- getAbstractType(col):any {
+ getAbstractType(col): any {
switch ((col.dt || col.dt).toLowerCase()) {
case "int":
case "smallint":