fix: scope key error (#5322)
Some checks are pending
auto-merge / push-commit (push) Waiting to run
Build Docker Image / build-and-push (push) Waiting to run
Build Pro Image / build-and-push (push) Waiting to run
E2E / Build (push) Waiting to run
E2E / Core and plugins (push) Blocked by required conditions
E2E / plugin-workflow (push) Blocked by required conditions
E2E / plugin-workflow-approval (push) Blocked by required conditions
E2E / plugin-data-source-main (push) Blocked by required conditions
E2E / Comment on PR (push) Blocked by required conditions
NocoBase Backend Test / sqlite-test (20, false) (push) Waiting to run
NocoBase Backend Test / sqlite-test (20, true) (push) Waiting to run
NocoBase Backend Test / postgres-test (public, 20, nocobase, false) (push) Waiting to run
NocoBase Backend Test / postgres-test (public, 20, nocobase, true) (push) Waiting to run
NocoBase Backend Test / postgres-test (public, 20, public, false) (push) Waiting to run
NocoBase Backend Test / postgres-test (public, 20, public, true) (push) Waiting to run
NocoBase Backend Test / postgres-test (user_schema, 20, nocobase, false) (push) Waiting to run
NocoBase Backend Test / postgres-test (user_schema, 20, nocobase, true) (push) Waiting to run
NocoBase Backend Test / postgres-test (user_schema, 20, public, false) (push) Waiting to run
NocoBase Backend Test / postgres-test (user_schema, 20, public, true) (push) Waiting to run
NocoBase Backend Test / mysql-test (20, false) (push) Waiting to run
NocoBase Backend Test / mysql-test (20, true) (push) Waiting to run
NocoBase Backend Test / mariadb-test (20, false) (push) Waiting to run
NocoBase Backend Test / mariadb-test (20, true) (push) Waiting to run
Test on Windows / build (push) Waiting to run

* fix: scope key error

* fix: group by scope key

* fix: test
This commit is contained in:
ChengLei Shao 2024-09-26 08:21:02 +08:00 committed by GitHub
parent 786e1e4f25
commit 2199969579
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 19 additions and 15 deletions

View File

@ -11,7 +11,7 @@ import { Database } from '../../database';
import { mockDatabase } from '../';
import { SortField } from '../../fields';
describe('string field', () => {
describe('sort field', () => {
let db: Database;
beforeEach(async () => {
@ -37,7 +37,7 @@ describe('string field', () => {
},
{
type: 'string',
name: 'scopeKey',
name: 'someField',
},
],
});
@ -45,13 +45,19 @@ describe('string field', () => {
await db.sync();
await Test.repository.create({
values: {
name: 't1',
scopeKey: 'a',
},
values: [
{
name: 't1',
someField: 'a',
},
{
name: 't2',
someField: 'b',
},
],
});
Test.setField('scopeKeySort', { type: 'sort', scopeKey: 'scopeKey' });
Test.setField('scopeKeySort', { type: 'sort', scopeKey: 'someField' });
await db.sync();
});

View File

@ -460,10 +460,6 @@ export class Database extends EventEmitter implements AsyncEmitter {
}
if (options.underscored) {
if (lodash.get(options, 'sortable.scopeKey')) {
options.sortable.scopeKey = snakeCase(options.sortable.scopeKey);
}
if (lodash.get(options, 'indexes')) {
// change index fields to snake case
options.indexes = options.indexes.map((index) => {

View File

@ -9,7 +9,7 @@
import { Mutex } from 'async-mutex';
import { isNumber } from 'lodash';
import { DataTypes } from 'sequelize';
import { DataTypes, Sequelize } from 'sequelize';
import { BaseColumnFieldOptions, Field } from './field';
const sortFieldMutex = new Mutex();
@ -171,10 +171,12 @@ export class SortField extends Field {
};
const scopeKey = this.options.scopeKey;
if (scopeKey) {
const groups = await this.collection.repository.find({
attributes: [scopeKey],
group: [scopeKey],
const scopeKeyColumn = this.collection.model.rawAttributes[scopeKey].field;
const groups = await this.collection.model.findAll({
attributes: [[Sequelize.fn('DISTINCT', Sequelize.col(scopeKeyColumn)), scopeKey]],
raw: true,
transaction,
});