From 2d03af81efb90e6ec50a37f2ff11322ee58c5d9b Mon Sep 17 00:00:00 2001 From: Classic298 <27028174+Classic298@users.noreply.github.com> Date: Mon, 30 Nov 2026 15:41:44 +0100 Subject: [PATCH] fix: paged lists repeat some entries and skip others (#35281) The admin user list, feedbacks, groups, the workspace models, prompts and skills lists, prompt history, notes, shared chats, automations, calendar search, analytics messages and SCIM user listings are loaded page by page, and when many rows share the sorted value (a role, a bulk-import timestamp, a rating) the database is free to return those ties in a different order for each page. On PostgreSQL 16, paging through 300 users sorted by role showed 16 users twice and never showed 16 others, and with an id tiebreaker every user showed up exactly once. These lists now sort by each entry's id as a last step, as the chats and knowledge lists already do. Fixes #35280 --- backend/open_webui/models/automations.py | 2 +- backend/open_webui/models/calendar.py | 2 +- backend/open_webui/models/chat_messages.py | 4 ++-- backend/open_webui/models/feedbacks.py | 4 +++- backend/open_webui/models/groups.py | 2 +- backend/open_webui/models/models.py | 2 ++ backend/open_webui/models/notes.py | 4 +++- backend/open_webui/models/prompt_history.py | 2 +- backend/open_webui/models/prompts.py | 2 ++ backend/open_webui/models/shared_chats.py | 2 ++ backend/open_webui/models/skills.py | 2 ++ backend/open_webui/models/users.py | 4 ++++ 12 files changed, 24 insertions(+), 8 deletions(-) diff --git a/backend/open_webui/models/automations.py b/backend/open_webui/models/automations.py index cf11c8d70d..89ea00cee6 100644 --- a/backend/open_webui/models/automations.py +++ b/backend/open_webui/models/automations.py @@ -293,7 +293,7 @@ class AutomationTable: elif status == 'paused': stmt = stmt.filter(Automation.is_active == False) - stmt = stmt.order_by(Automation.created_at.desc()) + stmt = stmt.order_by(Automation.created_at.desc(), Automation.id) # Get total count count_result = await db.execute(select(func.count()).select_from(stmt.subquery())) diff --git a/backend/open_webui/models/calendar.py b/backend/open_webui/models/calendar.py index 7bcd393ebc..78210d5309 100644 --- a/backend/open_webui/models/calendar.py +++ b/backend/open_webui/models/calendar.py @@ -628,7 +628,7 @@ class CalendarEventTable: ) ) - stmt = stmt.order_by(CalendarEvent.start_at.desc()) + stmt = stmt.order_by(CalendarEvent.start_at.desc(), CalendarEvent.id) count_result = await db.execute(select(func.count()).select_from(stmt.subquery())) total = count_result.scalar() diff --git a/backend/open_webui/models/chat_messages.py b/backend/open_webui/models/chat_messages.py index 43ab06316c..56aad8af30 100644 --- a/backend/open_webui/models/chat_messages.py +++ b/backend/open_webui/models/chat_messages.py @@ -548,7 +548,7 @@ class ChatMessageTable: result = await db.execute( select(ChatMessage) .filter_by(user_id=user_id) - .order_by(ChatMessage.created_at.desc()) + .order_by(ChatMessage.created_at.desc(), ChatMessage.id) .offset(skip) .limit(limit) ) @@ -570,7 +570,7 @@ class ChatMessageTable: stmt = stmt.filter(ChatMessage.created_at >= start_date) if end_date: stmt = stmt.filter(ChatMessage.created_at <= end_date) - stmt = stmt.order_by(ChatMessage.created_at.desc()).offset(skip).limit(limit) + stmt = stmt.order_by(ChatMessage.created_at.desc(), ChatMessage.id).offset(skip).limit(limit) result = await db.execute(stmt) messages = result.scalars().all() return [ChatMessageModel.model_validate(message) for message in messages] diff --git a/backend/open_webui/models/feedbacks.py b/backend/open_webui/models/feedbacks.py index 0d6ef3bcc5..484098d502 100644 --- a/backend/open_webui/models/feedbacks.py +++ b/backend/open_webui/models/feedbacks.py @@ -254,6 +254,8 @@ class FeedbackTable: else: stmt = stmt.order_by(Feedback.created_at.desc()) + stmt = stmt.order_by(Feedback.id) + # Count before pagination without wrapping the ordered item query. count_result = await db.execute(count_stmt) total = count_result.scalar() or 0 @@ -435,7 +437,7 @@ class FeedbackTable: select(Feedback, User) .join(User, Feedback.user_id == User.id) .filter(Feedback.user_id == user_id) - .order_by(Feedback.updated_at.desc()) + .order_by(Feedback.updated_at.desc(), Feedback.id) ) count_result = await db.execute(select(func.count()).select_from(stmt.subquery())) diff --git a/backend/open_webui/models/groups.py b/backend/open_webui/models/groups.py index 0162be1b5b..889f910495 100644 --- a/backend/open_webui/models/groups.py +++ b/backend/open_webui/models/groups.py @@ -419,7 +419,7 @@ class GroupTable: result = await db.execute( select(Group, member_count) .where(Group.id.in_(select(stmt.subquery().c.id))) - .order_by(Group.updated_at.desc()) + .order_by(Group.updated_at.desc(), Group.id) .offset(skip) .limit(limit) ) diff --git a/backend/open_webui/models/models.py b/backend/open_webui/models/models.py index 5677b703e8..791269b920 100755 --- a/backend/open_webui/models/models.py +++ b/backend/open_webui/models/models.py @@ -574,6 +574,8 @@ class ModelsTable: else: stmt = stmt.order_by(Model.created_at.desc()) + stmt = stmt.order_by(Model.id) + # Count BEFORE pagination count_result = await db.execute(select(func.count()).select_from(stmt.subquery())) total = count_result.scalar() diff --git a/backend/open_webui/models/notes.py b/backend/open_webui/models/notes.py index 6c551ec1c4..28b509309f 100644 --- a/backend/open_webui/models/notes.py +++ b/backend/open_webui/models/notes.py @@ -701,6 +701,8 @@ class NoteTable: else: stmt = stmt.order_by(Note.updated_at.desc()) + stmt = stmt.order_by(Note.id) + # Count BEFORE pagination count_result = await db.execute(select(func.count()).select_from(stmt.subquery())) total = count_result.scalar() @@ -747,7 +749,7 @@ class NoteTable: user_groups = await Groups.get_groups_by_member_id(user_id, db=db, include_inherited=True) user_group_ids = [group.id for group in user_groups] - stmt = select(Note).order_by(Note.updated_at.desc()) + stmt = select(Note).order_by(Note.updated_at.desc(), Note.id) stmt = self._has_permission(db, stmt, {'user_id': user_id, 'group_ids': user_group_ids}, permission) if skip is not None: diff --git a/backend/open_webui/models/prompt_history.py b/backend/open_webui/models/prompt_history.py index 2874b72f0d..7ed700fb5e 100644 --- a/backend/open_webui/models/prompt_history.py +++ b/backend/open_webui/models/prompt_history.py @@ -81,7 +81,7 @@ class PromptHistoryTable: result = await db.execute( select(PromptHistory) .filter(PromptHistory.prompt_id == prompt_id) - .order_by(PromptHistory.created_at.desc()) + .order_by(PromptHistory.created_at.desc(), PromptHistory.id.desc()) .offset(offset) .limit(limit) ) diff --git a/backend/open_webui/models/prompts.py b/backend/open_webui/models/prompts.py index d7252d665e..5989ab8267 100644 --- a/backend/open_webui/models/prompts.py +++ b/backend/open_webui/models/prompts.py @@ -382,6 +382,8 @@ class PromptsTable: else: query = query.order_by(Prompt.updated_at.desc()) + query = query.order_by(Prompt.id) + # Count BEFORE pagination count_result = await session.execute(select(func.count()).select_from(query.subquery())) total = count_result.scalar() diff --git a/backend/open_webui/models/shared_chats.py b/backend/open_webui/models/shared_chats.py index fff8d372cc..299ebf0204 100644 --- a/backend/open_webui/models/shared_chats.py +++ b/backend/open_webui/models/shared_chats.py @@ -188,6 +188,8 @@ class SharedChatsTable: else: stmt = stmt.order_by(SharedChat.updated_at.desc()) + stmt = stmt.order_by(SharedChat.id) + if skip: stmt = stmt.offset(skip) if limit: diff --git a/backend/open_webui/models/skills.py b/backend/open_webui/models/skills.py index 9a3f17952f..d71272e7ea 100644 --- a/backend/open_webui/models/skills.py +++ b/backend/open_webui/models/skills.py @@ -344,6 +344,8 @@ class SkillsTable: else: stmt = stmt.order_by(Skill.updated_at.desc()) + stmt = stmt.order_by(Skill.id) + # Count BEFORE pagination count_result = await db.execute(select(func.count()).select_from(stmt.subquery())) total = count_result.scalar() diff --git a/backend/open_webui/models/users.py b/backend/open_webui/models/users.py index 89747c3e51..9680fa9d00 100644 --- a/backend/open_webui/models/users.py +++ b/backend/open_webui/models/users.py @@ -529,6 +529,8 @@ class UsersTable: if order_by == 'created_at': stmt = stmt.order_by(User.created_at.asc() if direction == 'asc' else User.created_at.desc()) + stmt = stmt.order_by(User.id) + count_result = await session.execute(select(func.count()).select_from(stmt.subquery())) total = count_result.scalar() @@ -679,6 +681,8 @@ class UsersTable: elif not filter: stmt = stmt.order_by(User.created_at.desc()) + stmt = stmt.order_by(User.id) + # Count BEFORE pagination count_result = await session.execute(select(func.count()).select_from(stmt.subquery())) total = count_result.scalar()