Skip to content

fix: send queries without parameters without driver formatting - #1794

Open
ahmedpdf445-stack wants to merge 1 commit into
datajoint:masterfrom
ahmedpdf445-stack:fix/literal-percent-in-comments
Open

ahmedpdf445-stack wants to merge 1 commit into
datajoint:masterfrom
ahmedpdf445-stack:fix/literal-percent-in-comments

Conversation

@ahmedpdf445-stack

@ahmedpdf445-stack ahmedpdf445-stack commented Dec 5, 2026 •

Copy link
Copy Markdown

Fixes #1789

The bug

A table or attribute comment that holds a literal % fails to declare, on both backends:

  • MySQL (PyMySQL): ProgrammingError: not enough arguments for format string
  • PostgreSQL (psycopg2): IndexError: tuple index out of range

Connection.query always formats the statement with its arguments, even when there are none, so the % in the comment is read as a placeholder. Writing %% works around it today, but the comment is then stored with a single %.

The fix

quote_comment doubles a percent sign, the escape _comment_literal in migrate.py already applies to comments. The formatted statement then holds the comment as declared, on both adapters, for column comments, table comments and the ALTER statements that quote comments. A comment written as %% is now stored as %% instead of %.

I first tried sending the statement to the cursor without parameters. That fixes the comments, but it breaks the places that build %% for LIKE patterns (condition.py, dependencies.py) and the tests that pin them, so the escape stays where the rest of the code base keeps it.

Tests

tests/unit/test_comment_percent.py, no database needed:

  • test_mysql_comment_keeps_the_percent and test_postgres_comment_keeps_the_percent format the quoted comment the way the driver does and assert it reads back as declared, for 50%, 100%%, 50% alone and % alone. They fail before the change.
  • test_mysql_create_table_sql_keeps_the_percent covers the column and table comment of a created table.

pytest tests/unit 1925 passed, ruff check clean.

A comment that holds a literal % fails to declare: Connection.query always
formats the statement with its arguments, even when there are none, so the sign
is read as a placeholder (MySQL: not enough arguments for format string,
PostgreSQL: tuple index out of range). Doubling it in quote_comment stores the
comment as declared, and %% is then stored as written instead of collapsing to
%. This is the escape _comment_literal in migrate.py already applies.
@ahmedpdf445-stack
ahmedpdf445-stack force-pushed the fix/literal-percent-in-comments branch from a8213a6 to 0aca8bb Compare December 5, 2026 09:41

This branch has not been deployed

No deployments
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.

A % in a table or attribute comment fails to declare; %% is stored as %

1 participant