Skip to content

Return 400 instead of 500 when query name/description exceed column limits - #7790

Open
ekanshul wants to merge 1 commit into
getredash:masterfrom
ekanshul:fix-long-query-name-400
Open

ekanshul wants to merge 1 commit into
getredash:masterfrom
ekanshul:fix-long-query-name-400

Conversation

@ekanshul

@ekanshul ekanshul commented Aug 19, 2026

Copy link
Copy Markdown

What type of PR is this?

  • Bug Fix

Description

Fixes #7478.

Creating (or updating) a query with a name longer than 255 characters returned a 500: the handler passed the value straight through to the database, where the insert failed with psycopg2.errors.StringDataRightTruncation (value too long for type character varying(255)). The same applies to description (varchar(4096)).

This adds a small require_valid_field_lengths() check in redash/handlers/queries.py, used by both QueryListResource.post (create) and QueryResource.post (update). It reads the maximum lengths from the model's column definitions (no duplicated magic numbers) and aborts with 400 and a message like Query name must be at most 255 characters long.

How is this tested?

  • Unit tests (pytest, jest)

New tests in tests/handlers/test_queries.py:

  • TestQueryListResourcePost::test_rejects_too_long_name
  • TestQueryListResourcePost::test_rejects_too_long_description
  • TestQueryResourcePost::test_update_query_rejects_too_long_name

Before the fix these fail with sqlalchemy.exc.DataError (the 500 from the issue); with the fix pytest tests/handlers/test_queries.py passes (37 passed). ruff check and black --check are clean.

Related Tickets & Documents

#7478

Mobile & Desktop Screenshots/Recordings (if there are UI changes)

N/A (backend only)

Review in cubic

POST /api/queries and POST /api/queries/<id> passed the user-supplied name
straight to the database, so a name longer than the varchar(255) column
(or a description longer than varchar(4096)) failed with
StringDataRightTruncation and surfaced as a 500. Validate the lengths
against the column definitions and abort with a 400 and a clear message.

Fixes getredash#7478

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@greptile-apps

greptile-apps Bot commented Aug 19, 2026

Copy link
Copy Markdown

Greptile Summary

The PR prevents oversized query names and descriptions from reaching PostgreSQL by validating them against the SQLAlchemy column limits during query creation and updates.

  • Adds shared model-derived length validation for name and description.
  • Applies validation to the primary query create and update endpoints.
  • Adds handler tests covering oversized names and descriptions.

Confidence Score: 5/5

The PR appears safe to merge with no actionable defects identified in the changed paths.

The validation uses the current model column limits, covers both primary creation and update handlers before persistence, and is backed by focused regression tests.

Important Files Changed

Filename Overview
redash/handlers/queries.py Adds correctly placed validation against the existing 255-character name and 4096-character description limits.
tests/handlers/test_queries.py Adds focused regression coverage for oversized fields on query creation and update.

Reviews (1): Last reviewed commit: "Return 400 instead of 500 for over-long ..." | Re-trigger Greptile

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 2 files

Re-trigger cubic

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

500 error returned when creating query with long name

1 participant