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

2
packages/nocodb/package.json

@ -145,7 +145,7 @@
"mysql2": "^2.2.5", "mysql2": "^2.2.5",
"nanoid": "^3.1.20", "nanoid": "^3.1.20",
"nc-common": "0.0.6", "nc-common": "0.0.6",
"nc-help": "^0.2.10", "nc-help": "^0.2.11",
"nc-lib-gui": "^0.2.16", "nc-lib-gui": "^0.2.16",
"nc-plugin": "^0.1.1", "nc-plugin": "^0.1.1",
"nodemailer": "^6.4.10", "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 ids = (id + '').split('___');
const where = {}; const where = {};
for (let i = 0; i < this.pks.length; ++i) { 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; return where;
} }
@ -303,7 +303,7 @@ abstract class BaseModel {
} }
const response = await this.dbDriver.batchInsert(this.tn, data, 50) const response = await this.dbDriver.batchInsert(this.tn, data, 50)
.returning(this.pks[0].cn); .returning(this.pks?.[0]?.cn || '*');
await this.afterInsertb(data); await this.afterInsertb(data);
@ -454,7 +454,7 @@ abstract class BaseModel {
*/ */
async countByPk({where, condition}) { async countByPk({where, condition}) {
try { 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) .xwhere(where)
.condition(condition) .condition(condition)
.first()); .first());
@ -481,7 +481,7 @@ abstract class BaseModel {
return await this._run(this.$db.where(this._whereFk({ return await this._run(this.$db.where(this._whereFk({
parentId, parentId,
tnp tnp
})).count(`${(this.pks[0] || this.columns[0]).cn} as count`) })).count(`${(this.pks?.[0] || this.columns[0]).cn} as count`)
.xwhere(where) .xwhere(where)
.condition(condition) .condition(condition)
.first()); .first());
@ -722,7 +722,7 @@ abstract class BaseModel {
const columns = [...(column_name ? [column_name] : []), ...fields.split(',').filter(Boolean)]; const columns = [...(column_name ? [column_name] : []), ...fields.split(',').filter(Boolean)];
const query = this.$db const query = this.$db
.groupBy(columns) .groupBy(columns)
.count(`${(this.pks[0] || this.columns[0]).cn} as count`) .count(`${(this.pks?.[0] || this.columns[0]).cn} as count`)
.select(columns) .select(columns)
.xhaving(having); .xhaving(having);
@ -924,7 +924,7 @@ abstract class BaseModel {
parent.map(p => { parent.map(p => {
const query = this const query = this
.dbDriver(child) .dbDriver(child)
.where({[cn]: p[this.pks[0].cn]}) .where({[cn]: p[this.pks?.[0]?.cn]})
.xwhere(where).condition(condition) .xwhere(where).condition(condition)
.select(...fields.split(',')); .select(...fields.split(','));
@ -935,7 +935,7 @@ abstract class BaseModel {
const gs = _.groupBy(childs, cn); const gs = _.groupBy(childs, cn);
parent.forEach(row => { 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 || '*'; fields = fields || f || '*';
try { try {
if (fields !== '*' && fields.split(',').indexOf(this.pks[0].cn) === -1) { if (fields !== '*' && fields.split(',').indexOf(this.pks?.[0]?.cn) === -1) {
fields += ',' + this.pks[0].cn; fields += ',' + this.pks?.[0]?.cn;
} }
const parent = await this.list({childs, where, fields, ...rest}); const parent = await this.list({childs, where, fields, ...rest});

Loading…
Cancel
Save