Skip to content

Exception when using quoted_name as a parameter #12204

@ghost

Description

Describe the bug

When attempting to use a quoted_name, such as Table.name as a parameter value, attempting to compile a statement results in exception.

Optional link from https://docs.sqlalchemy.org which documents the behavior that is expected

No response

SQLAlchemy Version in Use

2.0.36

DBAPI (i.e. the database driver)

n/a

Database Vendor and Major Version

any

Python Version

3.12.7

Operating system

any

To Reproduce

import sqlalchemy as sa
table = sa.Table("foo", sa.MetaData())
lit = sa.literal(table.name)
# Following prints NULL
print(lit.type) 
# Following results in sqlalchemy.exc.CompileError
sa.text("SELECT :name").bindparams(name=table.name).compile(compile_kwargs=dict(literal_binds=True))

Error

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/mick/repo/graft/.venv/lib/python3.12/site-packages/sqlalchemy/sql/elements.py", line 308, in compile
    return self._compiler(dialect, **kw)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/mick/repo/graft/.venv/lib/python3.12/site-packages/sqlalchemy/sql/elements.py", line 316, in _compiler
    return dialect.statement_compiler(dialect, self, **kw)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/mick/repo/graft/.venv/lib/python3.12/site-packages/sqlalchemy/sql/compiler.py", line 1429, in __init__
    Compiled.__init__(self, dialect, statement, **kwargs)
  File "/Users/mick/repo/graft/.venv/lib/python3.12/site-packages/sqlalchemy/sql/compiler.py", line 870, in __init__
    self.string = self.process(self.statement, **compile_kwargs)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/mick/repo/graft/.venv/lib/python3.12/site-packages/sqlalchemy/sql/compiler.py", line 915, in process
    return obj._compiler_dispatch(self, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/mick/repo/graft/.venv/lib/python3.12/site-packages/sqlalchemy/sql/visitors.py", line 141, in _compiler_dispatch
    return meth(self, **kw)  # type: ignore  # noqa: E501
           ^^^^^^^^^^^^^^^^
  File "/Users/mick/repo/graft/.venv/lib/python3.12/site-packages/sqlalchemy/sql/compiler.py", line 2679, in visit_textclause
    BIND_PARAMS.sub(
  File "/Users/mick/repo/graft/.venv/lib/python3.12/site-packages/sqlalchemy/sql/compiler.py", line 2663, in do_bindparam
    return self.process(textclause._bindparams[name], **kw)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/mick/repo/graft/.venv/lib/python3.12/site-packages/sqlalchemy/sql/compiler.py", line 915, in process
    return obj._compiler_dispatch(self, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/mick/repo/graft/.venv/lib/python3.12/site-packages/sqlalchemy/sql/visitors.py", line 141, in _compiler_dispatch
    return meth(self, **kw)  # type: ignore  # noqa: E501
           ^^^^^^^^^^^^^^^^
  File "/Users/mick/repo/graft/.venv/lib/python3.12/site-packages/sqlalchemy/sql/compiler.py", line 3699, in visit_bindparam
    ret = self.render_literal_bindparam(
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/mick/repo/graft/.venv/lib/python3.12/site-packages/sqlalchemy/sql/compiler.py", line 3838, in render_literal_bindparam
    return self.render_literal_value(value, bindparam.type)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/mick/repo/graft/.venv/lib/python3.12/site-packages/sqlalchemy/sql/compiler.py", line 3871, in render_literal_value
    raise exc.CompileError(
sqlalchemy.exc.CompileError: No literal value renderer is available for literal value "'foo'" with datatype NULL

Additional context

I am guessing the bug is in sqlalchemy/sql/sqltypes.py's _resolve_value_to_type

Metadata

Metadata

Assignees

No one assigned

    Labels

    expected behaviorthat's how it's meant to work. consider the "documentation" label in additionwontfix / out of scopesomething we decided we aren't doing, for whatever reason

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions