Skip to content

πŸ”₯ πŸ”₯ πŸ”₯ A Free & Self-hostable Airtable Alternative

fix: ensure stable ordering by PK or CreatedTime when duplicates exist

Pranav C3dca69c8c3b2
diff --git a/packages/nocodb/src/db/BaseModelSqlv2.ts b/packages/nocodb/src/db/BaseModelSqlv2.ts
index 2346675b7f..4617d0d40a 100644
--- a/packages/nocodb/src/db/BaseModelSqlv2.ts
+++ b/packages/nocodb/src/db/BaseModelSqlv2.ts
@@ -550,12 +550,17 @@ class BaseModelSqlv2 implements IBaseModelSqlV2 {
}
const orderColumn = columns.find((c) => isOrderCol(c));
- // sort by primary key if not autogenerated string
- // if autogenerated string sort by created_at column if present
+ // sort by order column if present
if (orderColumn) {
qb.orderBy(orderColumn.column_name);
- } else if (this.model.primaryKey && this.model.primaryKey.ai) {
+ }
+
+ // Ensure stable ordering:
+// - Use auto-increment PK if available
+// - Otherwise, fallback to system CreatedTime
+// This avoids issues when order column has duplicates
+if (this.model.primaryKey && this.model.primaryKey.ai) {
qb.orderBy(this.model.primaryKey.column_name);
} else {
const createdCol = this.model.columns.find(
diff --git a/packages/nocodb/src/db/field-handler/handlers/long-text/long-text.general.handler.ts b/packages/nocodb/src/db/field-handler/handlers/long-text/long-text.general.handler.ts
index fd4ea1b502..af643283aa 100644
--- a/packages/nocodb/src/db/field-handler/handlers/long-text/long-text.general.handler.ts
+++ b/packages/nocodb/src/db/field-handler/handlers/long-text/long-text.general.handler.ts
@@ -1,11 +1,7 @@
import { NcError } from 'src/helpers/catchError';
import { NC_MAX_TEXT_LENGTH } from 'src/constants';
-import {
- AIRecordType,
- isAIPromptCol,
- ncIsObject,
- type NcContext,
-} from 'nocodb-sdk';
+import { isAIPromptCol, type NcContext, ncIsObject } from 'nocodb-sdk';
+import type { AIRecordType } from 'nocodb-sdk';
import type { IBaseModelSqlV2 } from 'src/db/IBaseModelSqlV2';
import type { MetaService } from 'src/meta/meta.service';
import type { Column } from '~/models';
diff --git a/packages/nocodb/src/services/galleries.service.ts b/packages/nocodb/src/services/galleries.service.ts
index b1adff7ab8..9e330a7f66 100644
--- a/packages/nocodb/src/services/galleries.service.ts
+++ b/packages/nocodb/src/services/galleries.service.ts
@@ -84,7 +84,7 @@ export class GalleriesService {
context,
});
- await view.getView(context)
+ await view.getView(context);
NocoSocket.broadcastEvent(
context,