Browse Source

fix: Handle table with no primary key

Signed-off-by: Pranav C <pranavxc@gmail.com>
pull/441/head
Pranav C 3 years ago
parent
commit
efbd30dce1
  1. 8
      packages/nocodb/package-lock.json
  2. 2
      packages/nocodb/package.json
  3. 18
      packages/nocodb/src/lib/dataMapper/lib/BaseModel.ts

8
packages/nocodb/package-lock.json generated

@ -1,6 +1,6 @@
{
"name": "nocodb",
"version": "0.11.10",
"version": "0.11.11",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
@ -11817,9 +11817,9 @@
"integrity": "sha512-3AryS9uwa5NfISLxMciUonrH7YfXp+nlahB9T7girXIsLQrmwX4MdnuKs32akduCOGpKmjTJSWmATULbuMkbfw=="
},
"nc-help": {
"version": "0.2.10",
"resolved": "https://registry.npmjs.org/nc-help/-/nc-help-0.2.10.tgz",
"integrity": "sha512-XkLvKf3clpewn576RGX84nsNBWDjvFJFsaNPKw0n77zcjlC6zeBOX6fGJsTVEBg56usr0jaK+LfOtJUUEc9D+A==",
"version": "0.2.11",
"resolved": "https://registry.npmjs.org/nc-help/-/nc-help-0.2.11.tgz",
"integrity": "sha512-588PlynqD01a6XXR1jX5mYqP2kXrkXeCv4bpPAElumiwJKdFZI/A5maaAQU7yVz/t7Qid1Lwz9TUyMG/XTptzg==",
"requires": {
"axios": "^0.21.1",
"boxen": "^4.2.0",

2
packages/nocodb/package.json

@ -145,7 +145,7 @@
"mysql2": "^2.2.5",
"nanoid": "^3.1.20",
"nc-common": "0.0.6",
"nc-help": "^0.2.10",
"nc-help": "^0.2.11",
"nc-lib-gui": "^0.2.16",
"nc-plugin": "^0.1.1",
"nodemailer": "^6.4.10",

18
packages/nocodb/src/lib/dataMapper/lib/BaseModel.ts

@ -161,7 +161,7 @@ abstract class BaseModel {
const ids = (id + '').split('___');
const where = {};
for (let i = 0; i < this.pks.length; ++i) {
where[this.pks[i].cn] = ids[i];
where[this.pks?.[i]?.cn] = ids[i];
}
return where;
}
@ -303,7 +303,7 @@ abstract class BaseModel {
}
const response = await this.dbDriver.batchInsert(this.tn, data, 50)
.returning(this.pks[0].cn);
.returning(this.pks?.[0]?.cn || '*');
await this.afterInsertb(data);
@ -454,7 +454,7 @@ abstract class BaseModel {
*/
async countByPk({where, condition}) {
try {
return await this._run(this.$db.count(`${(this.pks[0] || this.columns[0]).cn} as count`)
return await this._run(this.$db.count(`${(this.pks?.[0] || this.columns[0]).cn} as count`)
.xwhere(where)
.condition(condition)
.first());
@ -481,7 +481,7 @@ abstract class BaseModel {
return await this._run(this.$db.where(this._whereFk({
parentId,
tnp
})).count(`${(this.pks[0] || this.columns[0]).cn} as count`)
})).count(`${(this.pks?.[0] || this.columns[0]).cn} as count`)
.xwhere(where)
.condition(condition)
.first());
@ -722,7 +722,7 @@ abstract class BaseModel {
const columns = [...(column_name ? [column_name] : []), ...fields.split(',').filter(Boolean)];
const query = this.$db
.groupBy(columns)
.count(`${(this.pks[0] || this.columns[0]).cn} as count`)
.count(`${(this.pks?.[0] || this.columns[0]).cn} as count`)
.select(columns)
.xhaving(having);
@ -924,7 +924,7 @@ abstract class BaseModel {
parent.map(p => {
const query = this
.dbDriver(child)
.where({[cn]: p[this.pks[0].cn]})
.where({[cn]: p[this.pks?.[0]?.cn]})
.xwhere(where).condition(condition)
.select(...fields.split(','));
@ -935,7 +935,7 @@ abstract class BaseModel {
const gs = _.groupBy(childs, cn);
parent.forEach(row => {
row[child] = gs[row[this.pks[0].cn]] || [];
row[child] = gs[row[this.pks?.[0]?.cn]] || [];
})
}
@ -990,8 +990,8 @@ abstract class BaseModel {
fields = fields || f || '*';
try {
if (fields !== '*' && fields.split(',').indexOf(this.pks[0].cn) === -1) {
fields += ',' + this.pks[0].cn;
if (fields !== '*' && fields.split(',').indexOf(this.pks?.[0]?.cn) === -1) {
fields += ',' + this.pks?.[0]?.cn;
}
const parent = await this.list({childs, where, fields, ...rest});

Loading…
Cancel
Save