Skip to content

NameError: name 'FetchedValue' is not defined #1633

Closed

Description

@khteh

Describe the bug

pipenv run alembic upgrade head hit a NameError

Expected behavior

To Reproduce
Please try to provide a Minimal, Complete, and Verifiable example, with the migration script and/or the SQLAlchemy tables or models involved.
See also Reporting Bugs on the website.

    created_at: Mapped[datetime] = mapped_column(
        DateTime(timezone=True),
        default=sa.func.now(),
        server_default=sa.FetchedValue()
    )

Error

$ pipenv run alembic upgrade head
Loading .env environment variables...
INFO  [alembic.runtime.migration] Context impl PostgresqlImpl.
INFO  [alembic.runtime.migration] Will assume transactional DDL.
INFO  [alembic.runtime.migration] Running upgrade  -> ca93a9d04c54, Initial migration
Traceback (most recent call last):
  File "/home/khteh/.local/share/virtualenvs/PythonRestAPI-6TwrpjXm/bin/alembic", line 8, in <module>
    sys.exit(main())
             ^^^^^^
  File "/home/khteh/.local/share/virtualenvs/PythonRestAPI-6TwrpjXm/lib/python3.12/site-packages/alembic/config.py", line 636, in main
    CommandLine(prog=prog).main(argv=argv)
  File "/home/khteh/.local/share/virtualenvs/PythonRestAPI-6TwrpjXm/lib/python3.12/site-packages/alembic/config.py", line 626, in main
    self.run_cmd(cfg, options)
  File "/home/khteh/.local/share/virtualenvs/PythonRestAPI-6TwrpjXm/lib/python3.12/site-packages/alembic/config.py", line 603, in run_cmd
    fn(
  File "/home/khteh/.local/share/virtualenvs/PythonRestAPI-6TwrpjXm/lib/python3.12/site-packages/alembic/command.py", line 408, in upgrade
    script.run_env()
  File "/home/khteh/.local/share/virtualenvs/PythonRestAPI-6TwrpjXm/lib/python3.12/site-packages/alembic/script/base.py", line 586, in run_env
    util.load_python_file(self.dir, "env.py")
  File "/home/khteh/.local/share/virtualenvs/PythonRestAPI-6TwrpjXm/lib/python3.12/site-packages/alembic/util/pyfiles.py", line 95, in load_python_file
    module = load_module_py(module_id, path)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/khteh/.local/share/virtualenvs/PythonRestAPI-6TwrpjXm/lib/python3.12/site-packages/alembic/util/pyfiles.py", line 113, in load_module_py
    spec.loader.exec_module(module)  # type: ignore
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap_external>", line 995, in exec_module
  File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
  File "/usr/src/Python/PythonRestAPI/migrations/env.py", line 92, in <module>
    run_migrations_online()
  File "/usr/src/Python/PythonRestAPI/migrations/env.py", line 86, in run_migrations_online
    context.run_migrations()
  File "<string>", line 8, in run_migrations
  File "/home/khteh/.local/share/virtualenvs/PythonRestAPI-6TwrpjXm/lib/python3.12/site-packages/alembic/runtime/environment.py", line 946, in run_migrations
    self.get_context().run_migrations(**kw)
  File "/home/khteh/.local/share/virtualenvs/PythonRestAPI-6TwrpjXm/lib/python3.12/site-packages/alembic/runtime/migration.py", line 623, in run_migrations
    step.migration_fn(**kw)
  File "/usr/src/Python/PythonRestAPI/migrations/versions/ca93a9d04c54_initial_migration.py", line 30, in upgrade
    sa.Column('created_at', sa.DateTime(timezone=True), server_default=FetchedValue(), nullable=False),
                                                                       ^^^^^^^^^^^^
NameError: name 'FetchedValue' is not defined

Versions.

  • OS: Ubuntu 24.10
  • Python: 3.12.7
  • Alembic: 1.15.1
  • SQLAlchemy: 2.0.39
  • Database: PostgreSQL
  • DBAPI:

Additional context

Have a nice day!

Activity

  1. khteh commented on Mar 27, 2025

    @khteh
    Author

    Solution is to add from sqlalchemy import FetchedValue to the generated script

  2. CaselIT commented on Mar 31, 2025

    @CaselIT
    Member

    @zzzeek I'm not sure if this is considered something the user needs to adapt or it it's generally a bug?

  3. zzzeek commented on Mar 31, 2025

    @zzzeek
    Member

    looks like a bug, FetchedValue we would hope is preceded by sa.

  4. added a commit that references this issue on Jul 7, 2025
    468ad59
  5. sqla-tester commented on Jul 8, 2025

    @sqla-tester
    Collaborator

    david-fed has proposed a fix for this issue in the main branch:

    Fixed missing prefix on FetchedValue https://gerrit.sqlalchemy.org/c/sqlalchemy/alembic/+/6025

  6. added a commit that references this issue on Jul 9, 2025
    aa2ccd8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions