fix: sort field with table dose not have primary key (#1119)

This commit is contained in:
ChengLei Shao 2022-11-22 10:13:55 +08:00 committed by GitHub
parent fba8f253d4
commit 466aa4987e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 2 deletions

View File

@ -45,6 +45,7 @@ export class SortField extends Field {
const totalCount = await this.collection.repository.count({
transaction,
});
const emptyCount = await this.collection.repository.count({
filter: {
[this.name]: null,

View File

@ -210,12 +210,21 @@ export class Repository<TModelAttributes extends {} = any, TCreationAttributes e
};
}
const count = await this.collection.model.count({
const queryOptions: any = {
...options,
distinct: true,
distinct: Boolean(this.collection.model.primaryKeyAttribute),
};
if (queryOptions.include?.length === 0) {
delete queryOptions.include;
}
const count = await this.collection.model.count({
...queryOptions,
transaction,
});
// @ts-ignore
return count;
}

View File

@ -75,6 +75,18 @@ describe('collections repository', () => {
await app.destroy();
});
it('should create collection without id', async () => {
const response = await agent.resource('collections').create({
values: {
name: 'test',
autoGenId: false,
sortable: true,
},
});
expect(response.statusCode).toBe(200);
});
it('case 1', async () => {
const response1 = await app.agent().resource('posts').create();
const postId = response1.body.data.id;