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()